0

I am trying to print a char pointer returned by a function but the output is always gibberish.

I have tried changing the function type to char [] but this gives error. I have also tried casting the char [] to char * but this causes stack corruption. I noticed that if variable 'b' is printed within the function before the return statement, it displays correctly.

#include <stdio.h>
#include <string.h>

char *the()
{
    char a[100] = "Ace of Spades";
    char * b = &a[0];

    return b;
}
int main(void)
{

        printf("%s\n", the());

}

Expected Result:enter image description here Actual Result:enter image description here

jacobay43
  • 115
  • 1
  • 11

0 Answers0