Re: Bizarre char* problem
the.tarquin@gmail.com wrote:
Okay, this one has me totally baffled. I have a function,
getParsedKey(char* key, char* returnString). I pass in the key I
want, it retrieves it from a data structure and puts the value in
returnString. The problem is that returnString points to the correct
value in the function, but after the function has finished, the string
that it points to is empty.
....
char* radioList = NULL;
ini->getParsedKey("Radio",radioList);
....
bool iniparser::getParsedKey(char *key, char *returnString)
{
//for all the keys in the section
for (int i = 0; i < numKeysInSection; i++)
{
//returnString takes the value of the current key
returnString = strtok(iniSection[i],"=");
You change the *local* pointer here. This action has nothing to do
with the variable that you passed in.
//if the current key is the desired key
if ((strcmp(returnString,key) == 0))
{
//then returnString takes on the key's value
returnString = strtok(NULL,"\n");
Again...
MessageBox(NULL,returnString,"returnString in
getParsedKey(2)",NULL);
You have 'newline' in a literal here...
//return the key's value
return true;
}
}
MessageBox(NULL,"KEY NOT FOUND","OK",NULL);
//if the key isn't found, return NULL
return false;
}
So, to clarify, radiolist gets passed to getParsedKey, which finds the
key "Radio" and puts the value assosciated with radio in the pointer.
While it's in the function, the pointer (returnString) points, as it
should, to the value. After the function has finished, however, the
pointer radioList points to an empty string. This has me totally
baffled.
How about this:
foo(char const * blah)
{
blah = "DEF";
}
#include <stdio.h>
int main()
{
const char * blah = "ABC";
printf(blah);
}
? Confusing as well?
I'm hoping I've made a noob mistake somewhere in the code
and that someone can kindly point it out to me, because I've been
staring at this and poking at it for many, many hours now, to no
avail.
Any suggestions?
Don't use plain pointers. Or pass the second argument by reference.
Cheers,
Aaron Brown
P.S: I'm working in the Visual Studio 2005 IDE
If you need a VC++-specific solution, you might want to post to
'microsoft.public.vc.language' newsgroup.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask