I can't use <nameofmystructure>* this as an argument to a function - c

this is the code i'm having trouble with:
typedef const char* sun_date_t;
typedef const char* sun_time_t;
typedef struct sun_t {
int luminosity;
/* private */
sun_date_t date;
sun_time_t time;
sun_time_t sunrise;
sun_time_t sunset;
pthread_t tid;
pthread_mutex_t mutex;
} sun_t;
void sun_parse_data (sun_t* this, FILE* f){.....}
as you see, i define an structure and after i try to pass it as a pointer to this "sun_t", but the compiler says i need to write ',' or '....' before 'this'. Any ideas how to solve it?

You must be compiling as C++ since this is a keyword in C++ in C it is not but it is probably not a good idea to use this as a variable name in case you want to port the code later on.
The code compiles fine as a C program: see it live but we see the same error when we attempt to compile as a C++ program: see it live:
error: expected ',' or '...' before 'this'

Related

Including enums defined in header

I have been fiddling with enums for a while and wanted to try to use them in a project. The project structure is as follows:
// protocol.h
#ifndef PROTOCOL_H
#define PROTOCOL_H
enum C_C {P_NORTH = 0,
P_WEST = 1,
P_SOUTH = 2,
P_EAST = 3};
#endif
// other.h
#include "protocol.h"
struct cmd {
enum C_C code : 4;
};
void make_cmd(struct cmd*, enum C_C);
This file triggers the following errors:
field 'code' has incomplete type
'enum C_C' declared inside parameter list will not be visible outside of this definition or declaration
// other.c
#include "other.h"
void make_cmd(struct cmd* cmd, enum C_C code) {
cmd->code = code;
}
This throws the following errors:
conflicting types for 'make_cmd'
I have tried changing the enum to a type using typedef with no luck. This happens also with function definitions which rely on this type of parameters.
Will throw the following error:
type of formal parameter 2 is incomplete
Thanks for your help.
This only happens when using the defined enum in another header, either for structs or for functions prototypes.
I do believe there must be some issue with the compilation order. I have tested in Xilinx SDK and Vitis with the same result.
protocol.h holds all the definitions of the enums and the structures to be used throughout the project. I was hoping by just including this one in the other headers the definitions would be available to build the other.h and other.c on top of that one.
Update:
I have moved the definition of the structure inside the protocol.h and it lets me add a member using the enum without issues. I guess the problem is when importing protocol.h into another header and trying to use the enum there the compiler has all of the headers in the
This code compiles:
#include "other.h"
void make_cmd(struct cmd* cmd, enum C_C code) {
cmd->code = code;
}
int main(int argc, char *argv[])
{
struct cmd cmd;
make_cmd(&cmd, P_WEST);
}
If you #include "protocol.h" as well you'll get an error (type redefinition) because it is already included in other.h.

libwebsockets: Dereferencing pointer to incomplete type error

I'm using libwebsockets and I can't compile a demo code implemented by myself.
I created the context:
struct libwebsocket_context *context;
...
context = libwebsocket_create_context(&info);
and when I try to access the members of the struct libwebsocket_context, defined in private-libwebsockets.h:
struct libwebsocket_context {
struct pollfd *fds;
struct libwebsocket **lws_lookup; /* fd to wsi */
int fds_count;
int max_fds;
int listen_port;
...
};
For example,
printf("%d\n", context->listen_port);
The compiler returns,
error: dereferencing pointer to incomplete type
Thanks!
It seems that "struct libwebsocket_context" is not known for gcc - that's why this error occures. Are you sure that definition of this structure is included from .h file? I'd suggest you to insert for example #warning or #error with some message near definition of this struct (in .h file) and try to recompile your program. Your #error or #warning message should appear while compilation. If not - it means that gcc will not also see this struct.
The fact that the struct definition is in private-libwebsockets.h suggests that you are not supposed to use the struct members directly. You can #include that header to get access to the private implementation details of the library but you probably should not do it.

Extern stuct in C source files

