Last active
August 29, 2015 14:24
-
-
Save joelburton/2879db861a5dd0fa0751 to your computer and use it in GitHub Desktop.
Demonstrate C string creation & mutability
This file contains 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
/** | |
* Demonstrate types of strings & mutability & storage | |
* | |
* Joel Burton <joel@joelburton.com> | |
**/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int main (int argc, char *argv[]) { | |
// Strings as arrays of literals | |
char hi1[] = "Hello"; | |
char hi2[] = "Hello"; | |
// Strings as arrays of char -- this is just an alternate way to specify | |
// the above strings (they work and act exactly the same) | |
char hi3[] = {'H', 'e', 'l', 'l', 'o', '\0'}; | |
char hi4[] = {'H', 'e', 'l', 'l', 'o', '\0'}; | |
// Strings as pointers to char literals | |
char *hi5 = "Hello"; // could write as: const char *hi3 = "Hello"; | |
char *hi6 = "Hello"; // (since it is constant anyway) | |
// Strings as pointers to char stored in heap & dynamically assigned | |
char *hi7 = malloc(6); // same as: char *hi5; hi5 = malloc(6) | |
char *hi8 = malloc(6); // (that may be easier to understand) | |
strcpy(hi7, "Hello"); | |
strcpy(hi8, "Hello"); | |
// strdup calls malloc and returns a pointer to allocated memory, so | |
// we can do the same thing as hi7 and hi8 like this: | |
char *hi9 = strdup("Hello"); // same as: char *hi0; hi9 = strdup("Hello") | |
char *hia = strdup("Hello"); // (also may be easier to understand) | |
// Mutate our strings by changing first character | |
hi1[0] = '1'; | |
hi2[0] = '2'; | |
hi3[0] = '3'; | |
hi4[0] = '4'; | |
// hi5[0] = '5'; // ERROR: can't mutate (but ok: hi5 = "New String") | |
// hi6[0] = '6'; // ERROR: can't mutate | |
hi7[0] = '7'; | |
hi8[0] = '8'; | |
hi9[0] = '9'; | |
hia[0] = 'a'; | |
// Print results (string @ variable location = location of first char) | |
printf("\nArray (stored in stack)\n"); | |
printf("hi1: %s @ %p = %p\n", hi1, &hi1, (void *) hi1); | |
printf("hi2: %s @ %p = %p\n", hi2, &hi2, (void *) hi2); | |
printf("hi3: %s @ %p = %p\n", hi3, &hi3, (void *) hi3); | |
printf("hi4: %s @ %p = %p\n", hi4, &hi4, (void *) hi4); | |
printf("\nPointers to literal (pointer in stack points to literal)\n"); | |
printf("hi5: %s @ %p = %p\n", hi5, &hi5, (void *) hi5); | |
printf("hi6: %s @ %p = %p\n", hi6, &hi6, (void *) hi6); | |
printf("\nPointers to heap, strcpy (pointer in stack to heap)\n"); | |
printf("hi7: %s @ %p = %p\n", hi7, &hi7, (void *) hi7); | |
printf("hi8: %s @ %p = %p\n", hi8, &hi8, (void *) hi8); | |
printf("\nPointers to heap, strdup (pointer in stack to heap)\n"); | |
printf("hi9: %s @ %p = %p\n", hi9, &hi9, (void *) hi9); | |
printf("hia: %s @ %p = %p\n", hia, &hia, (void *) hia); | |
// A good scout is tidy | |
free(hi7); | |
free(hi8); | |
free(hi9); | |
free(hia); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment