Not printing string from struct in C -


having problem print strings structs in c...

typedef struct box{     char *ref_or_sys; int num, x, y, w, h, o; }box;  sscanf(str, "%s %d %d %d %d %d %d", &label, &refnum, &x, &y, &w, &h, &o); box detect = {label, refnum, x, y, w, h, o}; printf("\nlabel %s\n", detect.ref_or_sys); //prints out string correctly                                               //(either word ref or sys) return detect; 

when structure passed structure, displayed right except string..

void printbox(box detect){ printf("type: %s    ref: %d    x: %d    y: %d    w: %d    h: %d    o:%d\n",   detect.ref_or_sys, detect.num, detect.x,   detect.y, detect.w, detect.h, detect.o); 

}

am missing simple? ref_or_sys prints out ??_?

use strdup() (usually available, if not use malloc()) copy string read label sscanf():

detect.ref_or_sys = strdup(label); 

as when function returns label out of scope , ref_or_sys dangling pointer. remember free() when no longer required.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -