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

Recommended Answers

All 3 Replies

>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.

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

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.

commented: [S]he is [too] comfortable in C. Rep for the effort. +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.