C语言 将2D数组行地址传递给1D数组以供将来使用



我试图存储数组的行地址。地址似乎是正确传递的,但它们似乎没有指向存储在数组中的值。

我想保存这些地址,以便我可以在Python应用程序中使用它们。

#include <stdio.h>
#include "func.h"
int main(int argc, const char * argv[]) {

uint64_t pkt[3];
func(pkt);

printf ("The address in main is %pn", pkt[0]);
printf ("The address in main is %pn", pkt[1]);
printf ("The address in main is %pn", pkt[2]);

uint8_t *ptr0 = (uint8_t*)pkt[0];
printf ("The address in main is %pn", ptr0);
printf("%dn", ptr0[0]);
printf("%dn", ptr0[1]);
printf("%dn", ptr0[2]);

uint8_t *ptr1 = (uint8_t*)pkt[1];
printf ("The address in main is %pn", ptr1);
printf("%dn", ptr1[0]);
printf("%dn", ptr1[1]);
printf("%dn", ptr1[2]);

uint8_t *ptr2 = (uint8_t*)pkt[2];
printf ("The address in main is %pn", ptr2);
printf("%dn", ptr2[0]);
printf("%dn", ptr2[1]);
printf("%dn", ptr2[2]);

return 0;
}
#include "func.h"

void func(uint64_t* pkt)
{
uint8_t matrix[3][3] = { {3 , 7 , 4} ,
{1, 2 , 8} ,
{8 , 3, 2} } ;
int i = 0;

for (i = 0; i < 3; i++)
{
pkt[i] = (uintptr_t)&matrix[i];
printf("The address is %pn", &matrix[i]);
printf("The address is %pn", pkt[i]);
}

}

函数func中声明的数组matrix具有自动存储时间。这意味着退出函数后,它将不再是活动的。因此,在退出函数后试图访问数组将调用未定义行为。

可以像

这样声明具有静态存储时间的数组
static uint8_t matrix[3][3] = { {3 , 7 , 4} ,
{1, 2 , 8} ,
{8 , 3, 2} } ;

在这种情况下,它在退出函数后仍然是活的,你可以通过指针间接访问它的元素。

注意printf这样的调用

printf ("The address in main is %pn", pkt[0]);

使用了不正确的转换说明符%p,同样会调用未定义的行为。

你的程序可以如下所示:

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
void func( uintptr_t *pkt )
{
static uint8_t matrix[3][3] = 
{ 
{ 3, 7, 4 },
{ 1, 2, 8 },
{ 8, 3, 2 } 
};
for (int i = 0; i < 3; i++)
{
pkt[i] = ( uintptr_t )matrix[i];
printf( "The address is %pn", ( void * )matrix[i] );
printf( "The address is %" PRIuPTR "n", pkt[i] );
}

putchar( 'n' );
}
int main(void) 
{
uintptr_t pkt[3];
func( pkt );

printf( "The address in main is %" PRIuPTR "n", pkt[0] );
printf( "The address in main is %" PRIuPTR "n", pkt[1] );
printf( "The address in main is %" PRIuPTR "n", pkt[2] );

uint8_t *ptr0 = ( uint8_t * )pkt[0];
printf( "The address in main is %pn", ( void * )ptr0 );
printf( "%un", ptr0[0] );
printf( "%un", ptr0[1] );
printf( "%un", ptr0[2] );

uint8_t *ptr1 = ( uint8_t * )pkt[1];
printf( "The address in main is %pn", ( void * )ptr1 );
printf( "%un", ptr1[0] );
printf( "%un", ptr1[1] );
printf( "%un", ptr1[2] );

uint8_t *ptr2 = ( uint8_t * )pkt[2];
printf( "The address in main is %pn", ( void * )ptr2 );
printf( "%un", ptr2[0] );
printf( "%un", ptr2[1] );
printf( "%un", ptr2[2] );
return 0;
}

程序输出为

The address is 0x5578e37f7010
The address is 93977701216272
The address is 0x5578e37f7013
The address is 93977701216275
The address is 0x5578e37f7016
The address is 93977701216278
The address in main is 93977701216272
The address in main is 93977701216275
The address in main is 93977701216278
The address in main is 0x5578e37f7010
3
7
4
The address in main is 0x5578e37f7013
1
2
8
The address in main is 0x5578e37f7016
8
3
2

我不同意。它像预期的那样工作。但是,不能保证,即使编程有点糟糕,从函数返回后matrix的地址也是有效的。因此,如果你不在堆中分配地址,就不应该使用在局部函数中定义的地址。

The address is 0x7ffcfeae7f6f
The address is 0x7ffcfeae7f6f
The address is 0x7ffcfeae7f72
The address is 0x7ffcfeae7f72
The address is 0x7ffcfeae7f75
The address is 0x7ffcfeae7f75
The address in main is 0x7ffcfeae7f6f
The address in main is 0x7ffcfeae7f72
The address in main is 0x7ffcfeae7f75
The address in main is 0x7ffcfeae7f6f
3
7
4
The address in main is 0x7ffcfeae7f72
1
2
8
The address in main is 0x7ffcfeae7f75
8
3
2

相关内容

  • 没有找到相关文章

最新更新