Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incompatible types in assignment

Tags:

c

I am writing some code in C:

int main(){
    char guess[15];
    guess = helloValidation(*guess);
    return 0;
}

And my function is:

char[] helloValidation(char* des) {
    do {
        printf("Type 'hello' : ");
        scanf("%s", &des);
    }while (strcmp(des, "hello") != 0);
        return des
}

But it is giving me this error:

incompatible types in assignment 
like image 219
Muaz Usmani Avatar asked Oct 18 '25 19:10

Muaz Usmani


1 Answers

The guess array is modified by the function itself. You are then trying to reassign the array pointer guess, resulting in an error. Not to mention incorrectly trying to reference *guess or using &des incorrectly. I suggest you read up on C pointer/array concepts.

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

char* helloValidation(char* des) {
    do {
        printf("Type 'hello' : ");
        scanf("%s", des);
    } while (strcmp(des, "hello") != 0);
    return des;
}

int main() {
    char guess[15];
    helloValidation(guess);
    return 0;
}
like image 105
Unsigned Avatar answered Oct 20 '25 08:10

Unsigned