strncpy到struct的char成员

strncpy到struct的char成员

本文介绍了strncpy到struct的char成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在全球范围内初始化了这个结构:

struct riddle {

char * text;

....

}


下面,我主要用谜语结构调用的函数为arg。是
定义:

void prepareRiddle(struct riddle * riddle)

{

//这里我得到一个具有已知大小的字符串(char * tmp)(int length)

incl。终结者。

riddle-> text =(char *)malloc(length * sizeof(char)); //想知道

sizeof(char)可能是!= 1在某些平台上

....

}


仍然,在main()中调用sizeof(riddle.text)并没有给我数字

我希望。


我是否不恰当地访问我的谜语结构中的文本成员?


谢谢。

Hi,

I have this struct initialized globally:
struct riddle {
char *text;
....
}

below, the function I call in main with the riddle struct as arg. is
defined:
void prepareRiddle(struct riddle *riddle)
{
// here I get a string (char *tmp) with a known size (int length)
incl. terminator.
riddle->text = (char*)malloc(length * sizeof(char)); // figured
sizeof(char) could be != 1 on some platforms
....
}

still, sizeof(riddle.text) called in main() doesn''t give me the numbers
I had hoped for.

Am I accessing the text member in my riddle struct inappropriately?

Thanks.

推荐答案




您可能想要使用strlen()函数而不是sizeof ()

运营商。 sizeof()给出了对象的大小,在这个

的情况下不是字符串的文本,而是指向char的指针。

-

:wq

^ X ^ Cy ^ K ^ X ^ C ^ C ^ C ^ C



You probably want to use the strlen() function instead of the sizeof()
operator. sizeof() gives you the size of the object, which is in this
case not the text of the string, but a pointer to char.
--
:wq
^X^Cy^K^X^C^C^C^C






有一些新闻组特别用于测试目的,

(alt.testing。*),随意测试你想要的东西。


-

:wq

^ X ^ Cy ^ K ^ X ^ C ^ C ^ C ^ C



There are some newsgroups especially ment for testing purposes,
(alt.testing.*), feel free to test as much as you want there.

--
:wq
^X^Cy^K^X^C^C^C^C


这篇关于strncpy到struct的char成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 23:27