-1

i'm new in C++.i am basically a C programmer.At present i'm learning about reference.It's very confusing.In C a pointer is the address of the variable and there is a difference b/w pointer and pointer variable.However many times pointer variable short-handed as pointer.

1.My question is that what is meant by reference then.In c++ are reference and address of variable means the same thing?If some one differentiate pointer from reference do he/she mean pointer variable and not pointer.They just written the pointer variable as pointer? If No then what is the difference b/w pointer and reference?

2.In C &a=b is illegal.But in C++ it is not.Why? please help as i am ver confused with it.I had searched about it but couldn't find the satisfactory answer.

1 Answers1

0

Pointers can be null. You can do arithmetic on pointers.

References cannot be null & must refer to an object. You cannot to arithmetic on references.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 3
    Careful when you say "cannot". C++ generally doesn't stop you from doing something, even if that something is completely insane. – tadman Oct 29 '14 at 17:14
  • 1
    @tadman: They cannot be null, unless you go outside the language, where all bets are off. But that restriction is always implicit, as long as we are not in a contest how to most artistically invoke UB. – Deduplicator Oct 29 '14 at 17:17
  • `char* x = NULL; char& y = *x;` You can do things like this unintentionally. It's probably safer to say they are *generally* not `NULL`, and if they are it's probably a bug, where pointers are *often* `NULL` so you need to be extra careful when dereferencing them. – tadman Oct 29 '14 at 17:19
  • 2
    @Deduplicator Mistakenly deferencing a null pointer at run-time and storing the result in a reference can happen. By saying references "cannot" be null, it can lure people into a false sense of security. – Neil Kirk Oct 29 '14 at 17:19
  • 1
    @NeilKirk: You invoked UB before you had the reference, when you dereference the null-pointer. There's no use in crying over spilt milk, especially as the compiler assumes the program's state is not irretrievably corrupted (and thus checking whether a reference does not point anywhere is completely useless). – Deduplicator Oct 29 '14 at 17:24
  • @tadman: Sure you can invoke UB unintentionally. Few people intentionally write buggy software. – Deduplicator Oct 29 '14 at 17:26
  • @Deduplicator So suppose I am a beginner in the real world and I have to debug a program that is deferencing null pointers. Aha, I say. I will change all my parameters to references, as they can't be null! – Neil Kirk Oct 29 '14 at 17:28
  • 1
    @NeilKirk Method signatures with `const x&` are a lot easier to deal with than those with `x*`. Modern C++ prefers references whenever practical, especially `const` ones. – tadman Oct 29 '14 at 17:30
  • @tadman I know I'm talking from the perspective of a C++ beginner from C who just saw that advice. – Neil Kirk Oct 29 '14 at 17:31