Hello and I'm sorry I don't speak English well.
I know this is repetitive But I Have a problem and i've never found the answer to my problem.
Shows this error whenever I compile this code by GCC : code :
const char* myFunction()
{
static char array[] = "my string";
return array;
}
char a[101];
a=myFunction();
Error :
StackOverFlow.c: In function ‘main’:
StackOverFlow.c:51:2: error: assignment to expression with array type
16 | a=myFunction();
| ^
When I use the return value of the function directly within printf Function, it doesn't make an error.
printf ("%s", myFunction());
i dont know what's it's problem. sorry for Bad English.
CodePudding user response:
That's because, in C, this is the same as assigning the pointer to array
to a
, but you have already "initialized" a
.
I don't know what you're try to achieve, but if you want to copy the string, use
strcpy(a, myFunction());
or, if you want the pointer, try
char *a;
a=myFunction(); //If you try using printf with a it will work.