Reputation: 271
I am trying to use array of structures to store the information of 5 books.I was reading a book that ia quite common in Indian sub-continent i.e. "Let us C" just to understand what is C so that i am uch prepare to study K&R.While i was trying to implement one of the example for array of structures, i make required changes in the example but still i am getting certain errors and i am unable to find the error.
#include<stdio.h>
//void inkfloat(); commented as i am not using any float variable
int main()
{
int i;
struct book
{
char bookname[30];
char authorname[30];
int price;
int book_id;
};
struct book b[5];
for(i=0;i<=4;i++)
{
printf("Enter bookname,authorname, price and book_id for book");
scanf("%s %s %d %d",&b[i].bookname,&b[i].authorname,&b[i].price,&b[i].book_id);
}
for(i=0;i<=4;i++)
{
printf(" %s %s %d %d \n",b[i].bookname,b[i].authorname,b[i].price,b[i].book_id);
}
return 0;
}
/*void inkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}*/
I am getting output as
Enter bookname,authorname, price and book_id for book shailendra
Enter bookname,authorname, price and book_id for book let us c
Enter bookname,authorname, price and book_id for bookEnter bookname,authorname, price and book_id for bookEnter bookname,authorname, price and book_id for bookyaswat kanetkar
s, �, -1218811592, -1216872840 l, :, -1218241152, -1218240426 u, ~, -1216874216, 0 c, �, 134513259, 0 y, , -1218653802, -1217138700
Along with that i am unable to understand the use of inkfloat variable which according to the book is used when we are using float variable inside our code and if didn't use it when using float variable a error will come"Floating points format not linked"
i had seen An array of structures and other related questions on stackoverflow, but cant resolve what error i am having in my code.
Upvotes: 0
Views: 168
Reputation: 1506
As said by the others you have 2 mistakes:
Your read code should be something like this:
for(i=0;i<5;i++)
{
printf("Enter bookname,authorname, price and book_id for book");
fgets(b[i].bookname,30,stdin);
fgets(b[i].authorname,30,stdin);
scanf("%d %d",&b[i].price,&b[i].book_id);
}
You can check the whole source on cfiddle: http://cfiddle.net/oZvbRm
Upvotes: 0
Reputation: 3143
You are not supposed to pass address of pointer in this case with scanf
. Use scanf without &. Moreover, it's not good to use scanf for reading string since when a blank space character appears it stops reading. So when you input "Let us C", here there's blank space after Let so it doesn't work. A better option is to use gets
that can be useful here. See reference documentation of gets
for more details.
I hope this helps.
Upvotes: 0
Reputation: 21435
The problem is that %s
in scanf
reads only until the first whitespace character. Thus, you cannot read let us C
using this approach because it will stop after reading let
.
Finally, the &
character is used to get the address of a variable. When reading into vectors you don't need it before the name of the vector since it has already decayed into a pointer.
Upvotes: 3
Reputation: 6606
You are overlooking warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[30]’
.Make parameter as char * in line calling scanf.
Upvotes: 0
Reputation: 17268
The array name is a pointer to the first element in the array. The address operator should not be applied to the character arrays in the scanf statement.
scanf("%s %s %d %d", b[i].bookname, b[i].authorname, &b[i].price, &b[i].book_id);
^^^ ^^^
Upvotes: 0
Reputation: 757
When you read strings in scanf you don't need to use "&" symbol.
Upvotes: 1