typedef struct mobiltelefon {
char herstellername[HLEN];
double displaydiagonale;
aufloesung_t aufloesung;
char standards[NUMBER_OF_STRINGS][STRINGLENGTH+1] = {"GPRS", "EDGE", "HSDPA", "HSUPA", "HSPA",
"LTE"};
} telefon_t;
我一直收到错误expected ; at the end of declaration list
。
答案 0 :(得分:1)
更改
char (*standards[6])[5] = {{"GPRS"}, {"EDGE"}, {"HSDPA"}, {"HSUPA"}, {"HSPA"}, {"LTE"}};
到
char standards[6][6] = {{"GPRS"}, {"EDGE"}, {"HSDPA"}, {"HSUPA"}, {"HSPA"}, {"LTE"}};
一个简单的例子:
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
char standards[6][6] = {{"GPRS"}, {"EDGE"}, {"HSDPA"}, {"HSUPA"}, {"HSPA"}, {"LTE"}};
for(i=0;i<6;i++)
printf("%s\n",standards[i]);
return 0;
}
请注意char standards[6][5]
在您的情况下是错误的,因为2D数组中最长的字符串是HSDPA
而HSUPA
长度为5,您需要再多一个字节来终止{ {1}} char。
答案 1 :(得分:0)
删除*和{}。改变这个:
char (*standards[6])[5] = {{"GPRS"}, {"EDGE"}, {"HSDPA"}, {"HSUPA"}, {"HSPA"}, {"LTE"}};
对此:
char standards[6][6] = {"GPRS", "EDGE", "HSDPA", "HSUPA", "HSPA", "LTE"};
结构:
您无法在其声明中初始化结构成员。你需要做这样的事情:
typedef struct mobiltelefon {
char herstellername[HLEN];
double displaydiagonale;
aufloesung_t aufloesung;
char standards[NUMBER_OF_STRINGS][STRINGLENGTH+1];
} telefon_t;
并像这样初始化变量(仅限C99 / C11):
telefon_t iphone = {.standards = {"GPRS", "EDGE", "HSDPA", "HSUPA", "HSPA", "LTE"}};
或者使用memcpy:
memcpy(iphone.standards[0], "GPRS", 5);
memcpy(iphone.standards[1], "EDGE", 5);
memcpy(iphone.standards[2], "HSDPA", 6);
memcpy(iphone.standards[3], "HSUPA", 6);
memcpy(iphone.standards[4], "HSPA", 5);
memcpy(iphone.standards[5], "LTE", 4);
但这是浪费空间,如果每部手机都有相同的标准,我宁愿选择全局变量:
char teleon_standards[6][6] = {"GPRS", "EDGE", "HSDPA", "HSUPA", "HSPA", "LTE"};
答案 2 :(得分:0)
如果您知道要放入其中的字符串的最大长度,则可以使用2D数组 例如,如果我们知道没有字符串长度超过4个字符,那么只需执行
char arr[6][5] = {"GPRS" , "EDGE" , "HSDPA" , ...};
更好的方法是
char *arr[] = { "GPRS" , "EDGE" , "HSPDA"}
这样你就不必担心字符串的长度或你可以添加多少个字符串
答案 3 :(得分:0)
初始化字符串数组的一种方法是:
char *standards[] = {"GPRS", ...};
您不必传递字符串数,编译器就会知道这一点。
答案 4 :(得分:0)
结构中的变量无法初始化。显然,标准无法在结构内初始化。