我的目标是访问 max_resources_per_client[1][1]
。
在无法修改的conf.c
中,我得到:
#define NUM_CLIENTS 5
#define NUM_RESOURCES 3
const unsigned int num_clients = NUM_CLIENTS;
const unsigned int num_resources = NUM_RESOURCES;
const unsigned int max_resources_per_client[NUM_CLIENTS][NUM_RESOURCES] = {
{7, 5, 3},
{3, 2, 2},
{9, 1, 2},
{2, 2, 2},
{4, 3, 3},
};
在我的文件 main.c
中,我有:
extern const unsigned int num_clients;
extern const unsigned int num_resources;
extern const unsigned int **max_resources_per_client;
如何访问 max_resources_per_client[1][1]
而不会导致段错误?
注意:尝试做
extern const unsigned int max_resources_per_client[num_clients][num_resources];
导致错误:在文件范围内可变地修改了“max_resources_per_client”
注意:尝试执行 extern const unsigned int max_resources_per_client[NUM_CLIENTS][NUM_RESOURCES];
导致错误:此处未声明 NUM_RESOURCES(不在函数中)
最佳答案
请参阅此 -> How to declare extern 2d-array in header?
您至少需要包含二维数组最右侧的列大小。您可以这样声明:
extern unsigned char LCD[][64];
否则编译器将无法计算第一行之后的偏移量。
关于c - 从 int** 读取 const 值,该值是从 int[][] 初始化的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36110047/