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);
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
prts.ptr1 is a member of the struct
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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/
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/
ASKER
Thank you so much.
Regards,
ak
ASKER
Thanks Ozo.
int *ptr1, *ptr2;