Note

Strings are just an array of characters ended with a string terminator: \0 (this just represents the integer 0)

char my_string[1024]; // We are letting this string be 1023 characters MAX (excluding terminator)
my_string[0]='H'; 
my_string[1]='e'; 
my_string[2]='l'; 
my_string[3]='l'; 
my_string[4]='o';
my_string[5]='\0'; // This indicates the string is over; the rest of the characters will not be read in most cases

If you do not have a terminator, the string will continue until it eventually hits a '0' somewhere within memory, which is obviously not an intended behavior.

#include<string.h>

^^ This is a library with useful string things

strcpy(destination, value)

  • Hard copies the string from value into the destination.
  • You can also use it to initialize strings so that there is no reference between the two strings in memory. If you use the = operator and update 1 string, the other will also change! That is most likely not intended behavior.
char cool_string[1024];
strcpy(cool_string, "I am so cool, you guys");
// But it's the same as just doing
char cooler_string[1024] = "I am even cooler, you guys";
// Unless we changed our mind and want to update cooler_string
cooler_string = "ah ha new string!" // This does NOT work! You can only use string literals at declaration of the variable. You must use strcpy instead
strcpy(cooler_string, "ah ha new string!");
  • Here is a better example of why it’s useful
char bad_string[1024];
char other_string[1024] = "This is no good. We are sharing!";
// BAD
bad_string = other_string; // Changing either string will update both of them.
 
// GOOD
strcpy(bad_string, other_string); // There is no sharing.

strcat(string_before, string_added)

  • Concatenates the two strings. Yes, you cannot just use + between two strings any more.
  • It will extend the original string, so it’s destructive…

strlen(string)

  • Gives the length of a string.

strcmp(string_1, string_2)

  • Compares two strings. Returns 0 if they are equal and other numbers if they aren’t. Similar to compare() in Java

String literals

String literals are stored in read-only memory. Those are the ones made with quotation marks: "" You CANNOT change or update them in any way.

Here’s an example:

char w1[10] = "Mangos <3";
char* w2 = "Mangos <3";
 
w1[5] = '!'; // Allowed
w2[5] = '!'; // Segfault

Here’s what they look like in the debugger:

  • Since string literals are read only, and the char* for w2 is only pointing to the first character in the string literal, you CANNOT change it, which is why it causes the Segfault (you aren’t allowed to edit that bit of memory!)
  • This means you cannot pass them into functions that ask for char arrays, like strcpy!

Weirdness

  1. Character literals and escaping them
int a = '0' // This will be 48 (because ASCII)
int b = '\0' // This will be the integer "0"
  1. You don’t need to use '\0' to terminate; 0 works just fine
 char chr[3];
 
chr[0] = 'h';
chr[1] = 'e';
chr[2] = 'y';
chr[3] = 0; // This will terminate the string, too.
 
// Also note that, even if the string spills out of the range of the array, when it's treated like a string it doesn't care. It just waits until the escape sequence is hit.
  1. Having a string with the entire thing filled with non-zero characters implies there is no delimiter for it to hit!
char a[10];
for (int i = 0; i < 10; i++)
{
	a[i] = 'A' + 1;
}
printf("%s", a); // WIll print A-->J, and then garbage until we hit the delimiter.