Strings
Strings are arrays of char
. Their size is the amount of characters + 1 (the
NULL (\0
) character).
Code example:
char c[6]; // 6 is the minimum size for "HELLO"
c[0] = 'H';
c[1] = 'E';
c[2] = 'L';
c[3] = 'L';
c[4] = 'O';
c[5] = '\0'; // NULL
printf("%s", c); // uses NULL charater to know when to stop printing the array
// without NULL it would print memory contents past the end of the string
or
char c[6] = { 'H', 'E', 'L', 'L', 'O', '/0' }; // includes NULL
or
char c[6] = "HELLO"; // adds NULL implicitly
c = "SOMETHING" // ERROR! Arrays cannot me modified like this. strcpy should be used
or
char c[] = "HELLO"; // array will have a size of 6 implicitly
Memory
Arrays and pointers are different:
char c[20] = "Hello"; // stored on the STACK segment
c[0] = 'A'; // OK
char *d = "Hello"; // stored in the STATIC memory segment
d[0] = 'A'; // ERROR
If a function expects char *a
as an argument, how can it know if it’ll
be a pointer or an array?