0

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 ?

  • 2
    This code should not compile. If it did, your compiler is crap. – Lundin Apr 08 '13 at 14:07
  • 3
    I would highly recommend getting a good book on C reading up on it. A lot of your questions would be covered there. Check the [C tag's info page](http://stackoverflow.com/tags/c/info) for some book suggestions. – Mike Apr 08 '13 at 14:11
  • now check the code ; my complier is gcc i hope its not a crap.no book is explaining why malloc is not working in 2nd code and why first code is showing segmentation fault – user2257769 Apr 08 '13 at 16:43
  • @user2257769 - you're biggest problems at the moment still are listed in my answer (#3 and #5). Whenever you have a pointer, before you dereference it you need to assign it to some valid memory. `s` from example 1 isn't pointing to anything, you need to malloc some memory to it. `pName` doesn't have any memory so the strcpy is failing and crashing. Also you're update to the code added a new problem with `name` in example 1 – Mike Apr 08 '13 at 16:48
  • thanx mike i got the point super cool man – user2257769 Apr 08 '13 at 17:20
  • @user2257769 `malloc()` is working perfectly fine. Be humble – don't assume that core library features "don't work". It's just that you are not using them correctly. In particular, where should `p->name` point to in the second case? it's an uninitialized pointer, it doesn't point anywhere. No wonder you get a segmentation fault by trying to `strcpy()` onto it. – The Paramagnetic Croissant Nov 29 '14 at 11:40

8 Answers8

5
  1. You do not assign values inside a struct definition
  2. Assuming you are working in a hosted environment (running on a system with an OS), main() should have the standard form (int main(void)) and return an int value
  3. In example one, you didn't assign any memory to your pointer s
  4. You have a memory leak in your second example where you didn't free the memory in obj
  5. You didn't assign any memory in your second example to pName within struct Foo
  6. Core dump means something went very wrong.
Mike
  • 47,263
  • 29
  • 113
  • 177
  • (Strictly speaking, main does not need to be int main (void) if this is for an embedded system. Nor is return 0 mandatory in main(), although it is good style.) – Lundin Apr 08 '13 at 14:06
  • @Lundin - Could you elaborate on that? How does code written for an embedded system allow it to not have to follow the C standard section 5.1.2.2.1 which specifies how `main()` "shall be defined"? – Mike Apr 08 '13 at 14:08
  • 5.1.2.2.1 only specifies how main shall be defined _in a hosted environment_, it does not apply to embedded (freestanding) systems. [More info here](http://stackoverflow.com/questions/5296163/why-is-the-type-of-the-main-function-in-c-and-c-left-to-the-user-to-define/5296593#5296593). – Lundin Apr 08 '13 at 14:23
  • @Lundin - Oh, yes, I see what you are saying and agree that in a *bare metal* environment that is not mandatory. I misunderstood you, since embedded systems can still be a hosted environment. Updated accordingly. – Mike Apr 08 '13 at 14:28
  • thanks for response . i changed the code bit but the result is same – user2257769 Apr 08 '13 at 16:28
  • @user2257769 - you can make an edit to your post and *append* the new code to the bottom (please don't overwrite everything in the question). Or if the problem changed significantly you can always ask a new question. It's normally best to keep one question per post. – Mike Apr 08 '13 at 16:32
  • The code is written with proper int main and return 0 statement but there is no proper explanation why malloc is not working . and why first code shows seg fault – user2257769 Apr 08 '13 at 16:47
  • @user2257769 - what do you mean why malloc isn't working? It is working, but you have to malloc memory to ***both*** the pointer to the struct, then to any pointers *within* the structure. (see #5 in my answer) – Mike Apr 08 '13 at 16:50
1

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.

Tevo D
  • 3,351
  • 21
  • 28
0

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

Mppl
  • 941
  • 10
  • 18
0
   #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;
   }
0

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

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
Src7
  • 7
  • 5
0

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

user6784306
  • 57
  • 1
  • 1
  • 5
0

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

Naman_DT98
  • 21
  • 1
  • 4
0

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.

schnauzer
  • 33
  • 5