6.087 practical programming in C, lec7

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.