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?
A pointers question.
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.
Quote:Original post by Lawtonfogleit won't compile on this line:
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?
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 SteveQuote:Original post by Lawtonfogleit won't compile on this line:
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?
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.
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 Lawtonfogleint *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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement