0
Cmd* GetCommand() {
            Cmd* command;
            char* cmdStr = getIn();
            command = parseL(cmdStr);
            return command;
   }

Cmd** parseL(char* str){
        Cmd** command;
        char* token;
        char str2[CMD_MAX_LINE_LENGTH];
        strcpy(str2, str);
        token = strtok(str2, DELI);
        command = ParC(token);
        return command;
}

Cmd* parC(char* cmdStr) {
    Cmd* command = calloc(1, sizeof(CmdCommand));
        if (cmdStr == NULL) {
            command->cmd = INVALID;
            return command;
        }
        else
            parse2C(cmdStr, command);
        return command;
    }

Hey, I'm new to C and got a problem in my code.

When I'm running this part of code I get an error:

assignment makes pointer from integer without a cast.

for this line:

             command = parseL(cmdStr);

command is from type Cmd* and also the function parseL returns Cmd*, so I can't find out the problem.

alk
  • 69,737
  • 10
  • 105
  • 255
user2026
  • 61
  • 1
  • 11
  • 2
    Order of declarations matters in C. Define your functions before you use them. – Carl Norum Sep 28 '18 at 00:19
  • 1
    you forgot to include the line number of your error! Sure, a smart person could probably spot it, but it's best to include the entire error. It will give you a line number. – erik258 Sep 28 '18 at 00:19
  • Incidentally, if you need to call functions from each other, you can't define one before the other, so you'll want to split function declaration and definition (https://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration). I don't think that's the case for you, but putting all the declarations atop will make the definition order unimportant. – erik258 Sep 28 '18 at 00:21
  • 1
    You ignore another warnings . Never ignore the warning – 0___________ Sep 28 '18 at 01:01
  • "*the function parseL return Cmd**" from the code you show it does not. – alk Sep 28 '18 at 12:18
  • 2
    Whatever you do, **do not add a cast** to suppress the (badly worded) warning. – pmg Sep 28 '18 at 13:43

1 Answers1

1

The moment the compiler sees

 command = parseL(cmdStr);

it does not know yet which type is returned by parseL(), as it is define later in the code.

For such cases the C Standard defines to many compilers assume int as return type.

As command is defined to be a pointer, the compiler chokes and issues the warning observed:

error assignment makes pointer from integer without a cast

To get around this issue

  • either define the whole function
  • or at least provide a prototype of the function

    Cmd** parseL(char*);
    

before it is used.


As a side note:

You will run into the next error then as Cmd** is not the same as Cmd*.

alk
  • 69,737
  • 10
  • 105
  • 255
  • Actually, the current standard, the previous standard, and the one prior to that all do _not_ assume `int`; they say 'error'. Many compilers continue to allow the 'default `int`', but that isn't what any of the standards except the original C90 (C89) standard permits. All other versions require the type to be known before the function is called. You don't necessarily have to have a full prototype; you do have to have a declaration (or definition) for every function called before it is called. – Jonathan Leffler Sep 28 '18 at 17:37
  • @JonathanLeffler: Thank you for taking care ... :} – alk Sep 29 '18 at 17:51