什么,“从非宽字符串初始化的宽字符数组”是什么意思,我如何在C中纠正它?

时间:2012-10-07 17:53:16

标签: c arrays string

我正在尝试制作一个程序,显示用户订购的杂货。它将写入另一个文件(当它完成时)数据,并显示每个食物组的平均值。它是这样的:

#include<stdio.h>

int main()
{
    int beverage[3][20] = {
        "Soda"
        "Water"
        "Coffee"
    }; /* Here is 9 */
    int produce[3][20] = {
        "Apple"
        "Carrot"
        "Banana"
    }; /* Here is 14 */
    int dairy[3][20] = {
        "Milk"
        "Yogurt"
        "Cheese"
        }; /* Here is 19 */
    int dessert[3][20] = {
        "Ice Cream"
        "Cake"
        "Chocolate"
    }; /* Here is 24 */
    int meat[3][20] = {
        "Chicken"
        "Seafood"
        "Beef"
    }; /* Here is 29 */
    int grain[3][20] = {
        "Bread"
        "Muffin"
        "Cereal"
    }; /* Here is 34 */

    printf("Beverages: %p\n ", beverage[1]);
    printf("Produce: %p\n ", produce[2]);
    printf("Dairy: %p\n ", dairy[0]);
    printf("Candy: %p\n ", dessert[0]);
    printf("Meat: %p\n ", meat[2]);
    printf("Grain: %p\n ", grain[1]);

    return 0;
}

它在第9,14,19,24,29和34行显示错误消息“从非宽字符串初始化的宽字符数组”。那么错误消息对我的代码意味着什么?如何更改它以及在哪里?

1 个答案:

答案 0 :(得分:5)

如果您想要的是字符串列表(我假设是您想要的)。您应该使用char代替int

char beverage[3][20] = {
    "Soda",
    "Water",
    "Coffee"
};