I am trying to have a dictionary_object structure named dict_obj available in two separate .c files. This is while trying to write a pthread TCP server for a class. I have not worked much with C before and am having a difficult time getting this figured out. I'm not sure if I'm declaring this as an external structure correctly as netbeans is throwing off errors on clean and build saying invalid use of undefined type.
In db_functions.c I have:
//------------------------------------------------------------------------------
// Server Function & Variable Initialization
//------------------------------------------------------------------------------
struct dictionary_object dict_obj;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Server Function Codes:
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Database Initialization
//------------------------------------------------------------------------------
int db_initialization()
{
dict_obj.word_count = 0;
return 1;
In db_operations.c I have:
struct dictionary_object
{
char dictionary[DICTIONARY_SIZE][WORD_LENGTH];
int word_count;
pthread_mutex_t dict_mutex;
};
extern struct dictionary_object dict_obj;
Hopefully this gives you guys enough idea of what I'm trying to accomplish without having to overwhelm you with too much code. Thanks in advance! Let me know if you need to see more of the code.
You need to define the structure contents and layout once in an include file. Also define the "extern" definitions for all functions and statics for the code there. In any file wanting to use those structs or definitions, include the header file (something like db_struct.h).
db_struct.h:
struct dictionary_obhect {
char dictionary ...
}
extern struct dictionary_object dict_obj;
extern int db_initialization();
db_struct.c:
#include "db_struct.h"
struct dictionary_object dict_obj;
int db_initiatilization() {
dict_obc.word_count = 0;
}
Note this is all pretty old-style C code. Modern C++ etc. have other means of doing similar things.
I'll go out on a limb and guess that your problem is that the C compiler complains that it doesn't know how to allocate memory for a struct dictionary_object when it compiles db_functions.c. Its confusion is understandable because the file you're asking it to compile doesn't tell it what struct dictionary_object is at all.
You should move the definition of the struct to a header file that both of your .c files include.

Strange makefile build error

I'm working on this very simple part of code, but somehow the build error is pretty strange to me.
I have these files in the project: main.c, http_request.h, http_request.c; and a simple Makefile.
1. http_request.h
/* STRUCTURES */
struct http_param {
char key[MAX_BUFFER];
char *value;
};
struct http_request {
int size;
struct http_param data[MAX_PARAM];
};
/* FUNCTIONS */
int parse_http_request(apr_pool_t *pool, const char *args, struct http_request *req); /* (A) */
2. http_request.c
#include <http_request.h>
...// something else goes here, it's fine
int
parse_http_request(apr_pool_t *pool, const char *args, struct http_request *req) /* (B) */
{
3. Makefile
-I$(PROJECT_DIR)/include
this directory contains http_request.h.
Then I build, it raises this error:
/project/src/http_request.c:14: warning: 'struct http_request' declared inside parameter list
/project/src/http_request.c:14: warning: its scope is only this definition or declaration, which is probably not what you want
/project/src/http_request.c: In function 'parse_http_request':
The error points to (A) and (B) as I mentioned in source code above.
Could anyone help to find out what the problem is?
Thanks.
I suspect, seeing that you use apr_pool_t, that you also have apache includes in your include path. apache also provides a file called http_request.h and that one gets included instead of the one you have.
use
#include "http_request.h"
instead of
#include <http_request.h>

Passing structs by pointer in C89

I am working with a C89 compiler and I'm coming across some pointer typing error.
Calling code:
struct cpu_state_type cpu_state;
//Stuff here....
foo()
{
print_out_cpu(&cpu_state);
}
Print_out_cpu is defined elsewhere and the H file is #included in.
struct cpu_state_type
{
int r[12];
};
void print_out_cpu(struct cpu_state_type *c);
I get error:
error: incompatible type for argument 1 of 'print_out_cpu'
As best as I can understand,&cpu_state returns type cpu_state_type*, so I am confused.
Are you sure the prototype has the * in it? If I compile (gcc -std=c89) the following code, I get that exact error:
struct cpu_state_type {
int r[12];
};
// note that it is the structure as the param here (not the pointer)
void print_out_cpu(struct cpu_state_type c);
struct cpu_state_type cpu_state;
foo()
{
print_out_cpu(&cpu_state);
}
I don't see any problems, so I wonder if it's an error in your include statement or in the file, etc.
It'll be difficult to determine the cause of the error without seeing more of the source. Try creating a source file like:
#include struct cpu_state_type cpu_state;
void foo() {
print_out_cpu(&cpu_state);
}
If that doesn't trigger the problem, keep adding things until it does.
If it does trigger the problem, extract the pertinent parts of the header file into your source (and remove the #include) and try again.

Resources