I have declared a structure in my .h file, as such:
struct node{
char* string;
}
I intend this to have created a structure, example, with one member, a character pointer named string.
Now, I figure out how long the string is and malloc an array of appropriate size, taking the input with that.
char* test;
test = (char*) malloc( n * sizeof(char) );
Insofar as I am aware, this has created character pointer test, and has assigned it to point at the head of the array I just malloc’d. I then proceed to assign each array slot to a character that the user has entered, and I read it back out – this all compiles and works appropriately. My problem comes when I try to assign this character pointer to the character pointer in a structure node passed in to this structure, as:
int f1( struct node* new ){
So I try to assign the pointer in the structure to the value of the pointer to the array, like so:
new->string = test;
But I segfault.
To me, this seems like I am assigning a char* to something that expects a char* so this should be working fine…I’m probably missing something stupid, but does anyone have a direction to point me in? Thanks much!
Check your caller of
f1()and make sure your parameter is valid.This will work:
As will this:
This will NOT work