Reputation: 81
my scanf statement is giving seg fault. could you please tell me why?
typedef struct message1
{
int call_id1;
int lac;
long int attribute;
int conn_id;
struct message1 *next1;
}M1;
M1 *last=NULL;
main()
{
printf("\nEnter Call Id\t");
scanf("%d",&last->call_id1);
}
Upvotes: 0
Views: 76
Reputation: 12688
You haven't allocated memory for struct M1
and trying to access its memory contents which is the reason for segmentation fault,
Allocate memory dynamically as, M1 *last = malloc(sizeof(M1));
Upvotes: 1
Reputation: 35458
Obviously ... it is not initialized to a proper value:
M1 *last=NULL;
should be
M1 *last= (M1*)malloc(sizeof(M1));
Upvotes: 1
Reputation: 2728
Because you initialized your structure pointer with NULL.
M1 last;
is enough.
No need to give any pointer. If you wish to give it as pointer then use malloc
M1 *last;
last = malloc(sizeof(M1));
Upvotes: 2
Reputation: 1420
your trying to read into a struct which is not allocated memory
add
M1 *last = malloc(sizeof(struct message1)); // in global space
or
last =malloc(sizeof(struct message1)); // in main function
Upvotes: 2