How do you know to what a pointer is pointing to ?

Recommended Answers

All 7 Replies

Dereference it?

:|
umm..you mean the value?
for value, dereference it. *variable gives the value.

i mean the variable name to which it is pointing ?? is it possible ?


I have a struct, it contains two data members and a pointer to char.


I created two objects of the struct and initialized all the members of the first one,
then I initialized the second one with the first


like b = a;

now whatever I change in the pointer of a, also gets changed in b. So they both are pointing to the same thing, is there anyway I can get the address of to what they are pointing ?

of course the pointers in the structs point to the same thing. That is because you do a shallow copy. If you want the two char pointers inside the structs point to diffrent strings you need to do a deep copy ( copy the content pointed by the pointer in a and make the pointer in b point to the new content ).

>>now whatever I change in the pointer of a, also gets changed in b. So they both are pointing to the same thing, is there anyway I can get the address of to what they are pointing ?

From what i have understood, i would like to say the following :
A pointer actually stores an address of the variable it points to. So you may just retrieve the address of the variable (a character variable in your case) from the pointer value.

Say the pointer is char* ptr. Then ptr gives the address of to what it points to.

If i have misunderstood your question, however, you may poke me. :)

>i mean the variable name to which it is pointing ?? is it possible ?
It's possible, but not without some extra manual framework. But my question to you is, what if the pointer isn't pointing to the address of a variable? For example:

char *p = new char[10];

There's no variable involved here, the address refers to anonymous dynamically allocated memory.

>is there anyway I can get the address of to what they are pointing ?
The value of a pointer is the address that it points to.

You can compare the pointer with the location in the memory of the structures (using &)

if pointer == &struct ==> the pointer points to that struct.

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.