Hi All, I want to ask about how to return the Char Array in Function, many thanks!!
Code:unsigned char[] FN() { unsigned char ret[35]; ret ="aabbccddee"; return ret } Main() { unsigned char abc[35]; abc= FN(); }
Hi All, I want to ask about how to return the Char Array in Function, many thanks!!
Code:unsigned char[] FN() { unsigned char ret[35]; ret ="aabbccddee"; return ret } Main() { unsigned char abc[35]; abc= FN(); }
1. You cannot return an array. You can return a pointer.
2. Returning a pointer to a non-static local array, or a pointer to an element of a non-static local array, is wrong because the array will cease to exist after the function returns.
My guess is that what you want to do would involve passing a pointer to unsigned char to the function, then copying the desired string literal to the array of unsigned char using that pointer.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I want to code like this,
but I can't, as I don't know the length of the output before pass it to the function FN(), how can I pass it as dynamic char array?Code:void FN(unsigned char output[]) { output="abcde"; } Main() { unsigned char output[100]; //<==============don't know the length of the char array unsigned char abc[35]; FN(output); printf("%s",output); }
Last edited by homoon; 04-19-2012 at 10:52 PM.
O_o
You can jump through the hoops of writing your own string type, download an existing one, or do as anyone else at your level would do and return a pointer freshly allocated by the relevant function. (`malloc'/`free')
Soma