c - Error while initializing the structure values -


i have started using pointers.so please bear me if looks silly not able find reason. have structure

typedef struct intermediatenode {     int key;     char *value;     int height;     struct node *next[skiplist_max_height]; } node; 

and wand create new node using below function

 node *create_node(int key, char * val, int h)  {         node *newnode;         newnode=malloc(sizeof(node));         newnode->height=h;         newnode->key=key;         printf("till here %s \n",val);         printf("till here %d \n",newnode->height);         printf("till here %d \n",newnode->key);         strcpy(newnode->value,val);         printf("till here %s \n",newnode->value);         return newnode;  } 

but getting segmentation fault @ "strcpy(newnode->value,val);" can please me that.thanks lot

you allocated memory node, not string in value. strcpy function copy bytes not allocate memory. assumes you've arranged that. in pinch, can allocate , copy string strdup:

newnode->value = strdup(val); 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -