473,657 Members | 2,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bizarre char* problem

Okay, this one has me totally baffled. I have a function,
getParsedKey(ch ar* 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",radioLis t);
.....

bool iniparser::getP arsedKey(char *key, char *returnString)
{
//for all the keys in the section
for (int i = 0; i < numKeysInSectio n; i++)
{
//returnString takes the value of the current key
returnString = strtok(iniSecti on[i],"=");
//if the current key is the desired key
if ((strcmp(return String,key) == 0))
{
//then returnString takes on the key's value
returnString = strtok(NULL,"\n ");
MessageBox(NULL ,returnString," returnString in
getParsedKey(2) ",NULL);
//return the key's value
return true;
}
}

MessageBox(NULL ,"KEY NOT FOUND","OK",NUL L);
//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. 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?

Cheers,
Aaron Brown

P.S: I'm working in the Visual Studio 2005 IDE

Jun 15 '06 #1
26 2316
th*********@gma il.com wrote:
Okay, this one has me totally baffled. I have a function,
getParsedKey(ch ar* 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",radioLis t);
....

bool iniparser::getP arsedKey(char *key, char *returnString)
{
//for all the keys in the section
for (int i = 0; i < numKeysInSectio n; i++)
{
//returnString takes the value of the current key
returnString = strtok(iniSecti on[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(return String,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",NUL L);
//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.publ ic.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
Jun 15 '06 #2
th*********@gma il.com wrote:
Okay, this one has me totally baffled. I have a function,
getParsedKey(ch ar* 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.

Look at the follow program. What do you think the value of i in main()
will be after the call to func()?

void func(int n)
{
n = 3;
}
int main()
{
int i = 0;

func(i);

return 0;
}

Brian
Jun 15 '06 #3
<th*********@gm ail.com> wrote:
... I have a function,
getParsedKey(c har* 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.
...
bool iniparser::getP arsedKey(char *key, char *returnString)
{
...
returnString = strtok(NULL,"\n ");
...
}
...
Any suggestions?


You are modifying only a local copy of returnString.
Jun 15 '06 #4

Default User wrote:
th*********@gma il.com wrote:
Okay, this one has me totally baffled. I have a function,
getParsedKey(ch ar* 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.

Look at the follow program. What do you think the value of i in main()
will be after the call to func()?

void func(int n)
{
n = 3;
}
int main()
{
int i = 0;

func(i);

return 0;
}


5?

Jun 15 '06 #5
You're all quite correct. I modified it so that, instead of passing in
a pointer by value I had it return a pointer to the correct string.
This, unfortunately, didn't solve the problem.

....
char* radioList = ini->getParsedKey(" Radio");
....

char* iniparser::getP arsedKey(char* keyName)
{
char* tempString = new char[MAX_INI_LINE_LE NGTH];
for(int i = 0; i < strlen(keyName) ; i++)
{
tolower(keyName[i]);
}

//for all the keys in the section
for (int i = 0; i < numKeysInSectio n; i++)
{
//tempString takes the value of the current key
tempString = strtok(iniSecti on[i],"=");
//if the current key is the desired key
if ((strcmp(tempSt ring,keyName) == 0))
{
//then tempString takes on the key's value
tempString = strtok(NULL,"\n ");
//return the key's value
return tempString;
}
}

MessageBox(NULL ,"KEY NOT FOUND","OK",NUL L);
//if the key isn't found, return NULL
return NULL;
}

Now, correct me if I'm wrong, but that should fix the pass-by-value
problem of the previous version of this function, right? I mean, I'm
passing back an address to a character string. That character string
exists inside the function, but disappears once the function has
returned the pointer.
Roberto Waltman wrote:
<th*********@gm ail.com> wrote:
... I have a function,
getParsedKey(c har* 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.
...
bool iniparser::getP arsedKey(char *key, char *returnString)
{
...
returnString = strtok(NULL,"\n ");
...
}
...
Any suggestions?


You are modifying only a local copy of returnString.


Jun 15 '06 #6
Noah Roberts wrote:

Default User wrote:

Look at the follow program. What do you think the value of i in
main() will be after the call to func()?

void func(int n)
{
n = 3;
}
int main()
{
int i = 0;

func(i);

return 0;
}


5?

You may want to look into another occupation. ;)


Brian
Jun 15 '06 #7
th*********@gma il.com wrote:
You're all quite correct. I modified it so that, instead of passing
in a pointer by value I had it return a pointer to the correct string.
This, unfortunately, didn't solve the problem.
Don't top-post. Your replies belong following or interspersed with
trimmed quotes.

...
char* radioList = ini->getParsedKey(" Radio");
...

char* iniparser::getP arsedKey(char* keyName)
{
char* tempString = new char[MAX_INI_LINE_LE NGTH];
for(int i = 0; i < strlen(keyName) ; i++)
{
tolower(keyName[i]);
}


You are attempting to modify a string literal. That causes undefined
behavior.

Is there a reason you aren't using std::string?


Brian
Jun 15 '06 #8
just change function prototype to

char* iniparser::getP arsedKey(char[] keyName) ;

This will solve your problem

Regards
mangesh

Jun 16 '06 #9
mangesh schrieb:
just change function prototype to

char* iniparser::getP arsedKey(char[] keyName) ;

This will solve your problem


Please quote what yuo are referring to.

The original prototype is
char* iniparser::getP arsedKey(char* keyName);


So what problem will the char[] solve? Its functionally equal.

Thomas
Jun 16 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2159
by: Alan Little | last post by:
This is very bizarre. Could someone else have a look at this? Maybe you can see something I'm overlooking. Go here: http://www.newsletters.forbes.com/enews/admin/deliver.php4 U: bugtest P: test Enter 1 for "How many files in this delivery?" and select a file to upload, in one of the listed formats. Scroll down and click "Test Email".
8
1545
by: Al Reynolds | last post by:
Afternoon, I have just finished fixing one of my scripts after it started generating odd errors on IE6 on WinXP Service Pack 2. For info, the IE Version is: 6.0.2900.2180.xpsp_sp2_rtm.040803-2158 I have done all the Windows critical updates. I haven't managed to replicate the behaviour in any other browser. I have tested the faulty version in Netscape 4.7x, IE 5
8
2549
by: Snis Pilbor | last post by:
First, let me announce that this is very possibly off-topic because malloc is a specific third party accessory to c, etc. I spent about an hour trying to find a more appropriate newsgroup and failed. If anyone could point one out, I would be much obliged. I'm using MSVC .NET, but the only .NET specific newsgroups I could find were vbasic newsgroups and you'll agree a C malloc question is less off-topic here than there, at least. I...
1
2690
by: zoehart | last post by:
I'm working with VBScript to build a text email message. I'm seeing a variety of bizarre formatting issues. The following lines of code MT = MT & vbCrLf & "Card Type: " & CardType MT = MT & vbCrLf & "Credit Card Number: " & objBOWallet.Encrypt(Payment.) If Not IsNull(Payment.) Then MT = MT & vbCrLf & "Expiration Month: " & Payment. End If If Not IsNull(Payment.) Then MT = MT & vbCrLf & "Expiration Year: " & Payment. End If
3
1600
by: foahchon | last post by:
Hi, I'm trying to write a simple program (or portion of a program), that will re-prompt a user for input each time the user enters invalid input, and then exit once the user has entered valid input. Here's what I have so far in main(): int main() { char input; cout << "Enter a string: "; while (!InputString(input)) {
0
371
by: ckfan.painter | last post by:
I've run into a seemingly bizarre problem with insert() for std::vector. (This was done on Microsoft Visual C++ 2005 express version 8...maybe it is a compiler specific bug?) Here's the code: //=================== // vector tester 3.cpp : main project file.
3
1882
by: Peter | last post by:
Hi! I am having some very strange behavior with my databound controls. It's taken a long time to isolate exactly what is provoking the problem, but I'm still leagues away from solving it. I have a DataView which filters a DataSet. Bound to this dataview is a ListBox, via its DataSource property. The DisplayMember is the name property of the row. Simple enough so far?
7
1853
by: bajichuan | last post by:
Hello! I have the world's strangest linking error, and I'm hoping that someone can help me sort it out. I recently installed and compiled a library called LinBox without a problem. I have an object-oriented software application, and I want it to call the library. When I add the following 3 lines (copied directly from the library tutorial), #include <linbox/field/modular.h> using namespace LinBox; typedef Modular<shortField;
8
3126
by: =?Utf-8?B?TWFyaw==?= | last post by:
We've got a wierd failure happening on just one machine. One part of our product uses a 3rd party search implementation (dtSearch). DtSearch has a native core (dten600.dll), late-bound, and a managed wrapper (dtSearchNetApi2.dll). For reasons unknown our build and msi packaging process includes dtSearchNetApi2.dll but not dten600.dll in all packages, as well as a couple of assemblies that reference it, even though they are not used by...
0
8385
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8821
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8502
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8602
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7316
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.