C's array parameters and pointer parameters (31)

Source: Internet
Author: User

We said it earlier. In the C language, array parameters are degraded to pointers. So what is this for? InThe C language only passes the parameter as a copy of the value, and when passing an array to the function, instead of copying an entire array into the function, the array name is treated as a constant pointer to the first element address of the array.

Then, when the C language was set up, it was mainly used for UNIX operating systems, and UNIX was highly efficient. So the C language is efficient as the original design goal:when the a> parameter is passed, the efficiency of copying the entire array will be greatly reduced;b> the parameter is on the stack, and too large an array copy will cause the stack to overflow . The function call stack is stored in a piece of memory, and if the stack overflows, the function call will not execute and the program will collapse.

Two-dimensional array parameters also degenerate, which can be seen as a one-dimensional array, each element of which is a one-dimensional array. The parameters of the first dimension in the two-dimensional array parameters can be omitted, such as:void f (int a[5]) ==> void f (int a[]) ==> void F (int* a); void g (int a[3][3]) ==> void g ( int [][3]) ==> void g (int (*a) [3]); then the one-dimensional array we normally call will degenerate into one-dimensional pointers, and the two-dimensional arrays will not degenerate into two-dimensional pointers, but rather degenerate into array pointers. So what kind of parameter would degenerate into a two-dimensional pointer? We've summed up the table below.

Array parameters
Equivalent pointer parameters
One-dimensional array: float a[5]
Pointer: float* A
Array of pointers: int* A[5]
Pointer's pointer: int** a
Two-dimensional array: char a[3][4]
Pointer to array: char (*A) [4]

We can see that the pointer array is degraded to a two-dimensional pointer as a parameter. In the C language, it is not possible to pass any number of multidimensional arrays to a function, but all dimension lengths other than the first dimension must be provided , and the dimension information outside the first dimension is used to perform pointer operations, and the essence of the N-dimensional array is a one-dimensional array, and the element is an array of N-1 dimensions. Only the first dimension is mutable for the function parameters of a multidimensional array. Come down here. Take the code as an example to analyze the code as follows

#include  <stdio.h>void access (int a[][3], int row) {    int  col = sizeof (*a)  / sizeof (int);    int i = 0;     int j = 0;        printf (" sizeof (a)  = %d\n ",  sizeof (a)),     printf (" sizeof (*a)  = %d\n ",  sizeof (*a));         for (i=0; i<row; i++)      {        for (j=0; j<col; j++)          {             printf ("%d\n",  a[i][j]);         }    }         printf ("\ n");} Int main () {&NBSP;&NBSP;&NBSP;&NBSP;INT&NBSP;A[3][3]&NBSP;=&NBSP;{{0,&NBSP;1,&Nbsp;2}, {3, 4, 5}, {6, 7, 8}};    int aa[2][2] =  {0};        access (a, 3);     access (AA, &NBSP;2);         return 0;}

We see in the program that we have defined the two-dimensional array of 3*3,2*2 in lines 25th and 26, but the second dimension of the parameter in the Access function specifies 3. So we call this function in line 29th to make an error, because the type does not match. Let's take a look at the compilation results

We saw it as a warning, which means that the program was compiled, but it allowed the result to be indeterminate. We see that the AA array we define is 2*2, but it prints out 6 numbers, which is the 2*3. So it's wrong to call us that way.

Down we look at a three-dimensional array of code, the code is as follows

#include  <stdio.h>void access_ex (int b[][2][3], int n) {     Int i = 0;    int j = 0;    int k  = 0;        printf ("sizeof (b)  = %d\n",  sizeof (b));     printf ("sizeof (*B)  = %d\n",  sizeof (*b));         for (i=0; i<n; i++)     {         for (j=0; j<2; j++)         {             for (k=0; k<3; k++)              {                 printf ("%d\n",  a[i][j][k]);             }        }    }         printf ("\ n");} Int main () {    int aa[2][2] = {0};    int b[1 ][2][3]&NBSP;=&NBSP;{0};&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;ACCESS_EX (b, 1);   &NBSP;&NBSP;ACCESS_EX (aa, 2);         return 0;}

We specify in the ACCESS_EX function that the second dimension is 2, and the third dimension is 3. But the array AA we defined is not like this, let's look at the results of the compilation

We see that the second compiled result is also indeterminate. Through this section of the array parameters and pointers to the learning, summed up as follows:1, C language will only be copied by the value of the parameter, and the array parameters must degenerate into a pointer; 2. The multidimensional array parameter has to provide all dimension lengths except the first dimension, and the first dimension of the function parameter values for multidimensional arrays is variable.


Welcome Everybody to study C language together, can add me qq:243343083.

C's array parameters and pointer parameters (31)

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.