问题描述
问候,
我创建了以下类型:
typedef struct _tbl_certificate {
int cert_id;
char * cert_name;
} tbl_certificate;
typedef struct _cameraObj {
tbl_camera camera;
tbl_certificate *证书;
} cameraObj;
我在cameraObj结构中访问证书数组时遇到问题。
例如。 cameraObj * camera;
camera-> certificate [0] .cert_id = 7;
这不想工作。任何建议或想法将不胜感激!
谢谢
Greetings,
I have created the following types:
typedef struct _tbl_certificate{
int cert_id;
char *cert_name;
}tbl_certificate;
typedef struct _cameraObj{
tbl_camera camera;
tbl_certificate *certificate;
}cameraObj;
I am having trouble accessing the certificate array in the cameraObj struct.
eg. cameraObj *camera;
camera->certificate[0].cert_id = 7;
This doesn''t want to work. Any suggestions or ideas would be greatly appreciated!
Thanks
推荐答案
您只需要创建一个指向cameraObj类型变量的指针,其中
并没有指向你可以使用的任何东西。所以你首先要为malloc()
结构内存。但是你还没有完成,因为现在
''证书''你的结构成员仍然指向一些随机的
内存位置,所以你再次必须为结构分配内存
它应该指向。然后你可能仍然需要为该结构的''cert_name''成员分配内存
...
问候,Jens
-
\ Jens Thoms Toerring ___
\ __________________________
嗯,错误不在你发布的内容 - 那部分没问题,除了
下划线。据推测,错误在你的代码部分你没有发布; b $ b没有发布;或者也许它是你甚至不写的部分,
但应该有。你确实记得为所有那些
指针分配内存指向,是吗?
如果你需要更多帮助,你必须帮忙我们帮助你准确描述
出了什么问题; 这不起作用是我希望来自我的
用户的错误报告,而不是来自其他程序员。将你的代码减少到演示问题的最小例子,但仍然是可编辑的,
并发布。
Richard
Well, the error is not in what you posted - that part is OK, except for
the underscore. Presumably, the error is in the part of your code you
did not post; or perhaps it is in the part that you didn''t even write,
but should have. You did remember to allocate memory for all those
pointers to point at, did you?
If you need more help, you''ll have to help us help you. Describe exactly
what goes wrong; "this doesn''t work" is an error report I expect from my
users, not from a fellow programmer. Whittle down your code to the
smallest example that demonstrates the problem, but is still compilable,
and post that.
Richard
首先,摆脱标识符中的前导_,这不是
合法供您使用。然后定义tbl_camera是什么。然后
将你的代码修改为:
cameraObj * camera;
...
if(!(camera = malloc(sizeof * camera))){
退出(EXIT_FAILURE);
}
else if(! (相机 - >证书=
malloc(sizeof *(相机 - >证书))){
退出(EXIT_FAILURE);
}
else {
camera-> certificate.cert_id = 7;
}
并且您可能需要另一个术语来分配certificate.name。
将退出调用替换为您想要处理的任何内容,例如内存缺少
。
-
Chuck F(cb********@yahoo.com)(cb ******** @ worldnet.att.net)
可用于咨询/临时嵌入式和系统。
< http://cbfalconer.home.att.net>使用worldnet地址!
First, get rid of the leading _ in identifiers, which are not
legal for you to use. Then define whatever a tbl_camera is. Then
revise your code to something like:
cameraObj *camera;
...
if (!(camera = malloc(sizeof *camera))) {
exit(EXIT_FAILURE);
}
else if (!(camera->certificate =
malloc(sizeof *(camera->certificate))) {
exit(EXIT_FAILURE);
}
else {
camera->certificate.cert_id = 7;
}
and you may need another term to allocate certificate.name.
Replace the exit calls with whatever you want to handle such
memory lacks.
--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
这篇关于揭示我的C结构(自定义类型)的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!