I have a file called hittable.h that looks like this:
#ifndef HITTABLE_H
#define HITTABLE_H
#include "ray.h"
#include "vec3.h"
#include "material.h"
#include <stdbool.h>
typedef struct hit_record{
point3 p;
vec3 normal;
material* mat_ptr;
double t;
bool front_face;
} hit_record;
static inline void set_face_normal(hit_record *rec, const ray *r, vec3 outward_normal) {
rec->front_face = (dot(r->direction, outward_normal) < 0);
rec->normal = rec->front_face ? outward_normal : negate(outward_normal);
}
#endif
and I have another file called material.h that looks like this:
#ifndef MATERIAL_H
#define MATERIAL_H
#include "vec3.h"
#include "ray.h"
typedef struct {
color albedo;
} lambertian;
bool lambertian_scatter(lambertian lam, const ray* r_in, const hit_record* rec, color attenuation, ray* scattered) {
return false;
}
typedef struct{
int type;
union {
lambertian lam;
};
} material;
#endif
As you can see, in the the file material.h, the function lambertian_scatter()'s third argument is of type hit_record *. However, in hittable.h, the struct hit_record (which is required in material.h) has a member of type material which is defined in material.h. I tried several things and I couldn't fix this problem and was wondering what I should do.
Related
I am stuck on (what i think is) a circular dependency problem in VS2017 coding in c.
I tried looking up the problem, and found a lot of similar problems on stackoverflow, but I can't seem to get my problem solved with those questions.
My code:
main.c
#include <stdio.h>
#include "travelers.h"
#include "trip.h"
int main(void) {
int nTravelers = 0;
int nTrips = 0;
Traveler *travelersArray = (Traveler*)calloc(nTravelers, sizeof(Traveler));
Trip *tripsArray = (Trip*)calloc(nTrips, sizeof(Trip));
return 0;
}
travelers.h
typedef struct {
unsigned int id;
char *name;
char *adress;
char *residence;
} Traveler;
trip.h
typedef struct {
unsigned int id;
char *touringCar;
char *destination;
char *date;
Traveler *travelers;
unsigned int amount;
} Trip;
the travelers.c and trip.c files are only contain #include "travelers.h"/#include "trip.h"
The error occurs only in trip.h at Traveler *travelers;:
I don't know how to resolve this.
This looks like the same problem, but I couldnt translate it to my code.
Any help is apriciated.
No cycles here.
If trip.c includes trip.h is shall also include travelers.h as its definitions (Trip) depends on the latter (Traveller).
Knowing this, one could include travelers.h into trip.h. Still, this complicates things, so it is a good idea to 1st of all add to every header so call header-guards, protecting against duplicate definitions on pre-processor level.
Doing so made the headers look like this:
travelers.h
#ifndef TRAVELERS_H
#define TRAVELERS_H
typedef struct {
unsigned int id;
char *name;
char *adress;
char *residence;
} Traveler;
#endif // #ifndef TRAVELERS_H
trip.h
#ifndef TRIP_H
#define TRIP_H
#include "travelers.h" // makes including it unnecessary where trip.h is included
typedef struct {
unsigned int id;
char *touringCar;
char *destination;
char *date;
Traveler *travelers;
unsigned int amount;
} Trip;
#endif // #ifndef TRIP_H
As a remark, the error is caused by the typedef. C accepts opaque structs provided you do not need their implementation details:
a.h:
struct A {
int aVal;
const char * astr;
};
a.c:
#include "a.h"
const char *getAStr(struct A*a) {
return a->astr;
}
b.h
const char *getName(struct B*);
struct B {
int bVal;
struct A *a;
};
b.c
#include "b.h"
const char *getAStr(struct A*);
const char * getName(struct B* b) {
return getAStr(b->a);
}
main.c
#include <stdio.h>
#include "a.h"
#include "b.h"
int main() {
struct A a = { 1, "foo" };
struct B b = { 2, &a };
printf("%d - %d : %s\n", b.bVal, b.a->aVal, getName(&b));
return 0;
}
compiles and links without even a warning while in b.c nothing is known on struct A except that it is a struct.
I am trying to pass a structure by reference in C so that I can modify the values from within the function. This is the code I have so far, but it produces some warnings and one error.
main.c
#include <stdio.h>
#include "myfunctions.h"
#include "structures.h"
int main(int argc, char const *argv[] {
struct MyStruct data;
data.value = 6;
printf("Before change: %d\n", data.value);
changeData(data);
printf("After change: %d\n", data.value);
}
myfunctions.c
#include "structures.h"
void changeData(MyStruct data) {
data.value = 7;
}
myfunctions.h
#ifndef MyStruct
#define MyStruct
void changeData(MyStruct data);
#endif
structures.h
typedef struct {
int value;
} MyStruct;
Errors Produced
In file included from main.c:2:0:
myfunctions.h:4:1: warning: parameter names (without types) in function declaration
void changeData(MyStruct data);
^
In file included from main.c:3:0:
structures.h:5:1: warning: unnamed struct/union that defines no instances
} MyStruct;
^
main.c: In function ‘main’:
main.c:9:5: error: ‘data’ undeclared (first use in this function)
data.value = 6;
^
main.c:9:5: note: each undeclared identifier is reported only once for each function it appears in
That's all caused by
#define MyStruct
With this line, you've defined MyStruct as a macro that expands to nothing. I.e. you've effectively removed all occurrences of MyStruct in the following code, which is why the compiler is so confused about seeing things like
typedef struct {
int value;
} ;
or
void changeData( data);
To fix this, use
#ifndef MYFUNCTIONS_H_
#define MYFUNCTIONS_H_
instead. (This is the reason why we use ALL_UPPERCASE names for macros: To avoid accidental name clashes with normal identifiers.)
applying all my comments and elimination of the unnecessary 'typedef', and placing it all in one file ( Note: there is no problem with extracting the various files), results in the following code:
#ifndef STRUCTURES_H
#define STRUCTURES_H
struct MyStruct
{
int value;
};
#endif // STRUCTURES_H
#ifndef MYFUNCTIONS_H
#define MYFUNCTIONS_H
void changeData( struct MyStruct *data);
#endif // MYFUNCTIONS_H
#include <stdio.h>
//#include "myfunctions.h"
//#include "structures.h"
int main( void )
{
struct MyStruct data;
data.value = 6;
printf("Before change: %d\n", data.value);
changeData(&data);
printf("After change: %d\n", data.value);
} // end function: main
//#include "structures.h"
void changeData( struct MyStruct *data)
{
data->value = 7;
} // end function: changeData
which cleanly compiles and does do the desired operation
In a .h file I have an extern declare:
extern const some_struct SomeArray[];
In the .c file I have the definition:
const some_struct SomeArray[] =
{
1, 2, 3, 4, 5 etc.
};
VS Express 2013 is complaining with a C2085, 'not in formal parameter list', which according to MSDN, means a function prototype wasn't declared before the definition, but this isn't a function, so I'm a bit lost...
Edit: More detail...
An entire .h:
#ifndef MYHEADER_H_
#define MYHEADER_H_
#include <stdint.h>
typedef uint32_t my_event;
typedef uint32_t my_state;
typedef my_event(*my_handler)(my_event, void *);
typedef struct my_table
{
my_event Event;
my_handler Handler;
my_state NextState;
} my_table;
#endif
and it's .c(pp):
#include "myheader.h"
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
typedef enum TestEnum
{
A,
B,
C,
} TestEnum;
my_event functionA(my_event E, void *pointer);
my_event functionB(my_event E, void *pointer);
const my_table TestArray[] =
{
A, functionA, A,
};
return 0;
}
this is an example of how to initialize an array of structs:
typedef struct { int a; char b; } A;
const A arr[] = {
{ 3, 'a' },
{ 7, 't' }
};
if that does not help you, show us a complete compilable
minimal example that exhibits the error
I have 2 header files api.h and impl.h
api.h is visible to outside files and will be included in other ".c" files. So api.h includes impl.h
api.h defines 2 structures
typedef struct
{
uint32_t att;
union
{
struct
{
void* buffer;
size_t length;
} x;
struct
{
int a, b;
} v;
} content;
}dummy;
and impl.h has some other structures and function def which uses this structure.
I tried forward declaration but it doesn't help me .
Please help .
Actually, your dummy is not a structure, but a typedef to an unnamed structure. Try naming the structure, you can then forward-declare it:
typedef struct sdummy dummy; // forward declaration
void foo(dummy *);
struct sdummy { ... }; // definition
Either reorder your code in api.h so the type declaration precedes the #include "impl.h" or give your (currently anonymous) structure itself a name like dummy, dummy_, dummy_s so you can add a forward declaration
typedef struct dummy_ dummy;
to impl.h.
If you want to hide the details of your struct then you have to define it in some .c file, let's say impl.c, so that it has internal linkage to that compilation unit. To use it you have to expose create, destroy, getter and setter functions. So a basic setup would look like this:
api.h with forward declaration for your struct
// forward declaration
typedef struct dummy* dummy_t;
// create / destroy / setter / getter (omitted)
dummy_t alloc_dummy();
void free_dummy(dummy_t);
void set_number(dummy_t, int);
void set_string(dummy_t, char*);
void print_dummy(dummy_t);
Then comes impl.c
#include "api.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct dummy {
int n;
char* s;
};
dummy_t alloc_dummy()
{
return malloc(sizeof(struct dummy));
}
void free_dummy(dummy_t dummy)
{
if(dummy) {
free(dummy->s);
free(dummy);
}
}
void set_number(dummy_t dummy, int n)
{
if(dummy) {
dummy->n = n;
}
}
void set_string(dummy_t dummy, char* s)
{
if(dummy && s) {
dummy->s = strdup(s);
}
}
void print_dummy(dummy_t dummy)
{
if(dummy) {
printf("%d, %s\n", dummy->n, dummy->s);
}
}
And finally the usage in some other C files, here main.c
#include "api.h"
int main(int argc, char** argv)
{
// struct dummy d; // error! type is unknown
// instead use the create function
dummy_t d = alloc_dummy();
// d->n = 1; // error! dereference of unknown type
// instead use the setter function
set_number(d, 1);
set_string(d, "Hello, world!");
print_dummy(d);
free_dummy(d);
return 0;
}
Ouput
1, Hello, world!
If I wanted to make a function which takes the name of a struct as an argument, what would the method signature look like?
typedef struct Class{
} Class;
main()
{
Class *a = malloc(Class);
return instanceOf(a, Class);
}
What would the declaration of instanceOf look like?
You can't pass types to functions in C. However, you can simulate this behaviour using macros:
#define new_instance(t) (malloc(sizeof(t)))
Class *instance = new_instance(Class);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define type(x) #x
typedef struct Class{
char* type;
} Class;
Class *Class_new(){
Class *v;
v=(Class*)malloc(sizeof(Class));
v->type = "Class";
return v;
}
void Class_free(Class *a){
free(a);
}
int instanceOf(Class *a, const char* type){
return strcmp(a->type, type) == 0;
}
int main(){
Class *a = Class_new();
printf("%s\n", instanceOf(a, type(Class)) ? "YES" : "NO");
Class_free(a);
return 0;
}
// Encode and encapsulate the type within a nested structure when you create it.
struct struct_info
{
int type;
char name[32];
}
struct mystruct
{
struct struct_info;
}