Redefinition of structure - c

Below is the code of my header file trie.h.
The compiler keep showing the following error:
In file included from speller.c:11:
./trie.h:3:8: error: redefinition of 'letter'
struct letter
^
./trie.h:3:8: note: previous definition is here
struct letter
^
1 error generated.
make: *** [speller.o] Error 1
The code:
struct letter
{
int is_word;
struct letter* arr[27];
};
// fuctions
struct letter* create_trie();
void free_trie(struct letter* trie);

Most likely your file gets included multiple times, hence the redefinition error.
To avoid this problem use include guards:
#ifndef HEADERNAME_DEFINED
#define HEADERNAME_DEFINED
// your code goes here.
#endif // HEADERNAME_DEFINED
or you can use non-standard preprocessor directive like #pragma once to do the job. It results in less code, and sometimes faster compilation speed.
Put that on top of your file:
#pragma once
// your code goes here
Note: The comment (// HEADERNAME_DEFINED part) after #endif isn't necessary. It is just a hint for programmer to know what belongs together.

Related

C, "conflicting types for... " error

Before I continue, here is the code which is giving me an error:
#define numScores 3 // the number of test scores which a student will have
struct btreenode{
int studentID; // the ID number of the student at the current node
float scores[3]; // the 3 test scores of the student
float average; // the average of the 3 test scores for the student
struct btreenode *left; // pointer to left side of the tree
struct btreenode *right; // pointer to right side of the tree
};
typedef struct btreenode *Node;
I'm getting the following error when I compile:
btreenode.h:17: error: redefinition of 'struct btreenode'
btreenode.h:28: error: conflicting types for 'Node'
btreenode.h:28: note: previous declaration of 'Node' was here
I have a block comment at the top so the line numbers are off, but
line 17 is the first line "struct btreenode{"
line 28 is the last line "typedef struct btreenode *Node"
Does anyone know why i'm getting these errors?
The header file should not be included more than once. So use macro in header file to avoid multiple inclusion.
#ifndef TEST_H__
#define TEST_H__
/*you header file can have declarations here*/
#endif /* TEST_H__*/
I am assumed that, this kind of approach is not there in your header file.
It looks as though your btreenode.h file is being included (directly or indirectly) multiple times... that's why the "previous declaration" and the "conflicting types" are in the same file at the same line (previous declaration on the first include, conflicting types when it runs into the same line on the next include).
You should use header guards (in btreenode.h) to prevent the header file code from being processed if it's already been included. At the top of the file, add:
#ifndef BTREENODE_H
#define BTREENODE_H
and at the end of the file add:
#endif // BTREENODE_H
That way, whatever is between those will only be compiled if BTREENODE_H was not already #defined from a previous inclusion.

C Unknown type name 'my_structure'

I have this code:
main.h
#ifndef MAINH
#define MAINH
...
#include "my_struct.h"
void some_func(my_structure *x);
...
#endif
and
my_struct.h
#ifndef UTILSH
#define UTILSH
...
#include "main.h"
...
typedef struct abcd {
int a;
} my_structure;
...
#endif
but I'm getting this when I try to compile: error: unknown type name ‘my_structure’
Any idea why?
Because of how you've ordered your includes, the compiler sees void some_func(my_structure *x); before it sees typedef struct abcd { int a; } my_structure;.
Let's walk this through.
Assuming my_struct.h is processed first, we get the following sequence of events:
UTILSH is defined
MAINH is defined
Because UTILSH is already defined, we don't process my_struct.h again, so the typedef isn't processed
void some_func(my_structure *x); is processed.
Now the typedef is processed.
So after preprocessing, your compiler sees the following sequence of declarations:
...
void some_func(my_structure *x);
...
typedef struct abcd {...} my_structure;
Bad juju. You either need a forward declaration of my_structure in main.h, or you need to break that circular dependency (which is the much preferred option). Is there anything in main.h that my_structure.h actually uses? If so, you will want to factor it out into a separate file that both main.h and my_structure.h include.
You created a circular header inclusion. Circular inclusion never achieves anything. It is infinite. The #ifndef include guard will break the infinite inclusion circle at some unpredictable point (depending on which header is included into .c file first). This is what happened in your case. Basically, your circular inclusion got "resolved" into including main.h first and my_struct.h second. This is why main.h knows nothing about my_struct type.
Again, circular inclusion never achieves anything. Get rid of circular inclusion. Design your header structure hierarchically: lower-level headers included into higher-level headers, but never the other way around. In your case my_struct.h is probably a lower-level header, which means that you have to stop including main.h into my_struct.h. Redesign your headers so that my_struct.h no longer needs main.h.
The error message is coming from main.h while it's included in my_struct.h, before my_structure is defined. You should rethink your include paths since main.h and my_struct.h include each other.
You probably want your main.h file to just include my_struct.h, and not have my_struct.h to include anything. You're essentially instructing your C compiler to have an infinite co-include loop.

error: expected specifier-qualifier-list before

I am getting this following error,
I have a "A.c" file in which I have included a "b.h" file, which has a "c.h" file.
Now this c.h has structures which are getting used and they are all int.
the structures are used in the following way:
In "c.h" file
struct abc{
int a;<---- error
};
In "b.h"
struct def{
struct abc;
};
and I have used struct def in file "A.c" file.
Please, help me know what wrong have I done.
You probably have some nesting error, a missing ; or something that confuses the compiler.
I would recommend trying to get hold of the preprocessor output, so you can see what the compiler sees, once the #includes have been executed.

C compile error

What does the following error mean when compiling.
Tilemap.h:21: error: conflicting types for ‘ThreeDWorld’
Tilemap.h:21: error: previous declaration of ‘ThreeDWorld’ was here
Tilemap.h:29: error: conflicting types for ‘CGPoint’
Tilemap.h:29: error: previous declaration of ‘CGPoint’ was here
Tilemap.h:31: error: conflicting types for ‘tileForCoordinates’
Tilemap.h:31: error: previous declaration of ‘tileForCoordinates’ was here
Why is it giving an error for what was there?My source file has one instance of it as such
typedef struct
{
int xPosition;
int yPosition;
}
CGPoint;
Are you including the header file from more than one place? Use a guard in the header file, if so.
For example, in Tilemap.h:
#ifndef TILEMAP_H
#define TILEMAP_H
// header file contents
#endif /* TILEMAP_H */
Stick some inclusion guards on your headers.
Your type definition is appearing more than once in your compilation unit.
You included the header file twice.
In my own code, I wrapped all header files with
#ifndef HEADER_FILE_NAME
#define HEADER_FILE_NAME
#endif
to avoid such errors.

C typedef struct uncertainty

Consider the following typedef struct in C:
21:typedef struct source{
22: double ds; //ray step
23: double rx,zx; //source coords
24: double rbox1, rbox2; //the box that limits the range of the rays
25: double freqx; //source frequency
26: int64_t nThetas; //number of launching angles
27: double theta1, thetaN; //first and last launching angle
28:}source_t;
I get the error:
globals.h:21: error: redefinition of 'struct source'
globals.h:28: error: conflicting types for 'source_t'
globals.h:28: note: previous declaration of 'source_t' was here
I've tried using other formats for this definition:
struct source{
...
};
typedef struct source source_t;
and
typedef struct{
...
}source_t;
Which both return the same error.
Why does this happen? it looks perfectly right to me.
Are you sure you didn't include your header twice (without using #ifndef/#pragma once to avoid that)?
Even if there'd been some mistake in your construct it shouldn't trigger the error "redefinition of '...'" cause it's the very first line?
The most likely cause is that your header file is being included more than once.
You need to ensure that if this happens, the typedef is only executed once.
You can do this by wrapping globals.h with:
#ifndef _globals_h_
#define _globals_h_
[...]
#endif
The errors say a struct source has been defined more than once.
Maybe you included the header file twice?
Just to be on the safe side, be sure that your header gets only included once: put
#ifndef YOUR_HEADER_FILE_NAME
#define YOUR_HEADER_FILE_NAME
at the beginning, and
#endif
at the end of your header file: this will prevent it to be included twice or more by any source file.

Resources