#include <stdio.h>
#include <string.h>

#define MAX 5

int main(int argc, char **argv)  {

    // Declare a variable str_literal of type pointer to char
    // Give it an initial value which is a pointer to a string literal
    // String literals cannot be modified.
    char *str_literal = "Hippopotamus";

    //str_literal[1] = 'u'; // Error

    // Use a string literal to statically initialize an array of char
    // (Automatically terminates the string with \0
    char str_array1[] = "Giraffe";

    str_array1[1] = 'u';

    // The above statement is equivalent to the next statement. (This is
    // the long way to do it!}
    char str_array2[] = {'B', 'e', 'a', 'r', '\0'};

    // Can also specify the size of the array in the declaration:
    // Note that the size of the array must be at least one more than the
    // length of the string.
    char str_array3[MAX] = "Lion";

    // This is wrong because it overflows the size of the array
    //str_array3[4] = 's';
    //str_array3[5] = '\0';

    // Also wrong
    strncpy(str_array3, "Giraffe", strlen(str_array3));
    printf("Almost str_array3: %s\n", str_array3);

    // bad way first
    // NEVER EVER use strcpy (I'M REALLY SERIOUS)
    strcpy(str_array3, "Sasquatch");
    printf("str_array3: %s\n", str_array3);


    // The right way to do it
    // The 3rd argument is to prevent overflowing the capacity of the
    // destination array.
    strncpy(str_array3, "Giraffe", sizeof(str_array3)); // 3rd arg == MAX
    printf("Now str_array3: %s\n", str_array3);
    str_array3[MAX - 1] = '\0';


    // The capacity of the array and the length of the string are two
    // different things.
    char str_array4[30] = "Elephant";

    printf("The size of str_array4 is %d.\n" 
           "The length of the string it holds is %d\n", 
	   sizeof(str_array4), strlen(str_array4));

    char *animals[4];
    animals[0] = str_array1;
    animals[1] = str_array2;
    animals[2] = NULL;

    printf("%c\n", animals[0][1]);

    



    return 0;
}
