C language array pointers and sample code _c language

Source: Internet
Author: User
Tags array length constant

An array is a set of data that has the same type, and each data is called an array element (element). All the elements in the array are contiguous in memory, and the entire array occupies a single piece of memory. with int arr[] = {99, 15, 100, 888, 252}; For example, the array is distributed in memory as shown in the following illustration:

When you define an array, you give a list of array names and arrays of lengths, which can be considered a pointer to the NO. 0 element of the array. in the C language, we refer to the address of the No. 0 element as the first address of the array. Take the above array for example, the following figure is the point of arr:

The following example shows how to traverse an array element in a pointer way:

#include <stdio.h>
int main () {
 int arr[] = {888, MB, 252};
 int len = sizeof (arr)/sizeof (int); To find the array length
 int i;
 For (i=0 i<len; i++) {
  printf ("%d", * (Arr+i)); * (Arr+i) equivalent to Arr[i]
 }
 printf ("\ n");
 return 0;
}

Run Result:

99 15 100 888 252

The 4th line of code is used to find the length of the array, and sizeof (arr) Gets the number of bytes occupied by the entire array, and sizeof (int) Gets the number of bytes occupied by an array element, the result of which is the number of elements that the array contains, or the length of the array.

In line 8th we use the expression "Arr+i", arr is the array name, pointing to the No. 0 element of the array, representing the first address of the array, arr+i to the I element of the array, * (Arr+i) representing the data of the first element, which is equivalent to Arr[i].
Arr is a int* type of pointer, each time plus 1 o'clock its own value will increase sizeof (int), plus I when its own value will increase sizeof (int) * I, which in the "operation of pointer variables" has been explained in detail.

We can also define a pointer to an array, for example:

int arr[] = {99, 15, 100, 888, 252};
int *p = arr;

The arr itself is a pointer that can be assigned directly to the pointer variable p. ARR is the address of the No. 0 element of the array, so int *p = arr; you can also write int *p = &arr[0];. That is, arr, p, &arr[0] are equivalent in all three ways, pointing to the No. 0 element of the array, or to the beginning of the array.

If a pointer is to an array, we refer to it as a group pointer (array pointer).

Note that the array itself has no type, the array element has a type, and the array element that P points to is the int type, so the type of P must also be int *.

In turn, p doesn't know that it's pointing to an array, p only knows it's pointing to an integer, and exactly how to use p depends on the programmer's Code.

Change the above code and use array pointers to traverse the array elements:

#include <stdio.h>
int main () {
 int arr[] = {888, MB, 252};
 int I, *p = arr, len = sizeof (arr)/sizeof (int);
 For (i=0 i<len; i++) {
  printf ("%d", * (P+i));
 }
 printf ("\ n");
 return 0;
}

Arrays are simply permutations of array elements in memory, without the start and end flags, sizeof (p)/sizeof (int) cannot be used when the length of the array is evaluated, because P is just a pointer to the type int, and the compiler does not know whether it is pointing to an integer or a series of integers (arrays). So sizeof (p) evaluates the number of bytes that the pointer variable itself occupies, rather than the number of bytes the entire array occupies.

That is, the number of entire array elements cannot be reversed according to array pointers, and information such as where the array starts, where it ends, and so on.

As we mentioned in the previous section, the addition and subtraction of pointer variables is based on the length of the data type. If a pointer variable p points to the beginning of the array, then p+i points to the first element of the array, and if p points to the nth element of the array, then p+i points to the n+i element, regardless of which p points to the first element of the array, p+1 always points to the next element, and P-1 always points to the The previous element.

Change the above code so that P points to the second element in the array:

#include <stdio.h>
int main () {
 int arr[] = {888, MB, 252};
 int *p = &arr[2]; can also write int *p = arr + 2;
 printf ("%d,%d,%d,%d,%d\n", * (P-2), * (p-1), *p, * (p+1), * (p+2));
 return 0;
}

Run Result:

99, 15, 100, 888, 252

After we introduce the array pointers, we have two schemes to access the array elements, one is to use subscript, the other is to use pointers.

1 Use subscript

This means accessing the array elements in the form of arr[i]. If P is a pointer to an array arr, you can also use P[i to access the array element, which is equivalent to Arr[i].

2) using pointers

That is, the array element is accessed using the form of * (P+i). The array name itself is also a pointer, and you can use * (arr+i) to access the array element, which is equivalent to * (P+i).

Either an array name or an array pointer, you can access the array elements in both of the above ways. The difference is that the array name is a constant, its value cannot be changed, and the array pointer is a variable (unless you specifically specify that it is a constant), its value can be arbitrarily changed. That is, the array name can only point to the beginning of the array, and the array pointer points to the beginning of the array and then to the other elements.

Change the code above to traverse an array element with the help of a self-increasing operator:

#include <stdio.h>
int main () {
 int arr[] = {888, MB, 252};
 int I, *p = arr, len = sizeof (arr)/sizeof (int);
 For (i=0 i<len; i++) {
  printf ("%d", *p++);
 }
 printf ("\ n");
 return 0;
}

Run Result:

99 15 100 888 252

In the 8th line of code, *p++ should be interpreted as * (p++), and each loop changes the value of P (p++ increases the value of P itself) so that P points to the next array element. The statement cannot be written as *arr++ because Arr is a constant, and arr++ changes its value, which is obviously wrong.

About the array pointer puzzle

Assuming P is a pointer to the nth element in the array arr, what does *p++, *++p, (*p) + + mean?

*p++ is equivalent to * (p++), which means that the value of the nth element is first obtained, and then the P is pointed to the next element, which is explained in detail above.

*++p equivalent to * (++P), will be ++p operations, so that the value of p increase, point to the next element, the overall equivalent of * (p+1), so you get the n+1 array elements of the value.

(*p) + + is very simple, you will first get the value of the nth element, and then add 1 to the value of the element. Assuming P points to the No. 0 element, and the No. 0 element has a value of 99, the value of the No. 0 element becomes 100 after the statement is executed.

Above on the C language array pointer data collation, follow-up continue to supplement the relevant knowledge, thank you for your support to this site!

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.