0

Hi,
I am comfortable in c.But i find errors here in my code.I know its related to that structures cant be accessed outside main().Need s use of pointer.How to do?.Help me soon.
A revised code will be of help.

#include <stdio.h>
#include <string.h>
struct phonebook
{ 
char name[100]; 
long int phone; 
}myphonebook[5];
int addtobook(void);
int printbook(void);
int i;
int main()
{
addtobook();
printbook();
return 1;
}
int addtobook()
{
for(i=0;i<5;i++)
{
printf("\n Enter name of friend:-\n");
scanf("%s",&myphonebook.name );
printf("\n Enter phone number of friend:-\n");
scanf("%ld",&myphonebook.phone );
}
return 1;
}
int printbook()
{
for(i=0;i<5;i++)
{
printf("\n Friend name %s phone number is %ld \n",myphonebook.name,myphonebook.phone);
}
return 1;
}
2
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by John A
0

>scanf("%s",&myphonebook.name );

How about scanf("%s", myphonebook[i].name); You don't need to use the dereference operator since the array name acts as a pointer to the beginning of the data.

0

Removing defreference operator doesnot help.Same error of
structure required on left side of .or . in function addtobook()

1

That's because you're not identifying which element of the myphonebook array you want to use. If you had read my last post more carefully, you would have noticed that I added [i] after 'myphonebook' -- this way, in the loop you're actually accessing the i'th item.

Votes + Comments
[S]he is [too] comfortable in C. Rep for the effort.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.