如何在C中返回静态分配的二维数组?
我在如何在C中返回静态分配的二维数组?,c,multidimensional-array,return-type,function-declaration,C,Multidimensional Array,Return Type,Function Declaration,我在spbox.c中有以下代码: #include <stdbool.h> #include <stdint.h> typedef struct { bool initialized; uint32_t Spbox[8][64]; // Combined S and P boxes } spboxState; static spboxState stateInstance; uint32_t ** desGetSpbox(void) { if
spbox.c
中有以下代码:
#include <stdbool.h>
#include <stdint.h>
typedef struct {
bool initialized;
uint32_t Spbox[8][64]; // Combined S and P boxes
} spboxState;
static spboxState stateInstance;
uint32_t ** desGetSpbox(void) {
if (!(stateInstance.initialized)) {
// TODO: Compute data to populate Spbox array
stateInstance.initialized = true;
}
return stateInstance.Spbox;
}
我收到一条关于不兼容指针返回类型的警告:
spbox.c:16:9: warning: incompatible pointer types returning 'uint32_t [8][64]' from a function with result type 'uint32_t **' (aka 'unsigned int **') [-Wincompatible-pointer-types]
return stateInstance.Spbox;
^~~~~~~~~~~~~~~~~~~
1 warning generated.
如何更改代码以消除警告?据说
uint32\u t**
与uint32\u t[8][64]
不兼容。但是如果我尝试将后面的作为返回类型,我会得到一个语法错误。您可能无法返回数组。但您可以返回指向数组或其第一个元素的指针
比如说
uint32_t ( * desGetSpbox(void) )[8][64] {
if (!(stateInstance.initialized)) {
// TODO: Compute data to populate Spbox array
stateInstance.initialized = true;
}
return &stateInstance.Spbox;
}
或
这是一个演示程序
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
enum { M = 8, N = 64 };
typedef struct {
bool initialized;
uint32_t Spbox[M][N]; // Combined S and P boxes
} spboxState;
static spboxState stateInstance;
uint32_t ( *desGetSpbox1( void ) )[M][N]
{
stateInstance.Spbox[0][0] = 10;
return &stateInstance.Spbox;
}
uint32_t ( *desGetSpbox2( void ) )[N]
{
stateInstance.Spbox[0][0] = 10;
return stateInstance.Spbox;
}
int main(void)
{
uint32_t ( *p1 )[M][N] = desGetSpbox1();
printf( "%" PRIu32 "\n", ( *p1 )[0][0] );
uint32_t ( *p2 )[N] = desGetSpbox2();
printf( "%" PRIu32 "\n", ( *p2 )[0] );
return 0;
}
这回答了你的问题吗@罗伯塔维:这个问题中的很多东西似乎都不相关。它说您不能返回函数中声明的数组,因为当函数退出时它将被释放;那不是我要做的。建议返回指向结构的指针。我可以这样做,但是为什么我不能只返回指向结构数组元素的指针呢?
为什么我不能只返回指向结构数组元素的指针呢?
——如果您正确设置了它的类型,您可以。否则,C不知道数组的形状是什么。请看下面的答案。如果你知道你在做什么,你也可以简单地返回一个空指针。@RobertHarvey我需要的是有人教我返回数组元素(甚至是多维元素)指针的函数的语法(相当复杂)。弗拉德的回答就是这样。我在另一个问题上找不到,谢谢。您的第一个示例就是我想要做的,我只是不知道(相当复杂的!)语法。虽然考虑过这个问题,但也许我应该使用typedef来让它更清晰:typedef uint32\u t spbox\t[8][64]代码>。然后我就可以做spbox\u t*desGetSpbox(void){
。这相当于你的第一个例子,对吗?如果是这样,我可能会这样做,因为它看起来更清晰。@SimonKissane是的,你可以引入一个typedef。
uint32_t ( * desGetSpbox(void) )[64] {
if (!(stateInstance.initialized)) {
// TODO: Compute data to populate Spbox array
stateInstance.initialized = true;
}
return stateInstance.Spbox;
}
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
enum { M = 8, N = 64 };
typedef struct {
bool initialized;
uint32_t Spbox[M][N]; // Combined S and P boxes
} spboxState;
static spboxState stateInstance;
uint32_t ( *desGetSpbox1( void ) )[M][N]
{
stateInstance.Spbox[0][0] = 10;
return &stateInstance.Spbox;
}
uint32_t ( *desGetSpbox2( void ) )[N]
{
stateInstance.Spbox[0][0] = 10;
return stateInstance.Spbox;
}
int main(void)
{
uint32_t ( *p1 )[M][N] = desGetSpbox1();
printf( "%" PRIu32 "\n", ( *p1 )[0][0] );
uint32_t ( *p2 )[N] = desGetSpbox2();
printf( "%" PRIu32 "\n", ( *p2 )[0] );
return 0;
}
10
10