I am new in C, and i wrote the following code
#include <stdlib.h>
#include<stdio.h>
typedef struct
{
int name1;
}check1;
typedef struct
{
int name2;
}check2;
int main()
{
check1 *test1;
check2 *test2;
test1->name1=1;
test2->name2=2;
return 0;
}
When I am executing it, it is giving me an error:
$ gcc test1.c
$ ./a.out
Memory fault
In gdb:-
Program received signal SIGSEGV, Segmentation fault.
0x000000000040045e in main ()
What could be the reason???
Thanks.
You have declared two pointers, but you haven’t allocated any memory for them to point to. The pointers are pointing to invalid memory.
Try this: