所以我有一个结构:
typedef struct Board {
size_t size;
char* board;
} Board;
我想知道是否有可能在初始化结构时进行如下操作:
Board boardStruct = {
solutionLength,
char emptyBoard[size]
};
不幸的是,当我尝试以这种方式进行操作时,出现编译错误: expected expression before 'char'
有任何想法吗?我试图避免在结构初始化之外声明数组,但是如果这是唯一的选择,我想那是我必须走的路线。
您可以这样做:
#include <stdlib.h>
typedef struct Board {
size_t size;
char* board;
} Board;
int main()
{
const int solutionLength = 3; /* or #define solutionLength 3 */
Board boardStruct = {
solutionLength,
malloc(solutionLength)
};
return 0;
}
或更接近您的建议:
#include <stdlib.h>
typedef struct Board {
size_t size;
char* board;
} Board;
int main()
{
const int solutionLength = 3; /* or #define solutionLength 3 */
char emptyBoard[solutionLength];
Board boardStruct = {
solutionLength,
emptyBoard
};
return 0;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句