Hi, Let me know if there is any thing wrong with this code snippet.
1. Will this code works fine every time?
2. Returning "ptr" will be dangling?
3. Where my "ptr" is stored? in Read-Only data segment/stack/some where else?
=============== =============== =============== ============
=============== =============== =============== ============
Result on GCC -
[feroz@buzz TestPgms]$ ./a.out
0x80495b4
0x80495b4
[feroz@buzz TestPgms]$
PS: If im not wrong we should not return a local variable/pointer from a function, so I made it a 'const' pointer.
1. Will this code works fine every time?
2. Returning "ptr" will be dangling?
3. Where my "ptr" is stored? in Read-Only data segment/stack/some where else?
=============== =============== =============== ============
Code:
#include <stdio.h> int a = 20; int *foo(void) { const int *ptr = NULL; ptr = &a; printf("%p\n",ptr); return (int *)ptr; } main() { int *x = NULL; x = foo(); printf("%p\n",x); }
Result on GCC -
[feroz@buzz TestPgms]$ ./a.out
0x80495b4
0x80495b4
[feroz@buzz TestPgms]$
PS: If im not wrong we should not return a local variable/pointer from a function, so I made it a 'const' pointer.
Comment