结构数组中的错误

时间:2015-03-20 15:01:13

标签: c arrays pointers struct

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

#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: 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);
                                                                                  ^

2 个答案:

答案 0 :(得分:3)

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

中的

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 

答案 1 :(得分:2)

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