【发布时间】:2013-04-13 16:07:00
【问题描述】:
在 C 中,我需要静态预分配一个数字数组,每个数字都与不同的字符串数组相关联。像下面这样的代码可以解决问题吗:
struct number_and_strings {
int nnn;
char **sss;
}
static struct number_and_strings my_list[] = {
{12, {"apple","banana","peach","apricot","orange",NULL}},
{34, {"tomato","cucumber",NULL}},
{5, {"bread","butter","cheese",NULL}},
{79, {"water",NULL}}
}
【问题讨论】:
-
为什么不试试看呢?
-
我担心它似乎会起作用,但由于对指针的一些误解,我最终会吞下或泄漏内存。
-
指针将指向数据段中的内存位置。那里没有泄漏。
-
不,它不会工作。数组不是指针,因此您不能使用字符串数组的初始化程序来初始化指向指针的指针。