-
crashing code
Why doesn't this code work? Is it because I have to allocate memory for char* tool_tip_text and how would I do that?
I've tried allocating memory using this, but it doesn't help either.
button->tool_tip_text = malloc(sizeof(char) * strlen(text)+1)
Code:
typedef struct {
char* tool_tip_text;
}BUTTON;
int create_button(BUTTON* button, char* text)
{
printf("%d",strlen(text));
strcpy(button->tool_tip_text,text);
return 0;
}
create_button(&my_button[0],"New Level");
-
After including the proper header file that code does actually fixes this problem.
I have one more question. Why doesn't this error checking work, unless I merge these two lines?
button->tool_tip_text = malloc(sizeof(char) * (strlen(text)+1) );
if(button->tool_tip_text == NULL)
return -1;
-
button->tool_tip_text = malloc(sizeof(char) * (strlen(text)+1) );
if(button->tool_tip_text == NULL)
return -1;
if((button->tool_tip_text = malloc(sizeof(char) * strlen(text)+1)) == NULL)
return -1;
Out these two samples of code the second one does what I want it to do. Why couldn't first line of code work?