strncpy到struct的char成员 [英] strncpy to a char member of struct

查看:76
本文介绍了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.

推荐答案

anars< an ************ @ gmail.com>写道:
anars <an************@gmail.com> wrote:


我在全球范围内初始化了这个结构:
struct riddle {
char * text;
....
}

下面,我主要用谜语结构调用的函数为arg。
定义:
void prepareRiddle(struct riddle * riddle)
//
//这里我得到一个已知大小的字符串(char * tmp)(int length)
包括终结者。
riddle-> text =(char *)malloc(length * sizeof(char)); //想想
sizeof(char)可能是!= 1在某些平台上
....
}

仍然,sizeof(riddle.text)调用main()没有给我我希望的数字。
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.




您可能想要使用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


啊,谢谢非常好。


顺便说一句,我在这篇文章中使用了Gmail网上论坛的海报,很抱歉,如果

我是顶级帖子。 :)只是测试这个东西。

Ahh, thank you very much.

Btw, I used the Gmail Groups online poster for this post, so sorry if
I''m top-posting. :) Just testing this thing out.


anars< an ************ @ gmail.com>写道:
anars <an************@gmail.com> wrote:
啊,非常感谢你。


欢迎您。

顺便说一下,我在这篇文章中使用了Gmail网上论坛的海报,很抱歉,如果我是上面张贴。 :)


好​​吧,看来你不是顶部或底部的帖子,因为你的上一篇

帖子不包含任何上下文。尝试引用您回复的

消息的相关部分,最好将您的答案置于

或之前的帖子之间。

只是测试这个东西。
Ahh, thank you very much.
You''re welcome.
Btw, I used the Gmail Groups online poster for this post, so sorry if
I''m top-posting. :)
Well, it seems you are neither top- or bottom posting, since your last
post did not include any context. Try to quote the relevant parts of the
message(s) you are replying to, preferrably putting your answers under
or inbetween the previous post.
Just testing this thing out.




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

(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成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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