您能解释一下为什么在第一个程序中“array[ counter ]”打印值而在第二个程序中“array[ counter ]”打印地址吗?
第一个程序
main()
{
int array[ 4 ] = { 1, 2, 3, 4 };
int counter;
for( counter = 0 ; counter <= 3; counter++ )
printf( "%u\n", array[ counter ]);
}
第二个程序
main()
{
int array[ 4 ] [ 1 ]= { 1, 2, 3, 4 };
int counter;
for( counter = 0 ; counter <= 3; counter++ )
printf( "%u\n", array[ counter ]);
}
最佳答案
在第二个示例中,array[counter]
不是一个值,而是一个数组。
当您将数组作为参数传递给函数时,它会经历 array to pointer decay ,因此你得到一个指向数组开头的指针,而不是数组本身。
关于c - 为什么数组有时打印值有时打印地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25319691/