In C programming language, Is it correct to return a local char[] variable by converting it to const char *? -


i understand bad return local variable function stored in stack segment. if convert char[] const char *, return it, considered practice? if so, segment const char * stored in?

something this:

const char * foo() {     char str[32];     sprintf(str, "hello");     return (const char *)(str); } 

casting const char* nothing. still points str value, becomes invalid when return because it's on stack.

adding const makes whatever uses return value can't modify data pointed pointer (unless cast off const). since points returned function's stack space, once foo returns, access pointer undefined behavior, point moot.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -