结构数组错误

error in array of structure

我使用结构数组编写了一个程序,我正在使用指针访问元素,但我收到错误

#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);
    }
} 

我得到的错误很大

    fncstr.c:18:25: 错误:请求成员“名称”不是结构或联合
   scanf("%s",(程序+i).name);
                         ^
fncstr.c:20:13: 错误:被调用对象不是函数或函数指针
   scanf("%d"(程序+i).sold);
             ^
fncstr.c:22:25: 错误:请求成员“left”不是结构或联合
   scanf("%d",(程序+i).left);
                         ^
fncstr.c:28:36: 错误:请求成员“名称”不是结构或联合
   printf("%s %d %d",*(program+i).name,*(program+i).sold,*(program+i).left;
                                    ^
fncstr.c:28:54: 错误:请求成员“sold”不是结构或联合
   printf("%s %d %d",*(program+i).name,*(program+i).sold,*(program+i).left;
                                                      ^
fncstr.c:28:72: 错误:请求成员“left”不是结构或联合
   printf("%s %d %d",*(program+i).name,*(program+i).sold,*(program+i).left;
                                                                        ^
fncstr.c:38:37: 错误:“struct book”没有名为“name”的成员
    printf("%s %d %d",*(program+i)->name,*(program+i)->sold,*(program+i))->left);
                                     ^
fncstr.c:38:56: 错误:“struct book”没有名为“sold”的成员
    printf("%s %d %d",*(program+i)->name,*(program+i)->sold,*(program+i))->left);
                                                        ^
fncstr.c:38:82: 错误:在 ‘)’ 标记之前需要‘;’
    printf("%s %d %d",*(program+i)->name,*(program+i)->sold,*(program+i))->left);
                                                                                  ^

您必须传递变量的地址才能接收输入。

主要

scanf("%d", &(program+i).sold);

scanf("%d", &(program+i).left);

变化中

scanf("%d", &(p+i).old);

scanf("%d", &(p+i).left);

为什么不使用符号 p[i]program[i]?这将使您的程序更具可读性。

另一件事是限制书名的输入以避免缓冲区溢出:

scanf("%29s",(program+i).name); // &a[0] is equivalent to a+i 
        scanf("%s", (program+i).name);

不对。 (program+i) 的类型是 struct book*。是的,一个指针。您不能使用 . 从指针访问成员。您需要使用以下其中一项:

        scanf("%s", program[i].name); 
        scanf("%s", (*(program+i)).name);
        scanf("%s", (program+i)->name);

对于其他成员,您需要使用:

        scanf("%d", &program[i].sold);
        scanf("%d", &program[i].left);