奇怪的char *问题 [英] Bizarre char* problem

查看:72
本文介绍了奇怪的char *问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这个让我感到困惑。我有一个函数,

getParsedKey(char * key,char * returnString)。我传入了我想要的密钥,

它从数据结构中检索它并将值放在

returnString中。问题是returnString指向函数中正确的

值,但在函数完成后,它指向的字符串

为空。


.....

char * radioList = NULL;

ini> getParsedKey(" Radio",radioList); < br $> b $ b .....

bool iniparser :: getParsedKey(char * key,char * returnString)

{

//对于该部分中的所有键

for(int i = 0; i< numKeysInSection; i ++)

{

// returnString获取当前键的值

returnString = strtok(iniSection [i]," =");

//如果当前key是所需的键

if((strcmp(returnString,key)== 0))

{

//然后returnString接受键的价值

returnString = strtok(NULL," \ n");

MessageBox(NULL,returnString," returnString in

getParsedKey(2)",NULL);

//返回t他键的价值

返回true;

}

}


MessageBox(NULL ,KEY NOT FOUND,OK,NULL);

//如果找不到密钥,则返回NULL

返回false;

}


因此,为了澄清,radiolist被传递给getParsedKey,它发现了

键无线电。并将与无线电相关的值放在指针中。

当它在函数中时,指针(returnString)指向该值,因为它应该是值b / b
。但是,函数完成后,

指针radioList指向一个空字符串。这让我完全感到困惑。我希望我在代码的某个地方犯了一个noob错误,并且有人可以向我指出它,因为我一直在盯着

在这个时候捅了好几个小时,现在都无济于事。


有什么建议吗?


干杯,
Aaron Brown


PS:我在Visual Studio 2005 IDE中工作

解决方案

th*********@gmail.com 写道:

好吧,这个让我感到困惑。我有一个函数,
getParsedKey(char * key,char * returnString)。我传入了我想要的密钥,它从数据结构中检索它并将值放在
returnString中。问题是returnString指向函数中正确的值,但在函数完成后,它指向的字符串
为空。

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

bool iniparser :: getParsedKey( char * key,char * returnString)
//
//
for(int i = 0; i< numKeysInSection; i ++)
{
// returnString获取当前键的值
returnString = strtok(iniSection [i]," =");


你在这里更改* local *指针。这个动作与您传入的变量无关。

//如果当前键是所需的键
if((strcmp(returnString) ,键)== 0))
//然后returnString接受键'的值
returnString = strtok(NULL," \ n");


再次......

MessageBox(NULL,returnString," getString in
getParsedKey(2)",NULL);


你在这里的文字中有''换行符'...

//返回键的值
返回true;
}


MessageBox(NULL,KEY NOT FOUND,OK,NULL);
//如果密钥不是发现,返回NULL
返回false;
}
因此,为了澄清,radiolist被传递给getParsedKey,它发现了
键Radio。并将与无线电相关的值放在指针中。
当它在函数中时,指针(returnString)指向该值。但是,函数完成后,指针radioList指向一个空字符串。这让我完全感到困惑。


怎么样:


foo(char const * blah)

{

blah =" DEF";

}


#include< stdio.h>

int main()

{

const char * blah =" ABC" ;;

printf(blah);

}


?令人困惑的是什么?

我希望我在代码的某个地方犯了一个noob错误,而且有人可以向我指出它,因为我曾经
盯着这个,现在捅了好几个小时,没有
有用。

有什么建议吗?


不要使用普通指针。或者通过引用传递第二个参数。

干杯,
Aaron Brown

PS:我在Visual Studio 2005 IDE中工作




如果您需要VC ++特定的解决方案,您可能希望发布到

''microsoft.public.vc.language''新闻组。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


th ********* @ gmail.com 写道:

好的,这个让我感到困惑。我有一个函数,
getParsedKey(char * key,char * returnString)。我传入了我想要的密钥,它从数据结构中检索它并将值放在
returnString中。问题是returnString指向函数中正确的
值,但在函数完成后,它指向的字符串
为空。



看看以下计划。你认为main()

中的i值在调用func()之后会是什么?


void func(int n)

{

n = 3;

}

int main()

{

int i = 0;


func(i);


返回0;

}


Brian


< th ********* @ gmail.com>写道:

...我有一个函数,
getParsedKey(char * key,char * returnString)。我传入了我想要的密钥,它从数据结构中检索它并将值放在
returnString中。问题是returnString指向函数中正确的值,但是在函数完成后,它指向的字符串
是空的。
...
bool iniparser :: getParsedKey(char * key,char * returnString)
{
...
returnString = strtok(NULL," \ n");
...
}
...
有什么建议吗?




您只修改了returnString的本地副本。


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],"=");
//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");
MessageBox(NULL,returnString,"returnString in
getParsedKey(2)",NULL);
//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. 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

解决方案

th*********@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


th*********@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.


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


<th*********@gmail.com> wrote:

... 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.
...
bool iniparser::getParsedKey(char *key, char *returnString)
{
...
returnString = strtok(NULL,"\n");
...
}
...
Any suggestions?



You are modifying only a local copy of returnString.


这篇关于奇怪的char *问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