Function returns char array that is assigned to another char array

I am trying to make several calls to a function that returns a char array and have the returned char array assigned to a second char array.

Happy to provide code if necessary but I think I am just missing a syntax trick.

In setup:

strcpy (cSSID, get_config_value ( SSIDFilename );

Function:

char * get_config_value ( ConfigFilename )
char ConfigValue;
// Open file and get config data (ConfigValue)
return ConfigValue;

This seems logical to me but nothing is assigned to the char array variable cSSID when I do this.
I need to get four different config values for setting up wifi connection to server so I can save some memory by doing all the file reading in a function.

That's a single, local char, not the char pointer you promised to return.

You don't even want to return a pointer to that local variable, because it is about to go out of scope.

A better approach would be to pass the buffer as a parameter to your function

char cSSID[20]; // or whatever size you need
get_config_value ( SSIDFilename, "PARAM", cSSID );

Function:

bool get_config_value ( const char *ConfigFilename, const char *param, char *buffer )
// Open file and get config data and load it directly into buffer
return true;  // we successfully loaded the value

It is left as an exercise for you to decide if you want to also pass the size of buffer and make sure you don't try to put too many chars into your buffer.

Thank you to both respondents.

blh64 I have a few questions.

I don't understand what the "PARAM" is about.

Can I assign values to variables passed as arguments in the call (like a byRef argument in VB).

So, in my function, I would just loop through the characters in the file in the usual serial manner and assign them to buffer[charNr].

I have lots of programming experience but little of it involved C in any form so I struggle with some of these character-handling requirements.

as AWOL said, the code is incorrect.

looks like your get_config_value() is expected to copy the contents of a file into a string and then the code separately copies that string to another string, cSSID.

why not pass a ptr to the destination string and have get_config_value() read the contents of the file directly into it?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.