+ 1

Isn't the character array variable name a pointer to array in C ?

Why there is an error while running this code : "function returns address of local variable". Wasn't ch (See the code) a pointer to the character array, and hence we are returning the starting address of the array. Can anyone help me out : CODE : #include<stdio.h> char * function() { char ch[10] = "Thanks"; return ch ; } int main() { char * point = function(); printf("%s",point); return 0 ; }

25th Jun 2019, 7:55 AM
cool_boy12344
cool_boy12344 - avatar
4 Respostas
0
~ swim ~ If the array in the function gets erased after the function returns to the main(). Then even if i store the address of the first element (ch[0]) in a character pointer ie. "char * ptr" and return ptr, it should also point to garbage but the program runs fine. Any explanations ?
25th Jun 2019, 5:00 PM
cool_boy12344
cool_boy12344 - avatar