Jump to content
  • Advertisement
Sign in to follow this  
Lawtonfogle

A pointers question.

This topic is 3832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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 = &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?
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;

Share this post


Link to post
Share on other sites
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 = &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?
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
** 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;

Share this post


Link to post
Share on other sites
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 int
k = &j; // assigns an rvalue of type int* to an lvalue of type int* -- ok
i = &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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!