I'm trying to find out how can I use a pointer to access an array of struct
#include<stdio.h>
struct p
{
int x;
char y;
};
int main()
{
struct p p1[]={1,92,3,94,5,96};
struct p *ptr1=p1;
printf("size of p1 = %i\n",sizeof(p1));
//here is my question, how can I use ptr1 to access the next element of the array
//of the struct?
printf("%i %c\n",ptr1->x,ptr1->y);
int x=(sizeof(p1)/3);
if(x == sizeof(int)+sizeof(char))
printf("%d\n",ptr1->x);
else
printf("falsen\n");
return 0;
}
//here is my question, how can I use ptr1 to access the next element of the array of the struct? I tried to write this line of code but an error appeared
printf("%i %c\n",ptr1[0]->x,ptr1[1]->y);
the error was
invalid type of argument '->'(have'struct p')
do I have to declare ptr1 as an array of pointers? and if I don't know the size of the array of struct how can I declare an array of pointers,
another question here, what should be the size of the pointer? sizeof(ptr1)? I've tried this line of code
printf("size of ptr1 = %i",sizeof(ptr1));
and the answer was 4 ?!!HOW COME?
thanks
printf("%i %c\n",(ptr1+1)->x,(ptr1+1)->y);