Below is the code I am using to read and print struct array values - I am getting the error when reading string:
#include <stdio.h>
#include <stdlib.h>
struct addressbook{
char *fname;
char *lname;
char *num;
char *email;
};
int main() {
struct addressbook addr[100];
int add=0,m=0;
while (m<3)
{
printf("1. Show All Entries\n");
printf("2. Add Entry\n");
printf("3. Quit\n");
scanf("%d",&m);
if (m==1)
{
int i;
for (i=0; i<add;i++)
{
printf("FName: %s , LName: %s , Number: %s , Email: %s \n",&addr[i].fname, &addr[i].lname,&addr[i].num,&addr[i].email);
}
}
else if (m==2)
{
if (add<101)
{
struct addressbook a;
printf("Enter First Name: ");
scanf(" %s", &a.fname);
printf("Enter last Name: ");
scanf(" %s", &a.lname);
printf("Enter Contact Number: ");
scanf(" %s", &a.num);
printf("Enter Email: ");
scanf(" %s", &a.email);
addr[add] = a;
add=add+1;
}
else{printf("100 limit reached");}
}
else if (m=3)
{
m=3;
}
else
{
m=0;
printf("Invalid option");
}
}
}
This is a basic program - but it is getting closed with unknow error.

if the length of string enters is some just 3 chars then there is no error. Could you please correct me where I have gone wrong.
Tried the below code too yet not working
printf("Enter First Name: ");
scanf(" %s", &addr[add].fname);
printf("Enter last Name: ");
scanf(" %s", &addr[add].lname);
printf("Enter Contact Number: ");
scanf(" %s", &addr[add].num);
printf("Enter Email: ");
scanf(" %s", &addr[add].email);
add=add+1;
a.fnameis a pointer, so this tries to read a string into a pointer?scanf.scanfdoes not allocate memory for you. You need to give it valid buffers to write into.else if (m=3)- it's actually an assignment which will always be true.