Confusion About Pointers and References in C++

Confused with references on pointers in C++

After this declarations

int& ref1 = *ptr;
int& ref2 = val2;

ref1 refers to val1 and ref2 refers to val2. After the declarations the references can not be changed to refer to other variables.

Pay attention to that the reference ref1 does not refer to the pointer ptr. It refers the variable val1 due to dereferencing the pointer that points to the variable val1.

So these statements

std::cout << ref1 << " ";
std::cout << ref2 << " ";

will output

121 234

In this statement

*ptr = 456;

the value of the variable val1 is changed to 456 through using the pointer ptr.

After that the value of the pointer ptr was changed to store the address of the variable val2

ptr = &val2;

and the value of the variable val2 was changed to 678 through using the pointer

*ptr = 678;

So these statements

std::cout << ref1 << " ";
std::cout << ref2 << "\n";

now will output

456 678

That is they output values of the variables to which the references refer to.

In this program the same one pointer was used to change values of two different objects due to reassignment of the pointer with addresses of the objects.

Confusion about pointers and references in C++

In the first example, std::swap is called, because of your using namespace std.
The second example is exactly the same as the first one, so you might have no using.

Anyway, if you rename your function to my_swap or something like that (and change every occurence), then the first code shouldn't work, as expected. Or, remove the using namespace std and call std::cin and std::cout explicitly. I would recommend the second option.

Confusion over C++ pointer and reference topic

void foo(int &x)

passes a reference to an integer. This is an input/output parameter and can be used like a regular integer in the function. Value gets passed back to the caller.


void food(int *x)

passes a pointer to an integer. This is an input/output parameter but it's used like a pointer and has to be dereferenced (e.g. *x = 100;). You also need to check that it's not null.


void foo(int **x)

passes a pointer to a pointer to an integer. This is an input/output parameter of type integer pointer. Use this if you want to change the value of an integer point (e.g. *x = &m_myInt;).


void foo(int *&x)

passes a reference to a pointer to an integer. Like above but no need to dereference the pointer variable (e.g. x = &m_myInt;).


Hope that makes sense. I would recommend using typedefs to simplify the use of pointers and reference symbols.

Pointer confusion in C programming language

. operator has higher precedence than unary &. &d1.am is equivalent to &(d1.am) while ptd1.am is equivalent to (&d1).am, that said &d1.am != (&d1).am.

Confusion about references and pointers

This:

Buf& operator=( const Buf & );

returns a reference to a Buf object, not an address of a Buf object. So when the method returns *this, the caller gets a reference to the object.

This:
Buf* operator=( const Buf * );

returns a pointer to a Buf, so the corresponding function would indeed return this.

Note that here:

Buf& b = <some code returning Buf&>;

b is a reference to Buf, not an address. On the other hand,

Buf c = ...
Buf* pBuf = &c;

&c in the code above is the address of c and can be used to initialize pBuf, which is a pointer to Buf. So the * and & can have different meanings depending on the context.

confusion in passing a pointer to function in C

  1. right after I call the swap function, this happens
fa = pa // fa points to what pa points to which means a
fb = pb // fb points to what pb points to which means b

If you mean after entering swap then yes. That is correct. Parameters are passed as a copy, which means inside that function fa and fb get a copy of pa and pb from the caller.


  1. we swap values of fa and fb, which in turn swap values of a and b, since pa and pb points to a and b they also point to these new values.

No.fa and fb are not touched at all. They are dereferenced. This means, the address where they point to, is affected. You can use printf to print the content of those pointer variables. They will not change.
Instead the content of a and b are changed.

BTW: Your printf statements don't make much sense. You should print both before and after the function call to see any effect.



Related Topics



Leave a reply



Submit