下面是我的程序
#include<stdio.h>
int main(){
int a[][4] = {1, 2, 3, 4, 5, 6, 7, 8};
int (*ptr)[4] = a;
for (int i=0; i< 4; i++){
printf("n Address of %d, and value is %d", ptr[i], (*ptr)[i]);
}
++ptr;
for (int i=0; i< 4; i++){
printf("n Address of %d, and value is %d", ptr[i], (*ptr)[i]);
}
printf("n ------------------------------------------------");
printf("n Address of %d, and value is %d", &a[0][0], a[0][0]);
printf("n Address of %d, and value is %d", &a[0][1], a[0][1]);
printf("n Address of %d, and value is %d", &a[0][2], a[0][2]);
printf("n Address of %d, and value is %d", &a[0][3], a[0][3]);
printf("n Address of %d, and value is %d", &a[1][0], a[1][0]);
printf("n Address of %d, and value is %d", &a[1][1], a[1][1]);
printf("n Address of %d, and value is %d", &a[1][2], a[1][2]);
printf("n Address of %d, and value is %d", &a[1][3], a[1][3]);
return 0;
}
Out is the Following.
Address of -294704432, and value is 1
Address of -294704416, and value is 2
Address of -294704400, and value is 3
Address of -294704384, and value is 4
Address of -294704416, and value is 5
Address of -294704400, and value is 6
Address of -294704384, and value is 7
Address of -294704368, and value is 8
------------------------------------------------
Address of -294704432, and value is 1
Address of -294704428, and value is 2
Address of -294704424, and value is 3
Address of -294704420, and value is 4
Address of -294704416, and value is 5
Address of -294704412, and value is 6
Address of -294704408, and value is 7
Address of -294704404, and value is 8
使用简单的&打印的地址序列和静态代码是正确的,其中使用指针,我得到正确的值,但第二行元素的地址覆盖了第二个元素的第一行的地址。
使用指针的结果是
Address of -294704416, and value is 5
覆盖
的地址Address of -294704416, and value is 2
这并不影响我在矩阵上做的操作,但它显示了相同的地址如何在内部工作,我正在寻找更多的解释。
您声明了一个二维数组,其中包含两个' ';(即数组的元素类型为一维数组int[4]
)
int a[][4] = {1, 2, 3, 4, 5, 6, 7, 8};
指针ptr
int (*ptr)[4] = a;
指向第一行。
for循环
for (int i=0; i< 4; i++){
printf("n Address of %d, and value is %d", ptr[i], (*ptr)[i]);
}
表达式ptr[i]
具有int[4]
类型,并且作为printf
调用的参数,它被隐式转换为int *
类型的指针。
所以在格式字符串中(在printf调用和其他printf调用中),你必须使用转换说明符p
而不是d
来输出指针
printf("n Address of %p, and value is %d", ( void * )ptr[i], (*ptr)[i]);
由于数组只有两个"row "然后for循环中的表达式ptr[i]
访问数组之外的内存(对于i
等于3
),从而导致未定义的行为。
在任何情况下,输出的地址都不对应行中输出元素的地址(i
等于0
时的第一个输出地址除外)。
看来你需要的是以下内容
printf("n Address of %p, and value is %d", ( void * )( ( *ptr ) + i ), (*ptr)[i]);
表达式( void * )( ( *ptr ) + i )
指向给定"行"的第i个元素。
也就是你在main中的代码看起来像
int a[][4] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int( *ptr )[4] = a;
for (int i = 0; i < 4; i++) {
printf( "n Address of %p, and value is %d", ( void * )( ( *ptr ) +i ), ( *ptr )[i] );
}
++ptr;
for (int i = 0; i < 4; i++) {
printf( "n Address of %p, and value is %d", ( void * )( ( *ptr ) + i ), ( *ptr )[i] );
}
printf( "n ------------------------------------------------" );
printf( "n Address of %p, and value is %d", ( void * )&a[0][0], a[0][0] );
printf( "n Address of %p, and value is %d", ( void * )&a[0][1], a[0][1] );
printf( "n Address of %p, and value is %d", ( void * )&a[0][2], a[0][2] );
printf( "n Address of %p, and value is %d", ( void * )&a[0][3], a[0][3] );
printf( "n Address of %p, and value is %d", ( void * )&a[1][0], a[1][0] );
printf( "n Address of %p, and value is %d", ( void * )&a[1][1], a[1][1] );
printf( "n Address of %p, and value is %d", ( void * )&a[1][2], a[1][2] );
printf( "n Address of %p, and value is %d", ( void * )&a[1][3], a[1][3] );