简体   繁体   English

指向整数数组和正常整数数组的指针

[英]pointer to array of integers and normal array of integers

In KR C book page 112 it says that following: 在KR C book 112页中,它说:

int (*arr1)[10];

is a pointer to an array of 10 integers. 是一个指向10个整数数组的指针。 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? 1- arr2本身不是指向10个整数数组的指针吗? (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? 2-如果数组的名称是数组地址和指向该数组的指针,那么arr1arr2都是指向整数数组的指针,这不是真的吗?

Isn't arr2 itself a pointer to array of 10 integers? arr2本身不是指向10个整数数组的指针吗?

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). 数组名称可以转换为指向其第0个元素 (而不是整个数组)的指针。

So both arr1 and arr2 are pointer to array of integers? 所以arr1和arr2都是指向整数数组的指针?

No. 没有。

  1. arr1 is a pointer to an array of 10 integers. arr1是一个指向10个整数数组的指针。
  2. arr2 is an array of 10 integers. arr2是一个包含10个整数的数组。 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: 在这里,我将arr1arr2视为“同样的事情”,我得到了这个错误:

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; (*arr1)[5] = 747; of course. 当然。

The relationship between arrays and pointers is one of the more confusing aspects of C. Allow me to explain by way of example. 数组和指针之间的关系是C中比较混乱的方面之一。请允许我以举例的方式解释。 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 * . 在此示例中,第一个元素是int ,因此指针是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 . 但是在这个例子中,数组的第一个元素本身就是一个数组,特别是一个10 int的数组。 So the pointer in the function is a pointer to an array of 10 int . 所以函数中的指针是一个指向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. 我希望给你留下深刻印象的是形式(int *ptr)[10]的指针与二维数组有一些对应关系,而int *ptr形式的指针与一维数组有一些对应关系。

cdecl.org will show you what C interprets your variable declaration as. cdecl.org将向您展示C将您的变量声明解释为。 Quite handy as you start getting into more complicated variable declarations. 当你开始进入更复杂的变量声明时非常方便。

int arr2[10]; int arr2 [10]; declare arr2 as array 10 of int 将arr2声明为int的数组10

int (*arr1)[10]; int(* arr1)[10]; declare arr1 as pointer to array 10 of int 将arr1声明为指向int数组10的指针

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM