Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

c - Error "incompatible types in assignment", and I don't know why

When I compile this code I get this error:

Q3.c:15: error: incompatible types in assignment

Some knows why?

The purpose of the code is to get 10 names and print them.

#include <stdio.h>
#include <string.h>

#define NAME 10
#define LONG 50

int main()
{
    int i = 0;
    char names[NAME][LONG] = {0};
    printf("Enter 10 names:
");
    for(i = 0; i < NAME; i++)
    {
        fgets(names[i], LONG, stdin);
        names[strcspn(names[i], "
")] = 0;
    }
    for(i = 0; i < NAME; i++)
    {
        printf("%s", names[i]);
    }
}

question from:https://stackoverflow.com/questions/66066712/error-incompatible-types-in-assignment-and-i-dont-know-why

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
        names[strcspn(names[i], "
")] = 0;

is wrong because names[something] is an array (char[LONG]) and you cannot assign things there.

It seems the line should be

        names[i][strcspn(names[i], "
")] = 0;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...