Skip to content

Instantly share code, notes, and snippets.

@StevenJL
Last active May 27, 2017 11:08
Show Gist options
  • Save StevenJL/a36182b4c41eb9901cf7203af79b676d to your computer and use it in GitHub Desktop.
Save StevenJL/a36182b4c41eb9901cf7203af79b676d to your computer and use it in GitHub Desktop.
c_note_pointer_strings
// You can create a string as a character array
char some_text[] = "Here is some text";
// However, you cannot first instantiate a character array
// and then assign to a string.
char some_text[20];
some_text = "Here is some text"; // WRONG
// If you want to instantiate a variable first, and then
// assign to a string, you must use a pointer
char *some_text_ptr;
some_text_ptr = "Here is some text"
// To create an array of strings, you must use an array
// of char pointers. Note you this is preferred over
// an array of character arrays.
char *names[] = { "Steve", "Rachel", "Anthony", "Pootie" };
// To print of the second name:
printf("%s", names[1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment