"Pointer text" C: the difference between a pointer and an array, an array pointer and an array of pointers.

Source: Internet
Author: User

Pointer is a pointer, pointer variable in the system, will always account for 4 byte , its value is the address of a certain memory. The pointer can point to anywhere, but not anywhere you can access it through this pointer variable.


an array is an array whose size is related to the type and number of elements. When you define an array, you must specify the type and number of its elements. Arrays can store any type of data, but cannot store functions.

Speaking of pointers, let's think of arrays. Arrays and pointers are also different. Pointer refers to the address that describes a variable, and we can point to the next address by the pointer's offset. An array is a collection that describes the same elements, and the array name is a constant address and cannot be modified by itself.

We can use the array's subscript access or pointer pointer order to reference the output constant string. such as:P[i] ,*(p+i).

#include <stdio.h>int main () {int a[5] = {1, 2, 3, 4, 5};    int *ptr = (int *) (&a + 1);    printf ("%d,%d", * (A + 1), * (ptr-1));    System ("pause"); return 0;}

Analysis:

* (a+1) equivalent to a[0+1]=a[1]=2


&a is not degraded, refers to the entire array, PTR is the entire array plus 1, refers to A[4] immediately after the 1 address. Minus 1, the pointer goes back and refers to its order, which is equivalent to a[4],a[4]=5.


#include <stdio.h>int main () {int a[4] = {1, 2, 3, 4};    int *PTR1 = (int *) (&a + 1);    int *PTR2 = (int *) ((int) a + 1);    printf ("%x,%x", Ptr1[-1], *PTR2);    System ("pause"); return 0;}

Analysis:

&a is not degraded, is the entire array of integers, +1, is an integer array 4 after the size of the address of the int, ptr[-1] is the subscript way to the address just now minus 1 after the address, is 4.


A represents the address of the first element of the array, forcing the type to be converted to int, representing the contents of the first element,

I analyze arrays and pointers based on sizeof , such as:

#include <stdio.h>int main () {    int a[] = { 1, 2,  3, 4 };    printf ("%d\n",  sizeof (a));//16     printf ("%d\n",  sizeof (a + 0)),//4    printf ("%d\n",  sizeof (*a));//4     printf ("%d\n",  sizeof (a + 1));//4    printf ("%d\n",  sizeof (a[1]);//4    printf ("%d\n",  sizeof (&a));//4     printf ("%d\n",  sizeof (&a + 1)),//4    printf ("%d\n",  sizeof (&a [0]); /4    printf ("%d\n",  sizeof (&a[0] + 1));//4     System ("pause");     int a[5];    int *p = &a;/ /??     int *q = a;//??     system ("pause"); &NBSP;&NBSP;&NBSP;&NBSP;RETURN&Nbsp;0;} 


The above is an output constant string with an array of subscript access or pointer-pointer-order references.


Perhaps people will think at this time, why can only be a constant string,int Array I can not do it? At this point we can output through a loop and pointer-order reference.

Here I'll make a detailed distinction between arrays and pointers in the following points ( often test in the written interview brief answer ):

(1) pointers are often used to store variable variables, and arrays are often used to hold a type of infrequently changing data.

(2) The pointer variable itself is that the pointer holds the address of the data, and the address of the pointer variable itself is unknown to us. The array name represents the address of the first element of the array that we often say, not the address of the array. To say the address of an array, we are going to address the array name (without downgrading).

(3) access to Data mode: pointers are often indirect access, we first find the corresponding address, and then through the order of reference or array subscript way to access to the array. The array can be searched directly by subscript, i.e. direct access.

array pointer and number of pointersGroupSuch asint *p[10],[]priority is higher than*, which means that each element in the array isint*type of. In the case of drawing, it isTenainttype size, eachintthe type of storage isint*. andint(*q)[Ten]represents an array pointer, in the final analysis, no matter what pointer, it is a pointer, then it is the address, but the array pointer holds an array of pointers. In the case of paint, it's the address.4The size of the byte. This pointer points to theTenaintarray of type size.

Next, I want to analyze arrays and pointers from the point of view of the arguments and receive parameters.

when a one-dimensional array, a first- level pointer is passed, a copy of the argument is made and passed to the called function . The pointer variable itself cannot be passed to a function:

My_print(arr1,len)

void My_print (int arr[],int len) or

void My_print (int *arr,int len)

two-dimensional arrays :

The first element of the array is passed and received in an array.

void My_print (int arr[][4],int len)

The columns of an array cannot be omitted.

First- level pointers:

That is, make a copy of the P2, assuming its copy is named _P2. That which is passed to the inside of the function is _P2 and not P2 itself.

Level Two pointers:

int arr*[4];

Test (arr);// Each element of the array pointer is the first element to pass the argument

the int test (int **q)// and the array pointer holds the address, requiring a level two pointer to address the address.




"Pointer text" C: the difference between a pointer and an array, an array pointer and an array of pointers.

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.