如何定义和初始化一个struct内的字符串数组? [英] How do I define and initialize an array of strings inside of a struct?

查看:470
本文介绍了如何定义和初始化一个struct内的字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;

我不断收到预期的错误;在声明列表

推荐答案

修改

char (*standards[6])[5] = {{"GPRS"}, {"EDGE"}, {"HSDPA"}, {"HSUPA"}, {"HSPA"}, {"LTE"}};

char standards[6][6] = {{"GPRS"}, {"EDGE"}, {"HSDPA"}, {"HSUPA"}, {"HSPA"}, {"LTE"}};

一个简单的例子:

A quick example:

#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;
}

请注意字符标准[6] [5] 是错在你的情况为你的二维数组中最长的字符串为 HSDPA HSUPA 这是一个长度为5,则需要多一个字节终止'\\ 0'字符。

Kindly note char standards[6][5] is wrong in your case as the longest string in your 2D array is HSDPA and HSUPA which is of length 5, you will need one more byte for terminating '\0' char.

这篇关于如何定义和初始化一个struct内的字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