I've got a .h file with:
JRAPI u64 list_directory_contents(char *directory);
In the .c file, I have a function that starts with:
u64 list_directory_contents (char *directory) {
And here is a piece of code:
// Convert directory name to wide-character string
u16 dir_address[268];
u64 dest;
int errorD = mbstowcs_s(&dest, dir_address, 268, directory, 268);
This works great, but I want to place this code in a new function.
In the .h file I make:
u16 get_absolute_path(char *directory);
And in the .c file I use:
u16 get_absolute_path(char *directory) {
u16 return_address;
u64 dest;
int errorDir = mbstowcs_s(&dest, return_address, 268, directory, 268);
return return_address;
}
That is initiated at the old place with:
u16 dir_address = get_absolute_path(directory);
And now it doesn't work.
Clang says I need to put a & infront of the return_address:
int errorDir = mbstowcs_s(&dest, &return_address, 268, directory, 268);
But then other problems start, and the value is not what it should be anymore and stops everything from working.
Placing this code in a function, what am I doing wrong?
u16 = Unsigned 16 bit integer, Unsigned short.
u64 = unsigned 64 bit integer, Unsigned long long.
When I declare the string as in the original code:
u16 return_address[268];
I get the following message from clang:
warning: address of stack memory associated with
local variable 'return_address' returned [-Wreturn-stack-address]
return return_address;
^~~~~~~~~~~~~~
Adding the [268] behind u16 return_address was the correct answer, but the error I get now is in a different part of the script that I have not addressed yet.
Take a look at the piece of code that works:
Specifically at
dir_address, then look at the code that doesn't work:Notice that you didn't declare
return_addressas an array like you did withdir_address. You could change this to:But then 1) what you're returning doesn't match the return type, and 2) if it does match, you're returning a pointer to a local variable.
Your options here are to either allocate the memory dynamically:
In which case the caller will need to
freethe memory, or have the caller pass in the buffer to fill: