Last active
May 27, 2017 11:08
-
-
Save StevenJL/a36182b4c41eb9901cf7203af79b676d to your computer and use it in GitHub Desktop.
c_note_pointer_strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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