0

I try to create a function, which returns astring (char array);

I tried different ways, but I always got error like:

[Error] conflicting types for 'readCommand'

Code:

char* cmd; //char cmd[256]
*cmd = readCommand();

//function
char* readCommand()
{
    char cmd[256];
    fgets(cmd, sizeof(cmd), stdin);
    return cmd;
}

How can I return a string from this function?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Leonid Zolotarov
  • 191
  • 1
  • 10
  • 1
    If `cmd` is not declared with the `static` qualifier, trying to access the returned value from `readCommand` will be undefined behavior – Govind Parmar Dec 12 '18 at 16:32
  • @GovindParmar i tried 'static char cmd[256];' bu the result the same. What I did wrong? Thank you! – Leonid Zolotarov Dec 12 '18 at 16:34
  • @LeonidZolotarov The error comes from `*cmd = readCommand();` instead of `cmd = readCommand();`, but you're doing it wrong anyway, read the duplicates. – Jabberwocky Dec 12 '18 at 16:38

0 Answers0