C - Forward declaration for structure and function

I am trying to understand how advanced declarations interact. When forward declares a function that takes a typedef'd structure, is there a way to simply force the compiler to accept a previously declared (but not defined) structure as a parameter?

The code I have is:

typedef struct{
  int year;
  char make[STR_SIZE];
  char model[STR_SIZE];
  char color[STR_SIZE];
  float engineSize;
}automobileType;

void printCarDeets(automobileType *);

What I would like to do:

struct automobileType;
void printCarDeets(automobileType *);

//Defining both the struct (with typedef) and the function later

I feel like I'm either missing something really basic, or I don’t understand how the compiler deals with forward declarations of structures.

+4
source share
1 answer

Typedefs and structure names are in different namespaces. So, struct automobileTypeand automobileType- this is not the same thing.

, .

.c :

typedef struct automobileType{
  int year;
  char make[STR_SIZE];
  char model[STR_SIZE];
  char color[STR_SIZE];
  float engineSize;
}automobileType;

:

typedef struct automobileType automobileType;
void printCarDeets(automobileType *);
+3

Source: https://habr.com/ru/post/1657165/


All Articles