【问题标题】:Trying to put a string into an array in c [duplicate]试图将字符串放入c中的数组中[重复]
【发布时间】:2014-05-03 23:00:13
【问题描述】:

我创建了一个非常简单的代码,但它不起作用!我只想创建一个包含字符串的数组。然而,这些字符串必须在没有字符-字符方法的情况下放置。换句话说:

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    char wstr[20][10];         
    int i;
    for (i=0;i<20;i++)
            wstr[i]='BA';
    return 0;
}

但编译器向我显示警告和错误:

[Error] incompatible types when assigning to type 'char[10]' from type 'int'
[Warning] multi-character character constant [-Wmultichar]

我该怎么办?

【问题讨论】:

  • strcpy(wstr[i], "BA");

标签: c arrays string character


【解决方案1】:
for (i=0;i<20;i++)
            strcpy(wstr[i], "BA");

单引号用于单个字符;字符串文字使用双引号。 strcpy() 调用确保您以后能够修改这些值, 因为修改字符串文字本身是未定义的行为。

【讨论】:

    猜你喜欢
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 2012-10-07
    相关资源
    最近更新 更多