codeQuantum
asked on
Understanding pointer indirection
Hi,
Can somebody explain what indirection really means?
1) I don't understand why they call it indirection... what is the source of this name?
2) as far as i can understand, indirecting a pointer is changing the value of the variable it points to. Is this true, or is there more to indirection?
3) supose I create a pointer using "int *ptr;"... is it possible to indirect the pointer, even if it does not point to a variable?
Thank!
Can somebody explain what indirection really means?
1) I don't understand why they call it indirection... what is the source of this name?
2) as far as i can understand, indirecting a pointer is changing the value of the variable it points to. Is this true, or is there more to indirection?
3) supose I create a pointer using "int *ptr;"... is it possible to indirect the pointer, even if it does not point to a variable?
Thank!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So I guess I need to use
int *p = new int;
Thanks infinity08, you have been very helpful. :)
int *p = new int;
Thanks infinity08, you have been very helpful. :)
>> So I guess I need to use
>>
>> int *p = new int;
Yep. Twice - once for A and once for B ;)
>>
>> int *p = new int;
Yep. Twice - once for A and once for B ;)
ASKER
One last detail, if I wanted to break that operation on two lines, would that work :
int *p;
*p = new int;
?
I am sometimes confused by the indirection operator * that seem to be used in 2 different ways between declaration and indirection.
int *p;
*p = new int;
?
I am sometimes confused by the indirection operator * that seem to be used in 2 different ways between declaration and indirection.
>> int *p;
>> *p = new int;
Make that :
int *p;
p = new int;
and it will work. The type of p is int* (pointer-to-int). *p is the indirection of p, so it actually refers to the int value.
>> that seem to be used in 2 different ways between declaration and indirection.
This is a declaration :
int *p;
The * here is NOT the indirection operator. It is part of the type of p (int* or pointer-to-int)
>> *p = new int;
Make that :
int *p;
p = new int;
and it will work. The type of p is int* (pointer-to-int). *p is the indirection of p, so it actually refers to the int value.
>> that seem to be used in 2 different ways between declaration and indirection.
This is a declaration :
int *p;
The * here is NOT the indirection operator. It is part of the type of p (int* or pointer-to-int)
ASKER
One last thing (sorry, I promess this is the last one!)
I read that arrays are basically pointers (to their first element). If it is so, why can't I do this :
int untab[] = {5, 19, 14, 8, -10, 99};
int *unptr = new int;
untab = unptr; // returns an error
Both are int pointers, so what's the problem?
I understand that I would lose information doing that. But since C++ is not as protective as Java, I don't see why it does not compile...
I read that arrays are basically pointers (to their first element). If it is so, why can't I do this :
int untab[] = {5, 19, 14, 8, -10, 99};
int *unptr = new int;
untab = unptr; // returns an error
Both are int pointers, so what's the problem?
I understand that I would lose information doing that. But since C++ is not as protective as Java, I don't see why it does not compile...
>> I read that arrays are basically pointers
That's not true. Arrays will degenerate to a pointer in certain contexts, so you can do this for example :
int untab[] = {5, 19, 14, 8, -10, 99};
int *unptr = untab; // <--- now unptr will point to the first element in the untab array (5)
However, that doesn't work the other way around. You cannot re-assign an array identifier, and let it point to different memory.
That's not true. Arrays will degenerate to a pointer in certain contexts, so you can do this for example :
int untab[] = {5, 19, 14, 8, -10, 99};
int *unptr = untab; // <--- now unptr will point to the first element in the untab array (5)
However, that doesn't work the other way around. You cannot re-assign an array identifier, and let it point to different memory.
ASKER
I was asking that last question because I am doing an exercise right now that is related.
It says : >> create 2 pointers... A and B. Then make it so each of those pointers is assigned a memory zone that can hold an int. <<
So if I understand your answer correctly, I have to create two int variable and associate the pointers to them, or use new int(123) in order to solve the problem? or is there another way to do this?
(in other words, is there an elegant solution that does not involve creating unnecessary int variables, or using an arbitrary number inside the new int(x)?)