About function (ii) array pointer and pointer array __ function

Source: Internet
Author: User

Reference http://blog.csdn.net/touch_2011/article/details/6966980 1, overview pointer array: An array of pointers can be said to be "an array of pointers," first of which is an array, and secondly, "pointers "Modifies this array, meaning that all elements of this array are pointer types, and in 32-bit systems, pointers are four bytes." Array pointers: Array pointers can be said to be "pointers to arrays", first the variable is a pointer, and secondly, the "array" modifies the pointer, meaning that the pointer holds the first address of an array, or that the pointer points to the first address of an array.
Depending on the explanation above, you can see the difference between pointer arrays and array pointers, because they are simply variables of a type. 2, array pointer (pointer to array) (1) The representation of an array in memory

To create an array is to open up a contiguous space in memory, such as int a[4], or in the memory to open up a size of 4*sizeof (int) bytes of memory space. A two-dimensional array is a special one-dimensional array.
Let's look at a piece of code:

void Main ()  
{  
    int a[2][2]={1,2,3,4};//This is a 2*2 two-dimensional array  
    int (*p) [2];//array pointer  
    p=a;//make P point to array a  
}

Notice the sentence in the code: INT (*P) [2]; the p here is an array pointer variable.
The elements of a are stored in memory in this way:
(2) Understanding array names and arrays of pointer variables

Now we think about what a,a[0],a[1],p,a+1,a[0]+1,p+1 really is.

Analysis:
1. A is an array name, the type is a pointer to a one-dimensional array, not a variable, and the value of a is a pointer constant, that is, no a++ or a=p of these operations. A point to the first address of this contiguous space, the value is &a[0][0].
2. A[0] is a one-dimensional array name, the type is a pointer to an integral type, the value is &a[0][0], and the value is a constant.
3. A[1] is a one-dimensional array name, the type is a pointer to an integral type, the value is &a[1][0], and the value is a constant.
4. P is an array pointer variable, pointing to the pointer variable of a one-dimensional array, the value being &a[0][0]. You can perform operations such as p++;p =a.
5. a+1 represents the next line of elements, or it can be understood to point to the next one-dimensional array.
6. * (A+1) is the first address to be taken out.
7. A[0]+1 is the first element that points to the 1th element in line No. 0, or to a one-dimensional array a[0].
8. P+1 and A+1
9. * (p+1) and * (a+1)
10. Although the value of a and a[0] is the same, but the type is different, the meaning of the expression is different. It is not difficult to understand why ((a+i) +j) and A[i][j] are equivalent through analysis.

Since the PA is a pointer to the address of an array, then when we define an array, the array name is the first address of the array, so what is the difference between the two?

int  a[2];

A is an integer array of length 2, and A is the first address of the first element of the array. Since a is an address and p is a pointer to an array, can you assign a value to P? The answer is no. Because A is the first address of the first element of the array, p store is the first address of the array, A is the int* type, a+1,a value will be real plus 1, and P is int[2] type, p+1,p will add 2, although the first address of the array and the first element of the first address of the same value, but the two operations are different, So the type mismatch cannot be directly assigned, but it can: P = &a,p is quite the row pointer to the two-dimensional array, and now it points to the address of a[2]. (3) The pointer is an iterator of the array

#include <stdio.h>  
#define M 2  
#define N 3  

int main ()  
{  
    int a[m][n]={1,2,3,4,5,6};  
    int *start=&a[0][0];  
    int * Const END=START+M*N;  
    for (; start!=end;start++)  
        printf ("%-5d", *start);  
    Putchar (' \ n ');  
    return 0;  
}

Understanding this code, traversing a two-dimensional array with pointers, is much like the vector iterator in the C + + standard library. Note that only a For loop is used here, which also shows that a two-dimensional array is actually a special one-dimensional array. (4) The difference between an array name and an array pointer variable

