#include <stdio.h>
#include <stdlib.h>
typedef struct foo {
char* text;
int num;
} foo;
int main(void) {
foo* a = malloc(sizeof(foo));
a->text = "Something";
a->num = 4;
printf("%s %d\n", a->text, a->num);
foo b;
b.text = "Something";
b.num = 4;
foo* c = &b;
printf("%s %d", c->text, c->num);
return 0;
}
Both print the exact same thing. The only difference between foo* a
and foo* c
is where each one points to. Which one should be preferred? I usually see malloc()
more but I don't understand why.