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.

6
  • 2
    Order of declarations matters in C. Define your functions before you use them. Commented Sep 28, 2018 at 0: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. Commented Sep 28, 2018 at 0: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 (stackoverflow.com/questions/1410563/…). I don't think that's the case for you, but putting all the declarations atop will make the definition order unimportant. Commented Sep 28, 2018 at 0:21
  • 1
    You ignore another warnings . Never ignore the warning Commented Sep 28, 2018 at 1:01
  • 2
    Whatever you do, do not add a cast to suppress the (badly worded) warning. Commented Sep 28, 2018 at 13:43

1 Answer 1

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*.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
@JonathanLeffler: Thank you for taking care ... :}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.