Reputation: 39
I'm learning the C language. Can anybody help me to understand the following result:
int main()
{
struct xx
{
int x;
char name[];
};
struct xx *s;
printf("%d",s->x);
printf("%s",s->name);
return 0;
}
output:- Segmentation fault
I wrote another code which is:
struct Foo
{
char *pName;
};
int main()
{
struct Foo *obj = malloc(sizeof(struct Foo));
strcpy(obj->pName,"Your Name");
printf("%s", obj->pName);
return 0;
}
output : Segmentation Fault (core dumped);
Why am I getting segmentation fault? What's wrong with the code? What is the meaning of core dumped ?
Upvotes: 0
Views: 489
Reputation: 33
The pointer in the struct is not initiallized when being used(which is called wild pointer). Even though the pointer has been initialized, it shoud point to an effective memory area.
When learning C language, memory operation is the top thing.
Upvotes: 0
Reputation: 89
nice to see someone working on C language...
Ok,,...In Your first code you haven't allocated memory for ss variable and you are accessing value of it so it generates Segmentation fault..
In Second,,,pName is a character pointer..so you can't use it in strcpy() function...
Upvotes: 0
Reputation: 57
First you need to allocate size of your variable s because your compiler donot understand the size of memory it should allocate to variable. Thats why you need to allocate memory as: S=(struct xx*)malloc (sizeof(struct xx));
Upvotes: 0
Reputation: 7
You have declared the pointer variable but have not allocated memory to it
To do add after strict xx *S;
S=(struct xx*) malloc(sizeof(strict xx));
And also declare first size of char name[30]
;
Upvotes: 0
Reputation:
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct xx
{
int x;
char name[25]; /* mention size of array */
};
struct xx *s;
s=(struct xx *)malloc(sizeof(struct xx)); /* allocate the size for pointer variable */
printf("Enter the value\n");
scanf("%d",&s->x); /*get the value */
printf("Enter the name\n");
scanf("%s",s->name);
printf("%d\n",s->x); /*get the value */
printf("%s\n",s->name);
return 0;
}
Upvotes: 0
Reputation: 49523
main()
should have the standard form (int main(void)
) and return an int
values
obj
pName
within struct Foo
Upvotes: 5
Reputation: 961
In example 1 you declared a pointer but you have not initialized it. Therefore the pointer points to an unknown location.
In example 2 obj->pname for the same reason points to An Unknown portion of the memory (it was not initialized) and therefore trying to acess it inside strcpy will crash the program.
What you should have done: you should have allocated some memory for the pointers to make them point to memory that belongs to you and not some random (illegal?) portion of memory
Upvotes: 0
Reputation: 3381
A pointer is declared for the structure, but the pointer is never initialized, no structure is never created. s
is pointing to some random memory space, from whic you attempt to read.
In the second case, you have a pointer pName
which has never been allocated memory. It is also pointing to a random memory space. You are string copying from the string literal and writing a random memory location.
Upvotes: 1