1

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

4
  • Why not use program[i] instead of error prone and obscure pointer arithmetic ? Commented Mar 20, 2015 at 15:05
  • pointer airthematic is fast isn't it? Commented Mar 20, 2015 at 15:08
  • It is not about speed, under the hood the [] operator will do pointer arithmetic as well but the code will be way more readable. Commented Mar 20, 2015 at 15:30
  • ok i will implement it. Commented Mar 20, 2015 at 15:32

2 Answers 2

3

You have to pass the address of the variables to receive the input.

in main

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

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

in change

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

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

Why not using the notation p[i] and program[i] ? This would make your program more readable.

Another thing is to put a limit for the input of the book name to avoid a buffer overflow:

scanf("%29s",(program+i).name); // &a[0] is equivalent to a+i 
Sign up to request clarification or add additional context in comments.

Comments

2
        scanf("%s", (program+i).name);

Is not right. Type of (program+i) is struct book*. Yes, a pointer. You can't use . to access members from a pointer. You need to use one of the following:

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

For the other members, you need to use:

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

4 Comments

this gives me error 'fncstr.c:18:26: error: ‘struct book’ has no member named ‘name’ scanf("%s", program[i].name); '
@kapilsharma, from your error messages, it's clear that the program needs to be fixed at a few more places. Take a look working code at ideone.com/lSbKHg. I changed the for loop limit from 10 to 0 to avoid having to provide input. But the program contains all the compiler error fixes.
@kapilsharma, that error is caused by a missing ; after name in your struct.
yes sir you are right i checked it just now ,thanks for your time

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.