From the analysis in (2), the array name is the pointer, the type is a pointer to the element type, but the value is a pointer constant, and the compiler reserves the memory space for the number of elements specified by the declaration when the array is declared. An array pointer is a pointer to an array, and the compiler reserves only memory space for the pointer itself when declaring the pointer variable.

Look at this code:

#include <stdio.h>  
void Main ()  
{  
    int a[2][2]={1,2,3,4};//This is a 2*2 two-dimensional array  
    int (*p) [2];//array pointer  
    p=a;//p points to array a  
    printf ("%d\n%d\n", sizeof a,sizeof p);  
}


Note that when sizeof is used for a variable, it returns the size of the actual space occupied by the variable. When sizeof is used for an array name, the size of the entire array is returned (the size of this refers to the number of bytes occupied). P is a pointer variable that occupies four bytes. And a is the array name, so sizeof a returns the number of bytes occupied by all the elements in array a. Read the sizeof and see what the following code outputs

#include <stdio.h>  

void Main ()  
{  
    int a[2][2]={1,2,3,4};//This is a 2*2 two-dimensional array  
    int (*p) [2];//array pointer  
    p=a;//p points to array a  
    printf ("%d\n%d\n", sizeof (a+1), sizeof (p+1));  
    printf ("%d\n%d\n", sizeof (a+0), sizeof (p+0));  


As seen from the result, a is transformed into a pointer variable when doing the + operation, at which point the A+i type is a pointer variable, not an array name. But A[i] is the array name of a one-dimensional array, and the value of sizeof (A[0]) is 8.

Now let's look at a piece of code:

#include <stdio.h>  

void f (int a[][2])  
{  
    printf ("%d\n", sizeof a);  
}  
void Main ()  
{  
    int a[2][2]={1,2,3,4};//This is a 2*2 two-dimensional array  
    printf ("%d\n", sizeof a);  
    f (a);  
}


Explanation: This is because the name of the array is converted to a pointer variable, and it is noted that f (int a[][2] in function f) does not need to specify the length of the two-dimensional array, which can be changed to int (*A) [2]. So the pass is an array pointer variable.

Summary: The type of the array name is a pointer to the element type, and the value is a pointer constant. The type of (a+1) is a pointer variable. When you pass an array name as a parameter, you actually pass a pointer variable. sizeof the results returned when the variable and array names are manipulated. An array pointer is a pointer to an array, and its value can be a variable. 2. Array of pointers (array holding pointers) (1) Understanding pointer array

An array of type int is called an integer array, so the array holding the pointer is called an array of pointers.

#include <stdio.h>  

void Main ()  
{  
    int i=1,j=2;  
    P is combined with [] and then combined with an  
    int *p[2];//pointer Array, which holds the array of pointers  
    p[0]=&i;  
    p[1]=&j;  
    printf ("%d", sizeof (p));  
}


This example array p on two elements, p[0] is a pointer to I, P[1] is a pointer to J. Both pointers are int pointers, so p is an array that holds an int pointer. sizeof (p) returns the total space occupied by the array, so the program output is 8. (2) In-depth understanding

First you define an array of pointers, and since it's an array, the name is arr.

Char *arr[4] = {"Hello", "World", "Shannxi", "Xian"};
Arr is the array of pointers I've defined, which has four elements, each of which is a pointer to a char * type that holds the first address of its corresponding string.

When an operator appears around a variable, a person who does not remember the precedence of the operator will struggle to arr the variable with which operation Fuxian. If you are defining an array of pointers yourself, if you don't know the precedence of an operator, then add parentheses (), such as a pointer array, which can be written as char * (arr[4]), but be sure to define the variable you defined before you define it, and if the purpose is an array, enclose the arr[4. If it is a pointer, enclose the *arr. If you see a piece of code like this, it can be either an array or a pointer from his initialization, obviously, I'm defining an array, and if it is a pointer, it's initialized with null.
How large is the array of pointers. The answer is 16 bytes, because it is an array of pointers. Whenever these problems arise, the mind must first react to the memory map.

The leftmost column here is a simple but descriptive memory graph, typically from a stack area to a code area, from a high address to a low address. The stack grows downward and the heap grows upwards.

ARR[4] is an array defined in the main function. corresponding to the corresponding to the memory, arr is a stack area, there are four elements of an array, and each array is a pointer, so that its four elements each accounted for four bytes, so the size of the variable arr is 16 bytes.

Initialize arr {"Hello", "World", "Shannxi", "Xian"}; These four exist in memory, just with arr this variable is not in the same space, they are allocated in the read-only data area, array arr[4] four pointer elements, Storing the first address of these four strings, imagine that there are four of invisible fingers from the stack area to the data area. Arr+1 will skip four bytes,. Which is the size of a pointer.
This is quite similar to defining char *P1 = "Hello", char *p1 = "World", char *p3 = "Shannxi", char *p4 = "Xian", which is four pointers, each holding a string at the first address, and then using the arr[4] this array respectively Holding these four pointers, an array of pointers is formed. (3) ordering array of pointers


The ordering of pointer arrays is interesting because the array holds pointers, which are sorted by comparing the dictionary order of the strings pointed to by the pointer. The function implementation is as follows:

void sort (char **pa, int n)/bubble sort
{
    int i, J;
    char *tmp = NULL;
    for (i = 0; i < n-1; i++) {
        for (j = 0; J < N-1-i; J +) {
            if (strcmp (* (PA+J), * (pa+j+1)) > 0) {
                tmp = * (PA + j);
                * (PA + j) = * (PA + j + 1);
                * (PA + j + 1) = tmp;
            }}}
3. Declaring a function that returns an array pointer 1, the array can not be copied, the function can not return the array, can only return the array of pointers or references.
Typedet int arr[10]; Arr is a type alias that represents an array of 10 integers  
using arr=int[10];    Equivalent to the above
int arr[10];   Arr is an array  
int *p1[10] containing 10 integers;  P1 and [] are highly binding, so P1 is first an array of 10 sizes, where p is an array name, followed by *, which means that each element in the array is an integer pointer  
int (*P) [10]=&arr;  
2, the declaration returns the array of pointers

① returns function pointers in the form of the following:

Type (*function (parameter_list)) [dimension] 
int (*func (int i)) [10];
func (int i) means that an int type argument (*func (int)) is required to invoke the Func function, meaning that the result of a function call is a pointer (*func (int i)) [10] means that the pointer to the result of the function call is a pointing array. Array size is ten int (*func (int)) [10] means that the array elements are integral types in the array to which the pointer to the function call result is directed.

But if we use the type alias, declaring a function that returns the array pointer looks a lot more elegant.

Using ARRT=INT[10]; Arr is an alias for an array of size 10, which is equivalent to int[10] as a type, an integer array of the size of  
arrt* func (int i);  
3. Use the end return type

The definition of any function can be returned using a trailing end, which begins with a-> symbol after the formal parameter list, in order to represent the true return type of the function after the parameter list, we place an auto in the front, for example:

Auto func (int i)->int (*) [10];  

You can now see clearly that the Func function returns a pointer to an array of 10 integers. 4. Use Decltype

You can also use the DECLTYPE keyword to declare a return type, such as a typical example, if you know which array the pointer of the function returns to.

#include <iostream>  
int odd[] = {1, 3, 5, 7, 9};  
int even[] = {0, 2, 4, 6, 8};  
Decltype (Odd) * func (int i)  
{return  
    (I & 1)? &odd: &even;  
}  
int main ()  
{  
    std::cout << (*func (1)) [2]<< Std::endl;  
    Std::cout << (*func (2)) [2] << Std::endl;  
}  


The role of Deltype is simply to derive the type of expression and not to convert the array type to the corresponding pointer, where the odd type is deduced as an array of size 5, but only an array of this type, to make it a pointer to this type, Add the same * as normal.

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.