I'm really lost on this topic. I'm trying to study for an exam.

1)When the symbol '&' is in front of a variable, it is referring to the address right? How is it different from a reference parameter? I keep getting them mixed up.

2)By dereferencing it, it means that you want to value of it to display rather than the address?

3) For Dynamic Memory Allocations, why do you use the 'new' operator to store contents of an array? I never understood the reason for it and how it is used.

class C; 
C obj;
C& function_byref (C& c);  //passing by refernce and returning a reference to an object of type C
C function_2byref (C* c);  //same way of passing an argument but returning only a copy of an object of type C
C function_byval (C c); //receives a copy of an object of type C and returns a copy of type C

calling:

function_byref (obj); //can modify 'obj' and return a reference to it
function_2byref(&obj); //here you must pass the address of obj directly
function_byval (obj); //the functions gets a copy of obj and returns a copy of another object (Say obj)
int* x; //x is declared as a pointer to an object of type int
int value; 

x=&value; //now x is a pointer to value 
//so new you can modify value indirectly like this
*x=34; //value will hold 34


//as for the 'new' operator it returns a pointer to a dynamic object allocated on the heap
//so by declaring :
int* y=new int(5); //you get a pointer to a variable of type int allocated on the heap
//which you must subsequently free using the 'delete' operator
delete y;

1) It isn't, really. Reference param passes the address into the function rather than the value.

2) Yes

3) new doesn't store anything. It defines space and puts the address of that space in the variable specified.
Consider variables as boxes you store stuff in. Each box can hold a specific type (books, school papers, clothes) and amount of data. Well, new is like yelling to Mom "Hey, I need another box!" She gives you one after making you say Please and you can store your action figures in it. It just gives you more storage space you didn't have when you started.

Thanks, makes more sense now. Those were great examples^

Questions about different types of pointers

Constant pointers mean that once you point to an address, you won't be able to point to anything else? So changing the value of an address that a pointer is pointing to is the only way to change the value of the pointer? Like:

int value = 111;
int value2 = 222;
int * const ptr = &value //Can't do ptr = &value2 after this?

value = 1111; //So only way to change ptr is this?

All these "Pointer to a constant", "Constant points to constant" are confusing me. Wouldn't "pointer to a constant" be the same as constant pointers?

>>Constant pointers mean that once you point to an address, you won't be able to point to anything else?

Yes.

>>So changing the value of an address that a pointer is pointing to is the only way to change the value of the pointer?

The value of a pointer IS the address it is pointing to:
"the value of an address that a pointer is pointing to" = the pointer's value
"the value of the pointer" = the pointer's value
These two phrases are synonymous. Do not confuse the "value of the pointer" with the "value to pointer points to", the latter is the value you get by de-referencing the pointer.

>>All these "Pointer to a constant", "Constant points to constant" are confusing me. Wouldn't "pointer to a constant" be the same as constant pointers? const int* ptr or int* const ptr : these are two alternative syntax for declaring a "constant pointer". This means that the address stored in the pointer (i.e. the pointer's value) cannot be changed after initialization. This does not mean that the value pointed to by the pointer cannot be changed, it can:

int value 111;
int * const ptr = &value;
(*ptr) = 231; //this is ok, dereferencing the pointer and writing a new value at that location.

int const * ptr : This is a non-constant pointer to a constant value. This means that you can change the pointer's value, but you cannot change the value pointed to by the pointer:

const int value = 111;
int value2 = 222;
int const * ptr = &value; // OK.
ptr = &value2; //OK, but ptr cannot be used to modify value2
(*ptr) = 1231; //ERROR: cannot modify a const value.

int const * const ptr : This simply a constant pointer to a constant value. This means that neither the pointer's value nor the value it points to can be modified after initialization of ptr.

Thank you, it is a lot clearer now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.