Bloomberg LP Interview Question
Software Engineer / Developerswe can not have references to pointer, this is standard C++ rule defined. Program wont compile
We can have references to pointer.
int *p = 0;
int *&r = p; //valid. r is reference to pointer to int
Code above is valid. Since p is pointing to null, access to null pointer i.e., either *p or *r will lead to segv
std::cout << r << std::endl; // no probs. prints 0 is null pointer
std::cout << *r << std::endl; // segv
std::cout << *p << std::endl; // segv
2 main diff. are
- Pratik March 23, 20061) References can not be bound to null, where as pointer can be. So that when passing function argument, if passed by reference we do not need to explicitly check if reference is null whereas for pointers it is better to check if it is null. (normally nobody does).
2) No memory is allocated for references, whereas pointer it self is a variable and takes storage space. Reference is an alias.
Although, null reference can be created.
e.g. int *p = 0;
int& r = p;