Re: Problems dereferensing my C structures (custom types)

From: Richard Bos (rlb_at_hoekstra-uitgeverij.nl)
Date: 02/17/04


Date: Tue, 17 Feb 2004 12:24:35 GMT

fstruwig@msn.com (Franz) wrote:

> 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!

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



Relevant Pages

  • Re: mutually referential (Pg 140 K&R2)
    ... >In this example the vacuous declaration doesn't make any difference; ... >needed when you want a name in an inner scope to match a tag defined ... >in the inner scope instead of a tag from the outer scope. ... the vacuous declarations of struct ...
    (comp.lang.c)
  • Re: struct type completion
    ... # content is an incomplete type. ... We need to re-declare the identifiers to complete their type. ... With struct types this is a bit different: ... declaration, that's why sizeof object is okay after the second ...
    (comp.lang.c)
  • Re: Porting from visual C to gcc problem
    ... The declaration above defines the identifier 'DNIS_TIMER_CALLBACK' as ... of a structure type named 'struct DNS_TIMER_CB'. ... C is a scoped language, and one of the scopes is "prototype scope". ... int func; ...
    (comp.lang.c.moderated)
  • Re: global struct declarations
    ... Any type must be declared at a scope such that its declaration is ... (Remember that "struct foo" is how you define a new type in C. ... The "struct mystruct" type needs to be in scope for both the ...
    (comp.lang.c)
  • Re: Looking for free memory pool software
    ... entry to the block containing its declaration, not at the point of the ... (Scope is another matter; I've read the relevant ... (I'm probably missing something obvious; ... Two identifiers have the _same scope_ if and only if their scopes ...
    (comp.lang.c)