0

I'm getting output as "pqrs", But my understanding was we cannot return local pointer. I'm started to think since char *t = "pqrs" is string literal and it will be in read-only memory area, so we can return the local pointer — But not sure if my understanding is correct

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

char *t(char *s1)
{
    char *t = "pqrs";
    s1 = "fdsa";
    return t;
}

int main()
{
    char *s = "abcde";
    s = t(s);
    printf("%s \n",s);    
    return 0;
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
mark
  • 27
  • 2
  • 3
    You can always return local pointers. What you can't do is return a pointer to local data. But a string literal isn't local data. – Barmar Mar 17 '17 at 20:49
  • There is no "read-only" memory are in the standard. And it is not relevant here. Read about storgage duration. And whoever said you cannot return a local pointer is plain wrong. You cannot return **a pointer to an automatic variable** out of its scope. That's something very different. – too honest for this site Mar 17 '17 at 20:55

0 Answers0