C Language Strings Flashcards
Why can / should we not edit pointer to a string literal
Has undefined results
○ On some compilers (e.g. LCC), it works!
○ On other compilers, the data in the array is treated as read-only or “constant.”
Character array vs. string vs. pointer to the string
○ String
■ char my_string [ ] = {‘T’, ‘o’, ‘m’, ‘\0’} ;
○ Pointer to string
■ char* my_string_ptr = “Tom” ;
■ You cannot edit a pointer to a string literal!
○ Array contains a string
■ char my_string [ ] = “Tom” ;
■ You are allowed to edit a character array.
Array of Strings / 2D Array
■ char my_2D_array [3][4] = { {‘I’, ‘\0’},{‘a’, ‘m’, ‘\0’},{‘T’, ‘o’, ‘m’,‘\0’} } ;
○ This is just a 2D array of characters in C (notice no NULLs): char my_2D_array [3][4] = { {‘I’}, {‘a’, ‘m’}, {‘T’, ‘o’, ‘m’} } ;
Passing a 2D array in C
○ Note: absence of row number is permissible in 2D array declaration, but absence of column number is not permissible in 2D array declaration.
○ In reality, our function is receiving a memory address. Dereferencing with [ ] is automated pointer arithmetic. By providing the # of columns in the 2D
array, we are telling the compiler how many rows in memory to advance each time the pointer is referenced using the first [ ] operator.
strlen()
only counts characters up to (but not including) the first ‘\0’ although space is allocated as asked for.
Arrays of pointers
○ Integer pointer: int var[ ] = {10, 100, 200}; int i, *ptr[MAX]; for ( i = 0; i < MAX; i++) { ptr[i] = &var[i]; /* assign the address of integer. */ } ○ String Pointer: char *names[ ] = { "Zara Ali", "Hina Ali", "Nuha Ali", "Sara Ali" };
Common use of pointers to pointers
Pointers to pointers: often used in C to receive addresses of arrays of pointers to strings
○ int a = 1;
○ int * a_ptr = &a;
○ int ** a_ptr_ptr = &a_ptr;
○ Dereferencing
char I_array [] = {‘I’, ‘\0’ } ;
char Am_array [] = {‘a’, ‘m’, ‘\0’ } ;
char Tom_Array [] = {‘T’, ‘o’, ‘m’, ‘\0’ } ;
char* array_of_ptrs [3] = { I_array, Am_array, Tom_Array } ;
char** ptr_to_ptr = array_of_ptrs ;
○ ptr_to_ptr[0] => I_array
○ ptr_to_ptr[0][0] => ‘I’
○ Recall the two strings that we can use inside main( ) via argv[ ].
Is this structure valid
char *names[]={“anna”,”bob”}
Yes it is an array of strings.
To access ‘a’ in ‘anna’ -> names[0][0]
Pointer to Array of Strings, i.e. Pointer to array of pointers
char *ptr[3] = {"I","am","XYZ"}; char **ptrptr = ptr; printf("%s\n", *ptrptr); ptrptr++; printf("%s\n", *ptrptr);
prints out:
I
am
char *text = “hello”;
vs
char text[] = “hello”
First one, called string literal. Immutable stored in read only memory.
–> Pointer to a string
Second one mutable.
-> array that contains a string
char *text[] = {“hello”, “world”};
vs
char text[2][6] = {“hello”, “world”};
- Array of two pointers to string literals. string literals immutable
- 2D Array containing 2 strings
- char text[2][6] = {“hello”, “world”};
- char text[][6] = {“hello”, “world”};
- char text[][] = {“hello”, “world”};
I can skip row index but NOT column index. i.e. 1 ad 2 work 3 not.
Reason, compiler needs to know how many lines to jump over before storing next string
Where are string literals placed
C compiler can place it anywhere it likes.
It may be stored in static memory
It may be in the OBJECT file itself (like immediate data)
What happens if you try to edit point to string literal
Undefined results
On some compilers it works, On other compileres, the data in the array is treated as read-only or constant
strlen
size_t strlen(const char *str) This function returns the length of string