4

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 
Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
Muaz Usmani
  • 1,298
  • 6
  • 26
  • 48

4 Answers4

9

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;
}
Unsigned
  • 9,640
  • 4
  • 43
  • 72
  • By your argument, no method should be void. It's a ridiculous argument. This method does not need to return des; it only does because of the OP's initial confusion. That strcpy returns it's argument is irrelevant, a bit of ancient standardized history. – Jim Balter Jun 27 '12 at 17:01
  • 3
    @JimBalter - Where did you get the idea no method should be void? Regardless, I was not trying to rewrite the OP's code, merely correct mistakes. [This question](http://stackoverflow.com/q/3561427/629493) and [answer](http://stackoverflow.com/a/3561465/629493) have excellent examples of situations in which returning an argument can be quite useful (such as eliminating the need for extraneous temporary variables). – Unsigned Jun 28 '12 at 15:04
  • The "idea" is an implication of your argument; more so than the implication you drew from mine. And again, the OP returning des was a result of confusion, so when eliminating confusion it would make sense to eliminate the return, which is now pointless. As for the example, calling it "excellent" is pushing it, esp. since the answer itself says otherwise, and `char* s; strcpy(s = malloc(10), "test")` works fine. Feel free to argue further by yourself; i'm out of here. – Jim Balter Jun 28 '12 at 19:01
  • Just one more thing: The comment by R.. there is most relevant -- as is widely recognized, strcpy's return value is a botch; it should have returned a pointer to the terminating NUL. – Jim Balter Jun 28 '12 at 19:04
3

The scanf statement is incorrect! It should read as:

scanf("%s", des);
t0mm13b
  • 34,087
  • 8
  • 78
  • 110
1

you can't assign it back to guess, and in your case, you don't have to as you are passing guess into the function ( get rid of that * )

So the function is going to change guess ( not a copy of it ) so no need to try and assign it back

helloValidation(guess);
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
-1

since you're passing guess into the helloValidation function and using it in a scanf, there's no need to return it and reassign it back to the char[] reference.

Replace

guess = helloValidation(*guess);

with

helloValidation(*guess);

You're getting the error because the guess reference has already been allocated on the stack. You can't write over it. Had guess been a pointer, you could write over the value in the pointer, however that doesn't touch the memory that's there.

mj_
  • 6,297
  • 7
  • 40
  • 80