C language pointers, arrays, and functions

Source: Internet
Author: User

Basic explanation

1, the nature of the pointer is an address-related composite type, its value is the location of data storage (address), the essence of the array is a series of variables.

2, the array name corresponds to (rather than pointing to) a piece of memory, its address and capacity in the life period remain unchanged, only the contents of the array can be changed. Pointers can be pointed at any time to any type of memory block, which is characterized by "variable", so we often use pointers to manipulate dynamic memory.

3. When the array is passed as a function parameter, the array is automatically degraded to the same type of pointer.

Issues: pointers and arrays

I heard that Char a[] is the same as Char *a, isn't it?

Answer and Analysis:

There are some essential differences between pointers and arrays. Of course, in some cases, such as when the array is passed as a function parameter, since the array is automatically degraded to the same type of pointer, the pointer passed in as a function parameter does have a certain consistency within the function, but this is only a special case, in essence, the two are different. Take a look at the following examples:

Char a[] = "Hi, pig!";

Char *p = "Hi, pig!";

The memory layouts for these two variables are as follows:

Array a needs to occupy 8 bytes of space in memory, which is marked by name a. The pointer p requires 4 bytes of space to hold the address, which is marked with the name P of the 4 bytes. The address can be located almost anywhere, or it can not be anywhere, that is, a null pointer. The p now points to a contiguous 8 bytes, the string "Hi, pig!".

In addition, for example: for a[2] and p[2], both return the character ' I ', but the compiler produces a different execution code. For A[2], the execution code starts at the position of a, moves backwards by 22 bytes, and then takes out the characters. For P[2], the execution code takes an address from the position of P, adds 2 to it, and then takes out the corresponding character in memory.

Question: Array pointers

Why do we need to define pointers to arrays rather than to array elements at some point? How to define?

Answer and Analysis:

With pointers, which are designed to hold the address of an element to take advantage of the unique advantages of pointers, it is natural to use pointers to arrays when the element needs to be an array, such as a multidimensional array that needs to be dynamically generated in high dimensions.

The definition example is as follows: Int (*pelement) [2].

Here is an example:

int Array[2][3] = {{1,2,3},{4,5,6}};

int (*PA) [3]; Defines a pointer to an array

PA = &array[0]; The ' & ' symbol reflects the meaning of the PA, indicating a pointer to an array

printf ("%d", (*PA) [0]);//will print array[0][0], that is 1

pa++//Guess, who does it point to? ARRAY[1]? That's right!

printf ("%d", (*PA) [0]); Will print array[1][0], that is 4

The above example fully illustrates the array pointer-a definition and use of a pointer to an entire array.

It should be explained that, as we discussed in the fourth article, the pointer's stepping refers to the size of the object it refers to, so the pa++ moves the whole backward through the dimensions of an array, rather than simply moving the dimensions of an array element backwards.

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.