Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'->' operator in C and how it's used

EDIT - updated with 2-star pointer for more relevancy

Say I had a two-star pointer to a structure that also contained a pointer.

typedef struct{
    nodeT *ptr;
}nodeT; 

nodeT example; 
nodeT *test1 = &example;
nodeT *test = &test1; 

If I wanted the address of the pointer in the structure, what would the syntax be? I tried this:

&(*test->ptr)

Where it was a thought that the parethesis would reduce to the actual pointer, after which the & operator is used to return the address.

Rather, I found this to be the correct syntax by trial and error:

&(*test)->ptr;

Further, I was confused why the below syntax does not work to even dereference the test pointer to the structure pointer:

*test->ptr;

It was my experience that without the parenthesis around *test the compiler returned a statement informing me I was attempting to access something not a part of a structure or union.

It must have something to do with the priority assigned to the various format elements that I am not fully aware.

Any ideas?

like image 236
sherrellbc Avatar asked Nov 25 '25 03:11

sherrellbc


1 Answers

test->ptr is the same that (*test).ptr

So, I think you want &(test->ptr) (I am not sure of which operator has more priority).

like image 103
SJuan76 Avatar answered Nov 26 '25 15:11

SJuan76



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!