Papers/programming

pass stack array data to pointer

tomato13 2013. 12. 16. 17:17

char* testFunc14()

{

    char arrStr[5] = "aaaa";


    return arrStr;

}


void tc14()

{

    char* pStr = testFunc14();


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

}


Do you think the upper code works fine?

It will not work. 

For arrStr is useless out of testFunc14(). 

So, pStr has no data. 


By the way, how about the case below?


void testPrint(char* pStr)

{

    printf(pStr);

}


int tc17()

{

    char arrStr[5] = "aaaa";

    testPrint(arrStr);


    return 1;

}


It works fine. In other words, in case of an inner function, stack array is valid.