STRINGS (ARRAY OF CHARACTERS)

METHOD 1:

char protagonist[10] = {'B', 'i', 'l', 'b', 'o', '\0'};

 * whenever you use the name protagonist it’ll be interpreted as a pointer to the 
   first element
    - char protagonist[10] = "Frodo"; will point to "protagonist[0]" which is the
      character "F"
 * NTS: the CHAR data type is the distinguishing factor to this strings!
 * the \0 is an empty character or nul
    - null-terminated character arrays are used to represent strings. The \0 character 
      signifies the end of the string, allowing functions like strlen() and printf() to 
      know where the string ends
    - When users enter text from the keyboard and press the "Enter" key, the system 
      typically sends a newline character (\n) to the input stream. However, when 
      storing that input as a c style string, the newline character is often replaced 
      or followed by the null terminator \0. The C standard library functions like 
      fgets() or scanf() with the %s format specifier, when used to read keyboard input,
      will add the null terminator \0 to the end of the string. Therefore, the enter key
      indirectly causes the end of the string, by triggering functions that append the 
      null terminator. It is not the enter key itself that is stored as the null 
      terminator, but rather that the pressing of enter causes the program to know 
      to terminate the string.

METHOD 2:

char protagonist[10] = "Bilbo";

 * the null teminator is implied
    - when the character array is initialized this way, the compiler automatically 
      appends the null terminator (\0) to the end of the string.
    - The compiler handles the addition of the \0, so it doesn't have to explicitly 
      included in the initialization.
    - process: the compiler counts the number of characters inside the string. the 
      compiler reserves memory for the string but gets one character more than the 
      string's length. the compiler copies the entire string from our source code into 
      the reserved memory and appends an empty character at the end; the compiler 
      treats the string as a pointer to the reserved memory.
       - Don't forget: a string is stored as a sequence of characters followed by the 
         empty character but behaves as a pointer of type char *.

METHOD 3:

char protagonist[] = "Snape";

 * the compiler will count the characters itself and add empty character (Null) to the string
    - the array's size is larger than the initiator length by one.
       - the above example is similar to:
          char protagonist[6] = "Snape";

METHOD 4: ALTERNATIVE

#invalid assignment
char protagonist[10];
protagonist = "Gandalf";

 * note the above code snippet is not allowed because this assignment tries to force 
   the compiler to change the meaning of the pointer
    - protagonist is a pointer of type char * but that pointer has been designed to 
      point to only one location in the memory – the one which stores the protagonist 
      array, and nothing else. That pointer cannot be changed.

 * to solve this issue, the below snippet utilize the strcpy()
    - char protagonist[20];
      strcpy(protagonist, "Gandalf");
      
       - the left argument (protagonist) is the one to which the string is to be copied 
         and the right ("Gandalf") is the one which is being copied.
          - it copies the string along with its empty character into the location 
            pointed to by the "protagonist" variable.
             - it doesn't change the pointer's value so there will be artifact of old
               reference something that doesn't hae a reference or pointer

METHOD 5:

#pointer of type char
char *hero = "Dumbledore";
hero = "Sirius";

 * the compiler reserves the memory of 11 bytes 
   (10 for the hero's name itself + 1 for an empty char) and fills it with the 
   characters 'D', 'u', 'm', 'b', 'l', 'e' , 'd', 'o', 'r' , 'e' and '\0';
 * the compiler creates a variable named hero of type char *;
 * the compiler assigns the pointer to a newly reserved string to the hero variable.

Last updated