pointer to array of integers and normal array of integers

坚强是说给别人听的谎言 提交于 2019-12-06 02:09:27

问题


In KR C book page 112 it says that following:

int (*arr1)[10];

is a pointer to an array of 10 integers. I don't get what's difference between above and:

int arr2[10];

1- Isn't arr2 itself a pointer to array of 10 integers? (Because name of an array is a pointer itself.)

2- If the name of an array is the array address and pointer to that array, then both arr1 and arr2 are pointer to array of integers, isn't this true?


回答1:


Isn't arr2 itself a pointer to array of 10 integers?

No, it's an array.

Isn't the name of an array the array address and pointer to that array?

Array names can be/are converted to pointers to their 0th element (not the entire array).

So both arr1 and arr2 are pointer to array of integers?

No.

  1. arr1 is a pointer to an array of 10 integers.
  2. arr2 is an array of 10 integers. In most contexts it converts to a pointer to an integer (not a pointer to an array).

Check this wrong example for instance:

#include <stdio.h>

int main(void)
{
    int arr2[10] = {0};
    arr2[5] = 747;

    int (*arr1)[10] = {0};
    arr1[5] = 747;

    return 0;
}

Here I am treating both arr1 and arr2 as the "same thing", and I got this error:

C02QT2UBFVH6-lm:~ gsamaras$ gcc -Wall main.c 
main.c:9:13: error: array type 'int [10]' is not assignable
    arr1[5] = 747;
    ~~~~~~~ ^
1 error generated.

But if I do:

arr1[0][5] = 747;

it will pass compilation! Same with (*arr1)[5] = 747; of course.




回答2:


The relationship between arrays and pointers is one of the more confusing aspects of C. Allow me to explain by way of example. The following code fills and displays a simple one-dimensional array:

void showArray( int *ptr, int length )
{
    for ( int i = 0; i < length; i++ )
        printf( "%d ", ptr[i] );
    printf( "\n" );
}

int main( void )
{
    int array[10];
    for ( int i = 0; i < 10; i++ )
        array[i] = i;
    showArray( array, 10 );
}

You can see that when an array is passed to a function, the array name is taken as a pointer to the first element of the array. In this example, the first element is an int, so the pointer is an int *.

Now consider this code that fills and prints a two-dimensional array:

void showArray( int (*ptr)[10], int rows, int cols )
{
    for ( int r = 0; r < rows; r++ )
    {
        for ( int c = 0; c < cols; c++ )
            printf( "%2d ", ptr[r][c] );
        printf( "\n" );
    }
}

int main( void )
{
    int array[5][10];
    for ( int row = 0; row < 5; row++ )
        for ( int col = 0; col < 10; col++ )
            array[row][col] = row * 10 + col;
    showArray( array, 5, 10 );
}

The array name is still a pointer to the first element of the array. But in this example the first element of the array is itself an array, specifically an array of 10 int. So the pointer in the function is a pointer to an array of 10 int.

What I hope to impress upon you is that a pointer of the form (int *ptr)[10] has some correspondence to a two-dimensional array, whereas a pointer of the form int *ptr has some correspondence to a one-dimensional array.




回答3:


cdecl.org will show you what C interprets your variable declaration as. Quite handy as you start getting into more complicated variable declarations.

int arr2[10]; declare arr2 as array 10 of int

int (*arr1)[10]; declare arr1 as pointer to array 10 of int



来源:https://stackoverflow.com/questions/39628975/pointer-to-array-of-integers-and-normal-array-of-integers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!