Struct in C: Unknown params error storage size

I'm a little new to C, and I'm having problems with the project I'm currently working on. Essentially, I have the following files: main.c, alarm.c, alarm.h

I have a structure declaration in alarm.c that looks like this:

#define STRLEN 150; struct alarmparams { char time[STRLEN]; char duration[STRLEN]; char snooze[STRLEN]; char port[STRLEN]; }; 

In main.c, I have:

 #include <stdio.h> #include <string.h> #include "alarm.h" int main(int argc, char *argv[]) { struct alarmparams params; printf("%s, %s\n", params.time, params.duration); } 

And in alarm.h I have:

 struct alarmparams; 

Right now, when I go to compilation, I get the following error:

 error: storage size of 'params' isn't known 

I looked at other posts about this error, so I already worked a bit on this. I also tried some of the suggested solutions, and I either got the same error, or I got more on it. I don’t understand how to fix this.

Is there something I am missing? Am I announcing something wrong?

In general, structures should be declared in the header file or c file? Or does it even matter? What is the difference between:

 struct foo {...}; 

and

 typedef struct foo {...}; 
+6
source share
3 answers
 struct alarmparams; 

- Incomplete ad type. You can create a pointer to an object of this type, but you cannot declare an object of this type or take its size until it is complete. You must make your full declaration visible in main.c in order to declare an object of your type.

If you use a type in alarm.c and main.c , just declare it in alarm.h and include alarm.h in alarm.h

For you, the second question, the difference between:

 struct foo {...}; 

and

 typedef struct foo {...} foo_; 

in the latter case, you also declare the alias foo_ for the type name struct foo .

+14
source

You must declare the structure in the alarm.h header alarm.h .

At the moment, when you turn on alarm.h , the main code does not see the structure, all that it sees is struct alarmparams; so he does not know how long this has been going on. How can you set aside space for something that you don’t know how much space it takes?

typedef struct foo { ... }; invalid: typedef expects you to provide an alias for the type. typedef struct foo { ... } foo_t; will be correct.

typedef is a storage class specifier, so you can think of it like any other regular declaration. Imagine you need an alias foo for type bar : just declare a variable of type bar and name it foo . Now add the typedef keyword behind, and you're done. Even a complex typedef can be easily understood with this simple approach. Since struct foo { ... }; will be an invalid declaration for a variable (no name specified), that is, typedef struct foo { ... }; .

In general, declare structures in the header file when you reuse them in other source files. If you don't plan on doing this, declaring them in a .c file should be fine.

+7
source

In addition to other answers, the value of STRLEN should be known at compile time (this is most likely in this case, but just in case).

0
source

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


All Articles