简体   繁体   English

错误:char数组初始化程序中的多余元素

[英]error: excess elements in char array initializer

I am creating a weather report for college assignment. 我正在为大学分配天气报告。 I working on a function that is suppose to print out the data for all 12 months. 我正在研究一个函数,该函数应该打印出所有12个月的数据。 I have an array that is 12 in size and holds the name of the 12 months. 我有一个大小为12的数组,并保存12个月的名称。 When I compile the program I keep getting the following error: 当我编译程序时,我不断收到以下错误:

assignment3.c:149:5: error: excess elements in char array initializer

Here is the function that has this array: 这是具有此数组的函数:

    void printMonthlyStatistic(int month,const struct MonthlyStatistic* monthly){

    int i;
    char monthNames[12] = {"January", "February", "March", "April", "May", "June", "July", "August",
                           "September", "October", "November", "December"};

    for (i=0;i<12;i++) {

        printf(" %c | %.1f | %.1f | %.1f | %.1f \n",monthNames[i],monthly->maxTemperature,monthly->minTemperature,monthly->averageTemperature,
                                                    monthly->totalPrecipitation);
    }

}

You have defined an array of individual char values and since the elements are string literals and consist of more than one character there are excess elements in your initializer and hence the error message. 您已经定义了一个由单个char值组成的数组,并且由于元素是字符串文字,并且由多个字符组成,因此初始化器中有多余的元素,因此会出现错误消息。

Instead you could define an array of char* where each element will point to the start of each string literal in the array. 相反,您可以定义一个char*数组,其中每个元素都将指向数组中每个字符串文字的开头。

const char* monthNames[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

As these strings will probably end up in the read only data segment of your binary. 因为这些字符串可能会以二进制文件的只读数据段结尾。 It wont hurt to declare them const . 将它们声明为const不会有伤害。

In your code 在你的代码中

char monthNames[12]

represents an array of char, not a string, but a single character. 表示一个char数组,不是一个字符串,而是一个字符。 You must change your array to something like this: 您必须将数组更改为以下形式:

char* montNames[12]

in order to have an array of strings and not a simple character. 为了有一个字符串数组而不是一个简单的字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 char数组初始值设定项错误中的多余元素 - Excess elements in char array initializer error char数组初始化程序中的多余元素 - excess elements in char array initializer 数组初始化程序中的多余元素 - Excess elements in array initializer 遇到错误:打印出字符串数组期间 char 数组初始值设定项中的元素过多 - Faced an error: excess elements in char array initializer during print out of array of strings 警告:数组初始值设定项中的多余元素 - warning: excess elements in array initializer 不知道如何处理错误“数组初始值设定项中的元素过多” - Not sure how to deal with the error "excess elements in array initializer" 通过指向 2D 字符数组的指针访问。 警告:数组初始值设定项中的多余元素 - access via pointer to 2D char array. warning: excess elements in array initializer C:警告:数组初始值设定项中的元素过多; &#39;xxx&#39; 即将初始化; 需要“char *”,但类型为“int” - C: warning: excess elements in array initializer; near initialization for ‘xxx' ; expects ‘char *’, but has type ‘int’ 编译警告。数组初始化程序中的多余元素 - Compilation Warning.Excess elements in array initializer 在数组初始值设定项中有多余的元素可以吗? - Is it ok to have excess elements in array initializer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM