Pointers to pointers, pointer and string arrays, multidimen1_arrays. Stacks and queues.
Pointer pointers
• What does this function do?
Void swap (int second a, int second B ){
Int temperature temp = temperature;
Required a = required B;
Required B = temp;
}
• How does it compare to the familiar version of swap?
Void swap (int second a, int second B ){
Int temp = temperature;
Required a = required B;
Required B = temp;
}
The first one is to exchange two pointers, and the data in the memory has not changed; the second pointer has not changed, but the data in the memory has changed.
Pointer array
• Have an array int arr [100]; that contains some numbers
• Want to have a sorted version of the array, but not modify arr
• Can declare a pointer array int sort sorted_array [100]; containing pointers to elements of arr and sort the pointers insteadof the numbers themselves
• Good approach for sorting arrays whose elements are very large (like strings)
In struct sorting, pointer plays a major role.
String arrays
• An array of strings, each stored as a pointer to an array ofchars
• Each string may be of different length
Char str1 [] = "hello";/bytes length = 6 bytes/
Char str2 [] = "goodbye";/bytes length = 8 */
Char str3 [] = "ciao";/bytes length = 5 bytes/
Char character strArray [] = {str1, str2, str3 };
• Note that strArray contains only pointers, not the charactersthemselves
The array stores not characters but pointers. Therefore, pay attention to the scope of automatic variables. If you use the heap memory allocated by malloc, pay attention to the free time.
Multidimen1_arrays
• C also permits multidimensional arrays speci encoded ed using [] brackets notation:
Int world [20] [30]; is a 20x30 2-Darray of int's
• Higher dimensions possible:
Char bigcharmatrix [15] [7] [35] [4];-what are the dimensions of this?
• Multidimensional arrays are rectangular; pointer arrays can bearbitrary shaped
What is the difference between a two-dimensional char array and a char * array? I think the two-dimensional char array is a continuous space in the memory, and the element address is obtained through pointer algebra. the char * array is a one-dimensional array, and each element is a pointer, the biggest difference between the two is that the two-dimensional char array stores objects, while the char * array does not store any objects.