简体   繁体   English

遇到错误:打印出字符串数组期间 char 数组初始值设定项中的元素过多

[英]Faced an error: excess elements in char array initializer during print out of array of strings

I've tryed to print out some array of strings but faced error: excess elements in char array initializer Please make a hint what's worng with this code?我试图打印出一些字符串数组,但遇到错误:char 数组初始值设定项中的元素过多请提示此代码有什么问题?

Step 1 change '' with "" nothing changed, the same error. Step 1 把''换成""什么都没变,同样的错误。 Step 2 change maschar to *maschar, it helped, thaks.第 2 步将 maschar 更改为 *maschar,这很有帮助,谢谢。

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

int main()
{
    char maschar[] = {'char', 'mas', 'got'};
    int lenchar = sizeof(maschar) / sizeof(*maschar);

    for (int i = 0; i< lenchar; i++)
        printf("%s\n", *(maschar+i));

    return 0;

Step 1 change '' with "" nothing changed, the same error. Step 1 把''换成""什么都没变,同样的错误。 Step 2 change maschar to *maschar, it helped, thaks.第 2 步将 maschar 更改为 *maschar,这很有帮助,谢谢。

char *maschar[] = {"char", "mas", "got"};
int lenchar = sizeof(maschar) / sizeof(*maschar);

for (int i = 0; i< lenchar; i++)
    printf("%s\n", *(maschar+i));

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM