i just started learning pointers in c. right now i am trying to understand pointers in structures. i don't think i am interpreting them right. the following is the example given in textbook for pointers in structures.
#include<stdio.h>
int main(void)
{
struct intPtrs
{
int *p1;
int *p2;
};
struct intPtrs pointers;
int i1=100,i2;
pointers.p1=&i1;
pointers.p2=&i2;
*pointers.p2=-97; //here
printf ("i1 = %i, *pointers.p1 = %i\n", i1, *pointers.p1);
printf ("i2 = %i, *pointers.p2 = %i\n", i2, *pointers.p2);
return 0;
}
my question in the above code is since pointers
is declared as a type struct intPtrs
, shouldn't the above code look like pointers.(*p1) = -97;
. Also, i know it's wrong since it's giving me an error. but i am not able to understand why it's *pointers.p1
.
The above question might sound like naive, but please i am trying my best to understand the concepts by myself. so, any help in trying to explain it as simple as possible would be really helpful to me.