简体   繁体   中英

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?

Step 1 change '' with "" nothing changed, the same error. Step 2 change maschar to *maschar, it helped, thaks.

#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 2 change maschar to *maschar, it helped, thaks.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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