Arrays of Strings
You can make an array of strings to group multiple strings together:
char *stringArray[5]; // space to store 5 char *s
You can also use the following shorthand to initialize a string array:
char *stringArray[] = {
"my string 1",
"my string 2",
"my string 3"
};
Pointers
- A pointer is a variable that stores a memory address.
- Because there is no pass-by-reference in C like in C++, pointers let us pass around the address of one instance of memory, instead of making many copies.
- One (8 byte) pointer can refer to any size memory location!
Character Arrays
When you declare an array of characters, contiguous memory is allocated on the stack to store the contents of the entire array.
An array variable refers to an entire block of memory. You cannot reassign an existing array to be equal to a new array.
char str[6] = "apple";
char str2[8] = "apple 2";
str = str2; // not allowed!
An array’s size cannot be changed once you create it; you must create another new array instead.
char *
When you declare a char pointer equal to a string
literal, the characters are not stored on the stack.
Instead, they are stored in a special area of
memory called the “data segment”. You cannot
modify memory in this segment.
sizeof
A char array is not a pointer; it refers to the entire array contents. In fact, sizeof returns the size of the entire array!
Pointer Arithmetic
When you do pointer arithmetic (with either a pointer or an array), you are adjusting the pointer by a certain number of places (e.g. characters).
Strings as Parameters
When you pass a char * string as a parameter, C makes a copy of the address stored in the char *, and passes it to the function. This means they both refer to the same memory location.
When you pass a char array as a parameter, C makes a copy of the address of the first array
Arrays of Strings
You can make an array of strings to group multiple strings together:
char *stringArray[5]; // space to store 5 char *s
Here’s an ASCII diagram to illustrate the memory layout:
stringArray: [ ptr1 ] [ ptr2 ] [ ptr3 ] [ ptr4 ] [ ptr5 ]
↓ ↓ ↓ ↓ ↓
NULL NULL NULL NULL NULL
You can also use the following shorthand to initialize a string array:
char *stringArray[] = {
"my string 1",
"my string 2",
"my string 3"
};
stringArray: [ ptr1 ] [ ptr2 ] [ ptr3 ]
↓ ↓ ↓
"my string 1" "my string 2" "my string 3"
Pointers
- A pointer is a variable that stores a memory address.
- Because there is no pass-by-reference in C like in C++, pointers let us pass around the address of one instance of memory, instead of making many copies.
Here’s an example of a pointer pointing to an integer:
int x = 42;
int *ptr = &x;
x: [ 42 ]
ptr: [ &x ] → [ 42 ]
- One (8 byte) pointer can refer to any size memory location!
Character Arrays
When you declare an array of characters, contiguous memory is allocated on the stack to store the contents of the entire array.
char str[6] = "apple";
str: [ 'a' ] [ 'p' ] [ 'p' ] [ 'l' ] [ 'e' ] [ '\0' ]
An array variable refers to an entire block of memory. You cannot reassign an existing array to be equal to a new array.
char str[6] = "apple";
char str2[8] = "apple 2";
str = str2; // not allowed!
An array’s size cannot be changed once you create it; you must create another new array instead.
char *
When you declare a char pointer equal to a string literal, the characters are not stored on the stack. Instead, they are stored in a special area of memory called the “data segment”. You cannot modify memory in this segment.
char *str = "apple";
str: [ ptr ] → "apple" (in data segment)
sizeof
A char array is not a pointer; it refers to the entire array contents. In fact, sizeof returns the size of the entire array!
char str[6] = "apple";
printf("%zu\n", sizeof(str)); // Outputs 6
str: [ 'a' ] [ 'p' ] [ 'p' ] [ 'l' ] [ 'e' ] [ '\0' ]
sizeof(str): 6
Pointer Arithmetic
When you do pointer arithmetic (with either a pointer or an array), you are adjusting the pointer by a certain number of places (e.g., characters).
char str[] = "hello";
char *ptr = str;
ptr += 2;
str: [ 'h' ] [ 'e' ] [ 'l' ] [ 'l' ] [ 'o' ] [ '\0' ]
ptr: ↑
Strings as Parameters
When you pass a char * string as a parameter, C makes a copy of the address stored in the char *, and passes it to the function. This means they both refer to the same memory location.
void printString(char *str) {
printf("%s\n", str);
}
str (in function): [ ptr ] → original string
When you pass a char array as a parameter, C makes a copy of the address of the first array element, and passes it (as a char *) to the function.
void printString(char str[]) {
printf("%s\n", str);
}
str (in function): [ ptr ] → original array
Strings and Memory
These memory behaviors explain why strings behave the way they do:
- If we make a variable to store a string literal that is a char[], we can modify the characters because its memory lives in our stack space.
- If we make a variable to store a string literal that is a char *, we cannot modify the characters because its memory lives in the data segment.
- We can set a char*equal to another value, because it is just a pointer.
- We cannot set a char[] equal to another value, because it is not a pointer; it refers to the block of memory reserved for the original array.
- If we change characters in a string passed to a function, these changes will persist outside of the function.
- When we pass a char array as a parameter, we can no longer use sizeof to get its full size.