揭示我的C结构(自定义类型)的问题 [英] Problems dereferensing my C structures (custom types)

查看:67
本文介绍了揭示我的C结构(自定义类型)的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,

我创建了以下类型:

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

推荐答案

Franz< fs ****** @ msn.com>写道:
Franz <fs******@msn.com> wrote:
我创建了以下类型:
typedef struct _tbl_certificate {


无需预先加下下划线。永远不要在变量

或函数名称的开头使用它们,你可能会与

使用的变量发生冲突。这里真的没有必要,有点像


typedef struct my_name my_name;


效果非常好。

int cert_id;
char * cert_name;
} tbl_certificate;
typedef struct _cameraObj {
tbl_camera camera;
tbl_certificate * certificate;
} cameraObj;
我在cameraObj结构中访问证书数组时遇到问题。
例如。 cameraObj * camera;
camera-> certificate [0] .cert_id = 7;
这不想工作。任何建议或想法都会非常感激!
I have created the following types: typedef struct _tbl_certificate{
No need to prepend an underscore. Never use them at the start of variable
or function names, you might get into conflicts with variables used by
your inplementation. And here''s really no need for it, someting like

typedef struct my_name my_name;

works perfectly well.
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!




您只需要创建一个指向cameraObj类型变量的指针,其中

并没有指向你可以使用的任何东西。所以你首先要为malloc()

结构内存。但是你还没有完成,因为现在

''证书''你的结构成员仍然指向一些随机的

内存位置,所以你再次必须为结构分配内存

它应该指向。然后你可能仍然需要为该结构的''cert_name''成员分配内存

...


问候,Jens

-

\ Jens Thoms Toerring ___ Je ***********@physik.fu-berlin.de

\ __________________________ http://www.toerring.de


fs ****** @ msn.com (Franz)写道:
fs******@msn.com (Franz) wrote:
typedef struct _tbl_certificate {
int cert_id ;
char * cert_name;
} tbl_certificate;


两个注释:

*以_开头的标识符名称保留供文件范围内的

实现使用。该声明可能在文件

范围内。因此,_tbl_certificate不是该结构的合法名称。

此外,拥有一个struct tbl_certificate,

typedef'到tbl_certificate是完全合法的; struct name在不同的命名空间中

来自普通标识符。

*有些人会建议你不要在这种情况下使用typedef

all 。你只能获得几次击键,而且你会失去透明度。

OTOH,你获得了一些数据隐藏,这在某些情况下可能会很好。

情况。

因此声明为

typedef struct tbl_certificate {

int cert_id;

char * cert_name;

} tbl_certificate;

甚至只是简单地作为
struct tbl_certificate {

int cert_id;

char * cert_name;

};


(以__或_开头的标识符和大写字母保留

,无论范围如何,btw执行,btw我发现它更实际

实际上永远不会用_开头一个名字,不管怎么说它/ b $ b恰好是允许的。)

typedef struct _cameraObj {
tbl_camera camera;
tbl_certificate * certificate;
} cameraObj;


当然在这里同上。

我在cameraObj结构中访问证书数组时遇到问题。

例如。 cameraObj * camera;
camera-> certificate [0] .cert_id = 7;

这不想工作。任何建议或想法将不胜感激!
typedef struct _tbl_certificate{
int cert_id;
char *cert_name;
}tbl_certificate;
Two notes:
* identifier names starting with _ are reserved for use by the
implementation, at file scope. This declaration probably is at file
scope. Therefore, _tbl_certificate is not a legal name for that struct.
Besides, it is perfectly legal to have a struct tbl_certificate,
typedef''ed to tbl_certificate; struct names are in a different namespace
from normal identifiers.
* some people would advise you not to use typedef in this situation at
all. You only gain a couple of keystrokes, and you lose transparence.
OTOH, you gain some data hiding, which may be good in some
circumstances.
So declare this as
typedef struct tbl_certificate {
int cert_id;
char *cert_name;
} tbl_certificate;
or even simply as
struct tbl_certificate {
int cert_id;
char *cert_name;
};

(Identifiers beginning with __ or _ and a capital letter are reserved
for the implementation regardless of scope, btw. I find it more
practical never to start a name with _ anyway, and never mind where it
happens to be allowed.)
typedef struct _cameraObj{
tbl_camera camera;
tbl_certificate *certificate;
}cameraObj;
Ditto here, of course.
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!




嗯,错误不在你发布的内容 - 那部分没问题,除了

下划线。据推测,错误在你的代码部分你没有发布; 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


Franz写道:

我创建了以下类型:

typedef struct _tbl_certificate {
int cert_id;
char * cert_name;
} tbl_certificate;

typedef struct _cameraObj {
tbl_camera camera;
tbl_certificate * certificate;
} cameraObj;

我无法访问
cameraObj结构中的证书数组。

例如。 cameraObj * camera;
camera-> certificate [0] .cert_id = 7;

这不想工作。任何建议或想法都将非常感谢!

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!




首先,摆脱标识符中的前导_,这不是
合法供您使用。然后定义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结构(自定义类型)的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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