user2257769
user2257769

Reputation: 39

C language structure

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

Answers (8)

schnauzer
schnauzer

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

Naman_DT98
Naman_DT98

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

user6784306
user6784306

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

shriramchoubey
shriramchoubey

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

user3775089
user3775089

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

Mike
Mike

Reputation: 49523

  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.

Upvotes: 5

Mppl
Mppl

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

Tevo D
Tevo D

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

Related Questions