-3

I am trying to return the value of a string from another function and store it as a variable. This is what I have written but I am having trouble resolving the issue.

#include "stdio.h"

void main() 
{
    printf("Hello World\n");
    char result[60];
    result = menuFunction();
}

const char* menuFunction() 
{
    return "Hello Again";
}
Sourav Ghosh
  • 127,934
  • 16
  • 167
  • 234

1 Answers1

2

In your code, result is array type, which is not a modifiable lvalue, hence cannot be used as LHS of assignment operator.

Quoting C11, chapter §6.5.16

An assignment operator shall have a modifiable lvalue as its left operand.

and chapter §6.3.2.1, (emphasis mine)

An lvalue is an expression (with an object type other than void) that potentially designates an object; 64) if an lvalue does not designate an object when it is evaluated, the behavior is undefined. When an object is said to have a particular type, the type is specified by the lvalue used to designate the object. A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type.

Solution: Define result as a pointer instead.

That said,

  • for a hosted environment, void main() is pretty much disallowed. You should be using int main(void) to conform to the standard.
  • using a function (call) before the function definition or a forward declaration in place, is also disallowed. You need to define the function beforehand or have a forward declaration of the same before you use it.
Sourav Ghosh
  • 127,934
  • 16
  • 167
  • 234