I wrote a program using array of structures iam accessing the elements using pointers but iam getting error
#include<stdio.h>
struct book
{
char name[30]
int sold;
int left;
};
void change(struct book *p);
int main()
{
char ch;
int i;
struct book program[10];
printf("please enter the information \n ");
for(i=0;i<10;i++)
{
printf(" enter the name of author \n");
scanf("%s",(program+i).name); // &a[0] is equivalent to a+i
printf(" enter the number of book sold \n ");
scanf("%d"(program+i).sold);
printf("enter the number of book left \n");
scanf("%d",(program+i).left);
}
printf("the following is the information available \n");
for(i=0;i<10;i++)
{
printf("%s %d %d",*(program+i).name,*(program+i).sold,*(program+i).left);
}
printf("Do you want to change any data \n press y if yes and any key for no \n");
ch=getchar();
if((ch=='y')||(ch=='Y'))
{
change(program);
printf("the following is the information available \n");
for(i=0;i<10;i++)
{
printf("%s %d %d",*(program+i)->name,*(program+i)->sold,*(program+i))->left);
}
}
else
{
return 0;
}
return 0;
}
void change(struct book *p)
{
int i;
for(i=0;i<10;i++)
{
printf("enter your data for book %s \n",(p+i)->name);
printf(" enter the number of book sold \n ");
scanf("%d",(p+i).old);
printf("enter the number of book left \n");
scanf("%d",(p+i).left);
}
}
the error iam getting is pretty big one
fncstr.c:18:25: error: request for member ‘name’ in something not a structure or union
scanf("%s",(program+i).name);
^
fncstr.c:20:13: error: called object is not a function or function pointer
scanf("%d"(program+i).sold);
^
fncstr.c:22:25: error: request for member ‘left’ in something not a structure or union
scanf("%d",(program+i).left);
^
fncstr.c:28:36: error: request for member ‘name’ in something not a structure or union
printf("%s %d %d",*(program+i).name,*(program+i).sold,*(program+i).left);
^
fncstr.c:28:54: error: request for member ‘sold’ in something not a structure or union
printf("%s %d %d",*(program+i).name,*(program+i).sold,*(program+i).left);
^
fncstr.c:28:72: error: request for member ‘left’ in something not a structure or union
printf("%s %d %d",*(program+i).name,*(program+i).sold,*(program+i).left);
^
fncstr.c:38:37: error: ‘struct book’ has no member named ‘name’
printf("%s %d %d",*(program+i)->name,*(program+i)->sold,*(program+i))->left);
^
fncstr.c:38:56: error: ‘struct book’ has no member named ‘sold’
printf("%s %d %d",*(program+i)->name,*(program+i)->sold,*(program+i))->left);
^
fncstr.c:38:82: error: expected ‘;’ before ‘)’ token
printf("%s %d %d",*(program+i)->name,*(program+i)->sold,*(program+i))- >left);
^
program[i]instead of error prone and obscure pointer arithmetic ?[]operator will do pointer arithmetic as well but the code will be way more readable.