ways to define a string in C:
Code:
char foobar[] = "Hello";
char foobar[6] = "Hello"; // note 6 not 5, last is for null terminator
char *foobar = "Hello"; // set at initialization - is ok
char *foobar;
foobar = (char *) malloc (6); // allocate space
strcpy (foobar, "Hello"); // after initialization you must use string functions to change strings
free (foobar); // anything malloc()ed must be free()d after you're done with it
the first and second examples above can also have strcpy() used on them, but not outside of the space you have to work with! otherwise you'll be reaching into memory that's not yours.
the third example above should not be modified after initalization (thanks hammer).
Code:
char *foobar;
foobar = (char *) malloc (strlen ("Hello") + 1); // strlen returns the size of the inputted string
strcpy (foobar, "Hello");
free (foobar); // always free after your done using it, never free before
god i hope that's all right (it looks right to me) otherwise i'm gonna feel sofa king dumb