我试图将数组传递给我的函数,但输出中没有显示,为什么?

时间:2022-11-01 21:48:48

I have:

void displayMatrix(int a[][]){

    int howManyRows = sizeof(a)/sizeof(a[0]);
    int howManyColumns = sizeof(a)/sizeof(int);

    int r, c;

    for (r = 0; r < howManyRows; r++){
            if (r >= 1){
                printf("\n");
            }
        for (c = 0; c < howManyColumns; c++){
            printf("%d ",a[r][c]);
        }
    }


}

but when I create an array

但是当我创建一个数组

int samp[4][5] = {
    {1,2,3,4,5},
    {6,7,8,9,10},
    {11,12,13,14,15},
    {16,17,18,19,20}
    };

and pass it to the function in main, nothing shows up on the screen. What is going on?

并将其传递给main中的函数,屏幕上没有任何内容。到底是怎么回事?

2 个解决方案

#1


2  

This happens because in function parameters, arrays decay to pointers on the top level. So your function

发生这种情况是因为在函数参数中,数组会衰减到顶层的指针。所以你的功能

void displayMatrix(int a[4][5])

is no different from

没有什么不同

void displayMatrix(int (*a)[5])

Regarding N1570, Section 6.7.6.3, Paragraph 7:

关于N1570,第6.7.6.3节,第7段:

A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, ...

参数声明为''数组类型''应调整为''限定指向类型'',...

This results in an unexpected behavior:

这会导致意外行为:

int howManyRows = sizeof(a)/sizeof(a[0]);

Where sizeof(a) == sizeof(void*) and sizeof(a[0]) == 5*sizeof(int)

其中sizeof(a)== sizeof(void *)和sizeof(a [0])== 5 * sizeof(int)

Usually the length of 5 integers is longer than a pointer, so you get howManyRows = 0 after all.

通常5个整数的长度比指针长,所以你得到howManyRows = 0。

Try hard-coding # of rows or pass it by an extra parameter:

尝试对行数进行硬编码或通过额外参数传递:

void displayMatrix(int a[][5], int rows)

#2


1  

This is a classic gotcha of C programming. You have to understand what an array in C really is and how it works to understand why this code will not work.

这是C编程的经典问题。您必须了解C中的数组究竟是什么以及如何理解为什么此代码不起作用。

When you create an "array" as such of size x, the compiler sets aside enough memory to hold x elements of some some object. This is really just a lump of memory with nothing otherwise special about it. If the array is one of 4 integers, each 4 bytes in size, then the compiler sets aside 4 * 4 bytes (16). The variable that contains the array is in fact nothing more than a pointer to the start of the array. Accessing the array is a simple matter of going to the start, and moving forward over the required number of bytes to find the object specified.

当您创建大小为x的“数组”时,编译器会留出足够的内存来保存某些对象的x元素。这实际上只是一块内存,没有别的特别之处。如果数组是4个整数之一,每个大小为4个字节,那么编译器会留出4 * 4个字节(16)。包含数组的变量实际上只不过是指向数组开头的指针。访问数组是一个简单的问题,即开始,并在所需的字节数上向前移动以查找指定的对象。

That said, arrays do have a special property within the function they are declared. They are known as arrays and their total size is stored, and can be accessed by the sizeof operator. This property is not valid anywhere outside of that function. If you pass the array as a parameter, all that is passed is the pointer and not any information about the array. There is no way to get its size. Your code here will never work and cannot be made to work.

也就是说,数组在声明它们的函数中确实有一个特殊的属性。它们被称为数组,它们的总大小被存储,并且可以由sizeof运算符访问。此属性在该函数之外的任何位置都无效。如果将数组作为参数传递,则传递的所有内容都是指针,而不是有关数组的任何信息。没有办法达到它的大小。这里的代码永远不会工作,也无法工作。

#1


2  

This happens because in function parameters, arrays decay to pointers on the top level. So your function

发生这种情况是因为在函数参数中,数组会衰减到顶层的指针。所以你的功能

void displayMatrix(int a[4][5])

is no different from

没有什么不同

void displayMatrix(int (*a)[5])

Regarding N1570, Section 6.7.6.3, Paragraph 7:

关于N1570,第6.7.6.3节,第7段:

A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, ...

参数声明为''数组类型''应调整为''限定指向类型'',...

This results in an unexpected behavior:

这会导致意外行为:

int howManyRows = sizeof(a)/sizeof(a[0]);

Where sizeof(a) == sizeof(void*) and sizeof(a[0]) == 5*sizeof(int)

其中sizeof(a)== sizeof(void *)和sizeof(a [0])== 5 * sizeof(int)

Usually the length of 5 integers is longer than a pointer, so you get howManyRows = 0 after all.

通常5个整数的长度比指针长,所以你得到howManyRows = 0。

Try hard-coding # of rows or pass it by an extra parameter:

尝试对行数进行硬编码或通过额外参数传递:

void displayMatrix(int a[][5], int rows)

#2


1  

This is a classic gotcha of C programming. You have to understand what an array in C really is and how it works to understand why this code will not work.

这是C编程的经典问题。您必须了解C中的数组究竟是什么以及如何理解为什么此代码不起作用。

When you create an "array" as such of size x, the compiler sets aside enough memory to hold x elements of some some object. This is really just a lump of memory with nothing otherwise special about it. If the array is one of 4 integers, each 4 bytes in size, then the compiler sets aside 4 * 4 bytes (16). The variable that contains the array is in fact nothing more than a pointer to the start of the array. Accessing the array is a simple matter of going to the start, and moving forward over the required number of bytes to find the object specified.

当您创建大小为x的“数组”时,编译器会留出足够的内存来保存某些对象的x元素。这实际上只是一块内存,没有别的特别之处。如果数组是4个整数之一,每个大小为4个字节,那么编译器会留出4 * 4个字节(16)。包含数组的变量实际上只不过是指向数组开头的指针。访问数组是一个简单的问题,即开始,并在所需的字节数上向前移动以查找指定的对象。

That said, arrays do have a special property within the function they are declared. They are known as arrays and their total size is stored, and can be accessed by the sizeof operator. This property is not valid anywhere outside of that function. If you pass the array as a parameter, all that is passed is the pointer and not any information about the array. There is no way to get its size. Your code here will never work and cannot be made to work.

也就是说,数组在声明它们的函数中确实有一个特殊的属性。它们被称为数组,它们的总大小被存储,并且可以由sizeof运算符访问。此属性在该函数之外的任何位置都无效。如果将数组作为参数传递,则传递的所有内容都是指针,而不是有关数组的任何信息。没有办法达到它的大小。这里的代码永远不会工作,也无法工作。