I've got a simple question but I can't seem to find the answer in any of my searchs. I know this is very basic and probably will require 1-3 sentences but, when I use a pointer to pass it by reference to a function, will it use the actual location or use the variable that it's pointing at.

Again, this is probably very, very basic but I can't seem to find the answer.


Thanks,

jt

Recommended Answers

All 3 Replies

When you pass a variable's address via a pointer, your just saying this is where I am. I hold the location of the variable in my value or I hold the value NULL.

there are different scenarios for different purposes:

void f1(int i); // call by value
void f2(int* i); // pointer
void f3(int& i); // call by ref
void f4(int** i); // pointer to a pointer
void f5(int*& i); // pointer passed by ref
void f6(const int i); // call by value same as f1
void f7(const int* i); // const pointer
void f8(const int& i); // call by const ref
void f9(const int** i); // pointer to a const pointer
void f10(const int*& i); // pointer passed by const ref
//...

all these are used for different purposes, f1,f2,f3, f8 probably most commonly used.
f1 makes a copy of the value/object passed - the original is not changed even if inside the body of f1 the value is changed.
f2 passes the address of the object/value. Changes to the value itself that happen inside the body of f2 are done on the object that i points to. changes to i (the pointer) are not reflected, so when you leave the body i will point tho the original memory address even if you assigned it a new value inside the body.
similarly for f3. you pass a reference so changes to the value of i inside the body are done to the original object so will be there when you returned from the function. difference to f2 is that you cannot pass NULLs as references - there always needs to be an object and also you need not de-reference a ref with the -> operator.
The const versions of the functions basically restrict the user (programmer) what she can do with a parameter inside the body of the function : read - only.

This is by no means a comprehensive answer, just something to get you started. Takes a while til you get your head round it...

Hope that helps and answers your question.

f2 passes the address of the object/value. Changes to the value itself that happen inside the body of f2 are done on the object that i points to. changes to i (the pointer) are not reflected, so when you leave the body i will point tho the original memory address even if you assigned it a new value inside the body.

You should also mention, the pointer value is copied in the function call. Many a rookie, think that pointers are handled special in function calls, they are variables that hold memory locations and the value that the pointer holds is copied in a function call.

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.