I am trying to modify a field inside a struct. I have no trouble doing this with other types (i.e. int
, float
etc.) but char *
is giving me problems. I think I have to do something like:
typedef struct{
char *string_field;
} struct_name;
struct_name *struct_name1;
struct_name1 = (struct_name *) malloc(sizeof(struct_name));
strcpy(struct_name1->string_field, new_string);
printf("New string: %s\n", struct_name1->string_field);
But this gives me a segmentation fault. What reason do you think I would get this problem for? Initially, I thought maybe the char *string_field
was not big enough to copy to, but I changed the size of it manually to be of size 100 (more than enough) and I still get this problem.