EmbLogic's Blog

I am getting segmentation fault in the following Pgm..please explain if u clear about structure ponter…i am facing the issue that …is it madatory to initialize struct pointer?????

#include<stdio.h>
#include<string.h>

struct logic
{
char ch[10];
char x;
int y;

char z;
//int i;
};

int main()
{
struct logic *p;
strcpy(p->ch,”hello”);
p->x=’b’;
p->y=42;
p->z=’c’;
printf(“%s\n%c\n%d\n%c\n”,p->ch,p->x,p->y,p->z);
}

 

One Response to I am getting segmentation fault in the following Pgm..please explain if u clear about structure ponter…i am facing the issue that …is it madatory to initialize struct pointer?????

  1. hemant.kumar says:

    when ever you use a structure pointer varible you need to allocate memory to the structure
    p=malloc(sizeof(struct logic));

    otherwise Segmentation fault
    do one thing use p insted of (*p) then calculate sizeof(structure )
    you will get the size of the whole structure
    but in case of pointer no memory is allocated ,malloc is mandatory here

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>