Pretty much all of the code here has one or more flaws. I'll demonstrate "working" versions.
There are several ways of doing this.
One way:
Code:
#include<stdio.h>
typedef struct
{
char format_name[64];
}
StupidRec;
int main(int argc, char** argv)
{
StupidRec* list = malloc(sizeof(StupidRec));
strcpy(list->format_name, "hello");
printf("%s\n", list->format_name);
free(list); // DON'T FORGET!
return 0;
}
Another way:
Code:
#include<stdio.h>
typedef struct
{
const char* format_name;
}
StupidRec;
int main(int argc, char** argv)
{
StupidRec list;
list.format_name = "hello"; // NOTE: CAN'T MODIFY STRING
printf("%s\n", list.format_name);
return 0;
}
A third way:
Code:
#include<stdio.h>
typedef struct
{
char* format_name;
}
StupidRec;
int main(int argc, char** argv)
{
char buf[1024];
StupidRec* list = malloc(sizeof(StupidRec));
list->format_name = buf; // SET POINTER TO A VALID STORAGE FIRST
strcpy(list->format_name, "hello"); // DANGEROUS, BUT WE KNOW "hello" DO NOT TAKE UP 1024 CHARACTERS.
printf("%s\n", list->format_name);
free(list); // DON'T FORGET!
return 0;
}
And finally, here is how to NOT do it:
Code:
#include<stdio.h>
typedef struct
{
char* format_name;
}
StupidRec;
int main(int argc, char** argv)
{
StupidRec list;
list.format_name = malloc(1024);
list.format_name = "hello"; // MEMORY LEAK. DON'T DO THIS.
printf("%s\n", list.format_name);
return 0;
}
Note how I don't use the StupidList type. It's confusing only to typedef pointers that way. Avoid it.