Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to partially declare a struct that is typedef'ed when in an include file

Tags:

c

include

typedef

I'm trying to minimize interdependence of #include files as a general practice.

In xxx.h I have:

struct my_struct;  // partial decl to satisfy use of my_struct*
void funct(struct my_struct* ms);  // uses the partial def

How to do a similar partial decl with a typedef'd struct? I have an actual decl in some third #include that looks like (say in yyy.h):

typedef struct my_data_s {
  int ival;
  ... struct's other components ...
} my_data_t;

I just want a representative decl in xxx.h that reference the typedef:

typedef struct my_data_s  my_data_t;  // actual full decl is elsewhere
void funct2(my_data_t* md);   

This attempt causes 'redefinition of typedef my_data_t' error. (Using gcc 4.4.3 / Ubuntu 10.4) Other random search attempts (e.g., add '{}' to typedef) also give errors.

I know the compiler only needs to know that the function requires a pointer, so it seems like this should be possible. So far, found nothing that compiles w/o errors/warnings.

I've looked at the other questions and answers, could not find this problem addressed. Seems like there should be a well-known way to do this(?!) (I know that I can #include yyy.y whenever I #include xxx.h - trying to avoid such dependencies.) Thanks.

like image 995
Art Swri Avatar asked Mar 15 '12 17:03

Art Swri


People also ask

How do you declare a variable in a typedef struct?

The syntax is: typedef <specification> <name>; After you've done that, you can use <name> much like any of the built-in types of the language to declare variables. In your first example, you the <specification> is everything starting with struct atom , but there's no <name> after it.

Can we typedef in declaring structure?

You can declare a typedef name for a pointer to a structure or union type before you define the structure or union type, as long as the definition has the same visibility as the declaration. Typedef names can be used to improve code readability.

Can struct be declared inside function?

The function is declared inside the struct definition, in order to make the relationship between the structure and the function explicit.

Should you typedef a struct?

In general, a pointer, or a struct that has elements that can reasonably be directly accessed should never be a typedef. 'Opaqueness and "accessor functions" are not good in themselves'.


2 Answers

Have you tried the simple approach:?

xxx.h

struct my_data_s;
typedef struct my_data_s my_data_t;

yyy.h

#include "decl.h"
struct my_data_s {
   int foo;
};
like image 146
thiton Avatar answered Sep 28 '22 05:09

thiton


C99 doesn't allow to repeat a typedef, C11 does.

Just do the typedef only once and always have it first:

typedef struct my_data  my_data;

There also is no need to chose different names for the struct tag and the typedef identifier.

like image 36
Jens Gustedt Avatar answered Sep 28 '22 06:09

Jens Gustedt