A pointers question.

Started by
4 comments, last by Bregma 15 years, 10 months ago
This one kinda hit me last night, and I'm not sure about it, and it could possibly vary based on compiler. Anyways, here is the situation.

int *i;
int *k;
int j;
k = &j
i = &k
if I had: int **i it would be fine, but I'm wondering since i is a pointer who was passed a pointer if this would work. Would it only throw an error if I used **i in my code to try to return an int since it would think that *i was an int, or would it be happy and actually return to me j?
If history is to change, let it change. If the world is to be destroyed, so be it. If my fate is to die, I must simply laugh.- Magus
Advertisement
Quote:Original post by Lawtonfogle
This one kinda hit me last night, and I'm not sure about it, and it could possibly vary based on compiler. Anyways, here is the situation.

int *i;int *k;int j;k = &ji = &k


if I had: int **i it would be fine, but I'm wondering since i is a pointer who was passed a pointer if this would work.

Would it only throw an error if I used **i in my code to try to return an int since it would think that *i was an int, or would it be happy and actually return to me j?
it won't compile on this line:
i = &k
Because you're trying to assign the address of a pointer (a pointer to a pointer) to a pointer. You'd have to declare i as:
int** i;
Quote:Original post by Evil Steve
Quote:Original post by Lawtonfogle
This one kinda hit me last night, and I'm not sure about it, and it could possibly vary based on compiler. Anyways, here is the situation.

int *i;int *k;int j;k = &ji = &k


if I had: int **i it would be fine, but I'm wondering since i is a pointer who was passed a pointer if this would work.

Would it only throw an error if I used **i in my code to try to return an int since it would think that *i was an int, or would it be happy and actually return to me j?
it won't compile on this line:
i = &k
Because you're trying to assign the address of a pointer (a pointer to a pointer) to a pointer. You'd have to declare i as:
int** i;


is it int** i or int **i... or don't both work...?

Anyways, I know that using ** will make it work, I was just wondering if the compiler keeps track of if it is a * or a ** or more than that.

Thanks.
If history is to change, let it change. If the world is to be destroyed, so be it. If my fate is to die, I must simply laugh.- Magus
Quote:Original post by Lawtonfogle
is it int** i or int **i... or don't both work...?

Anyways, I know that using ** will make it work, I was just wondering if the compiler keeps track of if it is a * or a ** or more than that.

Thanks.


int** i, int ** i and int **i are the same thing.

The compiler keeps track of the *s, because int* and int** are different types.
** is nothing special, it's just two stars (as opposed to ++ for example which is not just two pluses). So you can also write
int * * i;
Quote:Original post by Lawtonfogle
int *i;  // declares i to be a variable of type int*int *k;  // declares k to be a variable of type int*int j;   // declares j to be a variable of type intk = &j  // assigns an rvalue of type int* to an lvalue of type int* -- oki = &k  // assigns an rvalue of type int** to an lvalue of type int* -- bad

if I had: int **i it would be fine, but I'm wondering since i is a pointer who was passed a pointer if this would work.

Their is no pointer type in C or C++. There is only pointer to ... Each pointer to .. type is a distinct and unrelated type. That means that pointer to int and pointer to pointer to int are unrelated types.
Quote:Would it only throw an error if I used **i in my code to try to return an int since it would think that *i was an int, or would it be happy and actually return to me j?

The expression **i when used as an rvalue would likely give you a beautiful crash. You would have to declare i to be of type int**, in which case it will do what you want.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement