Link to home
Start Free TrialLog in
Avatar of akohan
akohan

asked on

pointer/struct question.

Hello group,



I was looking at the following code and one thing I don't know how to explain to myself is why the object derived frm the structure works as a pointer?
because the ptrs is not declared as a pointer but as an instance of the struct.

I haven't done C programming for a long time so I'm doing a fast review.

Thanks for your help.
ak



      #include <stdio.h>
      main()   /* Illustrating structures containing pointers */
      {
            struct  int_pointers {  int  *ptr1, *ptr2;  };
            struct int_pointers  ptrs;
            int  i1 = 154, i2;

            ptrs.ptr1 =  &i1;
            ptrs.ptr2 =  &i2;
            *ptrs.ptr2 =  -97;
            printf("i1 = %d, *ptrs.ptr1 = %d\n", i1, *ptrs.ptr1);
            printf("i2 = %d, *ptrs.ptr2 = %d\n", i2, *ptrs.ptr2);
      }
SOLUTION
Avatar of Anthony2000
Anthony2000
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
*ptrs.ptr1 is *(ptrs.ptr1) you declared the ptr2 member of the int_pointers stricture to be a pointer to int
 int  *ptr1, *ptr2;
Avatar of akohan
akohan

ASKER


oh I see! so the ptrs is not the pointer but ptr1 and ptr2 are the pointers and ptrs is used just to access thsoe members. Right?

Thanks,
ak
ptrs is a struct, as you declared
prts.ptr1 is a member of the struct
Avatar of akohan

ASKER


Now , I understand it. I was thinking that * could only be behind a variable defined as pointer. Now, I know that it could be behind a struct as well.

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of akohan

ASKER



Thank you so much for your response and time. Just as the last question do you know by chance a very good/free online C/C++ tutorial ?



Regards,
ak
>> I was thinking that * could only be behind a variable defined as pointer. Now, I know that it could be behind a struct as well.

Jusr realize that ptrs.ptr2 IS a pointer. Look at it as one identifier (the ptr2 member of the ptrs struct) that has an int* type, and can be dereferenced.


>> Just as the last question do you know by chance a very good/free online C/C++ tutorial ?

I like this one :

        http://www.cplusplus.com/doc/tutorial/
Avatar of akohan

ASKER



Thank you so much.

Regards,
ak
Avatar of akohan

ASKER



Thanks Ozo.