Assigning a value to a pointer

For the statement: x = func(a); , do we say that we are returning an address to x ? Or, how exactly do we read it? EDIT: Is it eligible to say that we are returning a pointer to x ? If so, can you explain how this is done exactly? I mean, how are we returning a pointer?

383k 77 77 gold badges 659 659 silver badges 1.1k 1.1k bronze badges asked Jul 21, 2011 at 16:07 Simplicity Simplicity 48.6k 101 101 gold badges 263 263 silver badges 392 392 bronze badges

7 Answers 7

x is a pointer to an int , so in other words it is the address of a memory location where an int is stored. So x = func(a) means that func returns the address of an int and stores it in the variable x .

Take care not to return the address of a local variable whose contents would be undefined after func returns.

answered Jul 21, 2011 at 16:09 Alexander Gessler Alexander Gessler 46.4k 7 7 gold badges 84 84 silver badges 124 124 bronze badges

EDIT: Is it eligible to say that we are returning a pointer to x? If so, can you explain how is this done exactly? I mean, how are we returning a pointer?

Yes, it is certainly eligible. Try and think of and treat pointers as any other data type. Under the hood, they are just memory addresses. A pointer can be returned the same way any other data type can be returned.

Take this code for example:

int* giveMeAPointer() < return y; >int* x = giveMeAPointer(); 

Say that "y" is declared globally as: "int* y = . ". Now the memory address being pointed to by "x" is the same as the memory address being pointed to by "y".

Now let's say that "y" was -not- declared as a pointer, but instead as a normal int (e.g. "int y = 5"). The function could do this and still be valid:

int* giveMeAPointer()
answered Jul 21, 2011 at 16:25 1,459 1 1 gold badge 13 13 silver badges 22 22 bronze badges

*x is points to int typed variable in memory. So function func should return address to int.

int *x; x = new int; // create your own int *x = 5; // access - set it's value delete x; // delete int - free memory x = getGlobalCounter(); (*x)++; // increment global pointer 

For example the getGlobalCounter function:

static int counter; int *getGlobalCounter() < return &counter; // return address of counter >

But isn't always good idea to delete objects returned from functions. In that case it should result in runtime error, because of counter isn't dynamically allocated int as in top example.

answered Jul 21, 2011 at 16:27 11k 17 17 gold badges 67 67 silver badges 116 116 bronze badges

If you are assigning a variable's value to the return-type of a function, then that return-type must match the variable's type. This goes the same for pointers.

int* myPointer; 
int* func(); 

Then setting myPointer equal to func() will change the memory address which "myPointer" points to, to the memory address returned by func().

answered Jul 21, 2011 at 16:11 1,459 1 1 gold badge 13 13 silver badges 22 22 bronze badges

x is a pointer, specifically to an int. So it should be obvious that there are two memory locations used. One for the pointer and one for the memory it's pointing to (assuming the pointer is not null).

The pointer itself holds a memory address, specifically the location of the memory it's pointing to. Something like 0xa456e4fa.

Yes, func() is returning a pointer. The prototype of func would look like the following..

int * func(someObj a); //I don't know the datatype of your parameter, //so I'm just making something up. 

Notice the return type is a pointer to an int. From this and what I said previously, it should be obvious what this will return. Something of the form 0xyyyyyy, or a memory address/pointer. That memory address goes into your x pointer.

Remember that the pointer itself is not holding the data that it's pointing to, it is only the address. There's really no reason you CAN'T return a pointer. However, you do need to be careful in WHAT you return. You do not want to return the address of a local variable because that variable will go out of scope once the function has completed its execution. You'll then be returning the address of something invalid. Returning the VALUE of a local pointer however, is fine because the value you returned (the address) will be preserved as will the memory it's pointing to.

I also just realized I wrote you a book. Damn, I sure do ramble when I'm tired.