说我们有,
typedef struct{
char* ename;
char** pname;
}Ext;
Ext ext[5];
我要做的是按以下方式填充数据:
ext[0].ename="XXXX";
ext[0].pname={"A", "B", "C"}; // and so on for the rest of the array
- 我很确定这不是正确的做法,因为我遇到了错误。请告诉我正确的方法。感谢。
答案 0 :(得分:5)
第一项任务是正确的。
第二个不是。您需要动态分配数组:
ext[0].pname = malloc( sizeof(char*) * 5 );
ext[0].pname[0] = "A";
ext[0].pname[1] = "B";
//and so on
//you can use a loop for this
答案 1 :(得分:1)
您没有提到您正在使用的编译器。如果它符合C99,则以下内容应该有效:
const char *a[] = {"A", "B", "C"}; // no cast needed here
const char **b;
void foo(void) {
b = (const char *[]){"A", "B", "C"}; // cast needed
}
你的数组在typedef'd结构中是无关紧要的。