I have a struct where I put all the information about the players. That's my struct:
struct player{
int startingCapital;
int currentCapital;
int startingPosition;
int currentPosition;
int activePlayer;
int canPlay;
};
And that's my main:
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
int main(int argc, char *argv[])
{ int s,i,numOfPlayers;
struct player *players;
printf("Give the number of players: \n");
scanf("%d",&numOfPlayers);
players = (struct player *)calloc(numOfPlayers,sizeof(struct player));
system("PAUSE");
return 0;
}
I'm asking the user to give the number of players and then I try to allocate the needed memory. But I'm getting this compiler error that I can't figure out:
invalid application of `sizeof' to incomplete type `player'
It means the file containing main doesn't have access to the player structure definition (i.e. doesn't know what it looks like).
Try including it in header.h or make a constructor-like function that allocates it if it's to be an opaque object.
EDIT
If your goal is to hide the implementation of the structure, do this in a C file that has access to the struct:
struct player *
init_player(...)
{
struct player *p = calloc(1, sizeof *p);
/* ... */
return p;
}
However if the implementation shouldn't be hidden - i.e. main should legally say p->canPlay = 1 it would be better to put the definition of the structure in header.h.
The cause of errors such as "Invalid application of sizeof to incomplete type with a struct ... " is always lack of an include statement. Try to find the right library to include.
Your error is also shown when trying to access the sizeof() of an non-initialized extern array:
extern int a[];
sizeof(a);
>> error: invalid application of 'sizeof' to incomplete type 'int[]'
Note that you would get an array size missing error without the extern keyword.
I think that the problem is that you put #ifdef instead of #ifndef at the top of your header.h file.
I am a beginner and may not clear syntax.
To refer above information, I still not clear.
/*
* main.c
*
* Created on: 15 Nov 2019
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "dummy.h"
char arrA[] = {
0x41,
0x43,
0x45,
0x47,
0x00,
};
#define sizeA sizeof(arrA)
int main(void){
printf("\r\n%s",arrA);
printf("\r\nsize of = %d", sizeof(arrA));
printf("\r\nsize of = %d", sizeA);
printf("\r\n%s",arrB);
//printf("\r\nsize of = %d", sizeof(arrB));
printf("\r\nsize of = %d", sizeB);
while(1);
return 0;
};
/*
* dummy.c
*
* Created on: 29 Nov 2019
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "dummy.h"
char arrB[] = {
0x42,
0x44,
0x45,
0x48,
0x00,
};
/*
* dummy.h
*
* Created on: 29 Nov 2019
*/
#ifndef DUMMY_H_
#define DUMMY_H_
extern char arrB[];
#define sizeB sizeof(arrB)
#endif /* DUMMY_H_ */
15:16:56 **** Incremental Build of configuration Debug for project T3 ****
Info: Internal Builder is used for build
gcc -O0 -g3 -Wall -c -fmessage-length=0 -o main.o "..\\main.c"
In file included from ..\main.c:12:
..\main.c: In function 'main':
..\dummy.h:13:21: **error: invalid application of 'sizeof' to incomplete type 'char[]'**
#define sizeB sizeof(arrB)
^
..\main.c:32:29: note: in expansion of macro 'sizeB'
printf("\r\nsize of = %d", sizeB);
^~~~~
15:16:57 Build Failed. 1 errors, 0 warnings. (took 384ms)
Both "arrA" & "arrB" can be accessed (print it out). However, can't get a size of "arrB".
What is a problem there?
Is 'char[]' incomplete type? or
'sizeof' does not accept the extern variable/ label?
In my program, "arrA" & "arrB" are constant lists and fixed before to compile. I would like to use a label(let me easy to maintenance & save RAM memory).
Really late to the party here, but a special case of the reasons for this error cited above would simply be to reference a structure with sizeof() above where the structure is defined:
int numElements = sizeof(myArray)/sizeof(myArray[0]);
.
.
.
myArray[] =
{
{Element1},
{Element2},
{Element3}
};
Related
I'm trying to compile "Example 11-9. Source code to the SYNplescan tool" from this NetworkSecurityTools book on Ubuntu 18.04:
http://books.gigatux.nl/mirror/networksecuritytools/0596007949/networkst-CHP-11-SECT-4.html
But it says
error: dereferencing pointer to incomplete type ‘struct tcphdr’
if (tcp->th_flags == 0x14)
^~
How do I fix this?
People change and includes come and go, after the following changes:
## -1,9 +1,12 ##
+#define _DEFAULT_SOURCE 1
#define _BSD_SOURCE 1
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <libnet.h>
#include <pcap.h>
+#include <netinet/tcp.h>
+#include <netinet/ip.h>
int answer = 0; /* flag for scan timeout */
## -42,7 +45,7 ##
int
main (int argc, char *argv[])
{
- char *device = NULL; /* device for sniffing/sending */
+ const char *device = NULL; /* device for sniffing/sending */
char o; /* for option processing */
in_addr_t ipaddr; /* ip address to scan */
u_int32_t myipaddr; /* ip address of this host */
I was able to compile with:
gcc -Wall 1.c -lnet -lpcap
with no compiler messages. I guess that once netinet/tcp.h was included by libnet.h or maybe by pcap.h - seems not be the case anymore and you have to include netinet/tcp.h yourself for struct tcphdr.
First of all, I've googled the error and read these answers:
I don't understand why compiler is giving me error with this code
C : Warning about visibility of a struct
But none of them helped me, so here we are.
The problem resides somewhere in between these 2 structures, prx_data_s which store generic data and prx_ops_s that defines pointers to functions that will use that data.
I'll simplify the sources for the example:
prx_data.h
#ifndef PRX_EXAMPLE_DATA_H
#define PRX_EXAMPLE_DATA_H
#include "prx_ops.h"
struct prx_data_s {
enum prx_op_t op;
char *keyquery;
};
char *get_query(struct prx_data_s *dt);
#endif
prx_data.c
#include "prx_data.h"
char *get_query(struct prx_data_s *dt)
{
return dt->keyquery;
}
prx_ops.h
#ifndef PRX_EXAMPLE_OPS_H
#define PRX_EXAMPLE_OPS_H
#include "prx_data.h"
enum prx_op_t {
PRX_EXAMPLE_OP = 2
};
struct prx_ops_s {
int (*dec) (struct prx_data_s *);
};
#endif
I'm trying to compile the object from the above example with:
clang -c prx_data.c -o prx_data.o -std=c11 -g -Wall
And this is the output error:
In file included from prx_data.c:1:
In file included from ./prx_data.h:4:
./prx_ops.h:11:24: warning: declaration of 'struct prx_data_s' will not be visible outside of this function [-Wvisibility]
int (*dec) (struct prx_data_s *);
^
All help is welcome, thanks :)
You have a problem with circular dependencies in your header:
prx_data.h:
#include "prx_ops.h" <<< Here we do not yet see the struct definition
prx_ops.h:
#include "prx_data.h" <<<< Nothing will be included due to inclusion guards.
struct prx_ops_s {
int (*dec) (struct prx_data_s *); <<<< Here a new struct type is declared.
};
later back in prx_data.h:
struct prx_data_s {
enum prx_op_t op;
char *keyquery;
};
When reading prx_ops.h, you dont 't have prx_data.h included because the compiler is reading prx_ops.h from the include at the beginning of prx_data.h. You thus have to forward declare it.
Try adding
struct prx_data_s;
at the beginning of prx_ops.h`
Hope that helps ~~
So whenever I try to run my Makefile on my server, it always gives me the error is "Memory.c: 9 error: expected ')' before '*' token. But when I try to run it on my own computer, it works just fine. I've been trying to figure out what is wrong but can't seem to find it.
I've attached the 3 files that are used in this part of my program. Memory.c, Memory.h and ProcessInput.h.
This is Memory.c
/* Initializes memory */
#include <stdio.h>
#include <stdlib.h>
#include "memory.h"
void initializeMemory(memory** memArray, int memSize)
{
// Allocating space for memory array
*memArray = malloc(memSize * sizeof(memory));
if(*memArray == NULL)
{
fprintf(stderr, "Error allocating space for array of memory" );
exit(1); // exit(1) = Unsuccessful exit
}
// Initializing the contents within memory array
int i = 0;
for(i = 0; i < memSize; i ++)
{
((*memArray)[i]).occupied = false;
}
}
and this is Memory.h
// Definitions for Memory.c
#define bool int
#define true 1
#define false 0
#include "ProcessInput.h"
// Include guards to prevent redefinition of struct
#ifndef MEMORY_H
#define MEMORY_H
typedef struct memoryDetail
{
process process;
bool occupied;
} memory;
#endif
// Function declaration for memory.c
void initializeMemory(memory** memArray, int memSize);
the only thing used from ProcessInput.h is the process structure defined in ProcessInput.h
This is ProcessInput.h
// Include guards to prevent redefinition of struct
#ifndef PROCESSDETAIL_H
#define PROCESSDETAIL_H
typedef struct processDetail
{
int timeCreated;
int processID;
int memorySize;
int jobTime;
} process;
#endif
// function declarations for ProcessInput.c
void processInput(int* maxSize, int* count, process** processes, char* fileName);
I'm not too sure why it's giving me the error. I don't know where I'm supposed to be putting a missing right brace. Any advice is much appreciated!
edit: As informed, these are the following questions that I looked at but to not avail.
error: expected ‘)’ before ‘*’ token
Multiple of same error while compiling "error: expected ')' before '*' token
http://www.dreamincode.net/forums/topic/288956-error-expected-before-token/
thanks everyone for the help!
#include "memory.h" is different to #include "Memory.h" (i.e. C is case sensitive)
If you tried #include "myfile.h" instead of #include "MyFile.h" the error may be more obvious. In this case it just happens that the compiler finds the system memory.h.
<memory.h> is a header from C library of pre-standard era. It is quite possible that your standard library still provides it and the compiler takes that one instead of yours.
Try renaming your header file and see if it changes anything.
okay, ive searched a solution for like two days now but i couldnt find whats going wrong with my code. ;(
The task is simple: define a new type using typedef and have a function read out lines of this new type from a file into an array of again this new type. so my typedef inside the headerfile looks like this right now (ive tried several variants of writing this)
// filename: entries.h
#ifndef ENTRIES_H_
#define ENTRIES_H_
#include<time.h>
typedef struct{
char Loginname[25];
time_t RegDate;
unsigned long Highscore;
time_t Hdate;
}typePlayerEntry;
int readPlayerList(char *name, typePlayerEntry *feld);
#endif /* ENTRIES_H_ */
the main.c:
//filename: main.c
#include <stdio.h>
#include "entries.h"
int main(void) {
char name[13]="numbers.txt";
typePlayerEntry *pep;
readPlayerList(name, pep);
return 0;
}
my function file looks like this (and heres where the error is shown)
//filename: readPlayerList.c
int readPlayerList(char *name, typePlayerEntry *feld) {
return 0;
}
irrelevant code is completely left out. The problem is reproducable with the code posted.
the program wont compile because the type of the second argument in the function file could not be recognized,
- which is odd, because its defined in the header file and also usable in the main function.
And this error is somehow connected to the declaration of (in this case) a pointer of type playerEntry in my main.c. So if i do not declare it, theres no error, though i have to declare it to actually give it to the function. how come that the solution so far is to include the entries.h into the readPlayerList.c, which wasnt neccesary for previous functions?
im using eclipse kepler with MinGW, in case thats a compiler issue.
corrected the missing include of time.h and adjusted the code a little.
You are missing #include <time.h> in entries.h.
// filename: entries.h
#ifndef ENTRIES_H_
#define ENTRIES_H_
typedef struct {
char Loginname[25];
time_t RegDate; /* from <time.h> */
unsigned long Highscore;
time_t Hdate; /* from <time.h> */
} playerEntry;
int readPlayerList(char *name, playerEntry *feld);
#endif /* ENTRIES_H_ */
And you need to #include "entries.h" in readPlayerList.c
//filename: readPlayerList.c
int readPlayerList(char *name, typePlayerEntry *feld) {
/* ^^^^^^^^^^^^^^^ from entries.h */
return 0;
}
part of the problem is the compiler is seeing (at least) two different meanings/definitions for the 'playerEntry' name.
Suggest:
1) eliminate the 'typedef' statement
(it is just cluttering the code and confusing the compiler)
2) properly reference the struct via:
'struct playerEntry' instead of 'playerEntry'
in TheHeader.h file:
struct playerEntry
{
char Loginname[25];
time_t RegDate;
unsigned long Highscore;
time_t Hdate;
};
int readPlayerList(char *name, struct playerEntry *feld);
in the source file:
#include "TheHeader.h"
int readPlayerList(char *name, struct playerEntry *feld)
{
return 0;
}
First of all I am showing the code for my c file ..
#include <stdlib.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <memory.h>
#include <string.h>
#include <ctype.h>
#include "sendip_module.h"
#include "ipv6ext.h"
#include "../ipv6.h"
#include "../ipv4.h"
#include "ah.h"
#include "esp.h"
#include "crypto_module.h"
#include <openssl/hmac.h>
#include <openssl/md5.h>
/*
code for hmac_md5 here....
void
hmac_md5(text, text_len, key, key_len, digest)
unsigned char* text; /* pointer to data stream */
int text_len; /* length of data stream */
unsigned char* key; /* pointer to authentication key */
int key_len; /* length of authentication key */
caddr_t digest; /* caller digest to be filled in */
{
MD5_CTX context;
unsigned char k_ipad[65]; /* inner padding -
* key XORd with ipad
*/
unsigned char k_opad[65]; /* outer padding -
* key XORd with opad
*/
unsigned char tk[16];
int i;
/* if key is longer than 64 bytes reset it to key=MD5(key) */
if (key_len > 64) {
MD5_CTX tctx;
MD5Init(&tctx);
MD5Update(&tctx, key, key_len);
MD5Final(tk, &tctx);
key = tk;
key_len = 16;
}
/*
* the HMAC_MD5 transform looks like:
*
* MD5(K XOR opad, MD5(K XOR ipad, text))
*
* where K is an n byte key
* ipad is the byte 0x36 repeated 64 times
* opad is the byte 0x5c repeated 64 times
* and text is the data being protected
*/
/* start out by storing key in pads */
bzero( k_ipad, sizeof k_ipad);
bzero( k_opad, sizeof k_opad);
bcopy( key, k_ipad, key_len);
bcopy( key, k_opad, key_len);
/* XOR key with ipad and opad values */
for (i=0; i<64; i++) {
k_ipad[i] ^= 0x36;
k_opad[i] ^= 0x5c;
}
/*
* perform inner MD5
*/
MD5Init(&context); /* init context for 1st
* pass */
MD5Update(&context, k_ipad, 64); /* start with inner pad */
MD5Update(&context, text, text_len); /* then text of datagram */
MD5Final(digest, &context); /* finish up 1st pass */
/*
* perform outer MD5
*/
MD5Init(&context); /* init context for 2nd
* pass */
MD5Update(&context, k_opad, 64); /* start with outer pad */
MD5Update(&context, digest, 16); /* then results of 1st
* hash */
MD5Final(digest, &context); /* finish up 2nd pass */
}
*/
/*
rest of the program logic...
*/
I have already included ...<.path where openssl is installed.....>../openssl/include to C_INCLUDE_PATH and exported it.
and now when i try to compile it getting error :
$ make
gcc -o xorauth.so -I.. -fPIC -fsigned-char -pipe -Wall -Wpointer-arith -Wwrite-strings
wstrict-prototypes -Wnested-externs -Winline -Werror -g -Wcast-align -
DSENDIP_LIBS=\"/usr/local/lib/sendip\" -shared xorauth.c ../libsendipaux.a
../libsendipaux.a
cc1: warnings being treated as errors
xorauth.c:34:1: error: function declaration isn’t a prototype
xorauth.c: In function ‘hmac_md5’:
xorauth.c:56:17: error: implicit declaration of function ‘MD5Init’
xorauth.c:56:17: error: nested extern declaration of ‘MD5Init’
xorauth.c:57:17: error: implicit declaration of function ‘MD5Update’
xorauth.c:57:17: error: nested extern declaration of ‘MD5Update’
xorauth.c:58:17: error: implicit declaration of function ‘MD5Final’
xorauth.c:58:17: error: nested extern declaration of ‘MD5Final’
make: *** [xorauth.so] Error 1
if required I will edit the other implementation details I have skiped them just to make the post small because I think there is something which i need to do regarding include path and header files and i am unaware of it.
What is going wrong please help me ???
There is no MD5Init function in OpenSSL. (There is in the BSD implementation.)
man MD5_Init (note the underscore), or see here.
EDIT:
Now that you've shown us the offending code, I can also help with the "not a prototype" message.
You have (edited a bit):
void hmac_md5(text, text_len, key, key_len, digest)
unsigned char* text;
int text_len;
unsigned char* key;
int key_len;
caddr_t digest;
{
/* ... */
}
That's an old-style, or "K&R", function definition. It's still valid, but only for backward compatibility, and it means that the compiler won't be able to warn you about calls with the wrong number or type(s) of arguments. The modern (since 1989) version is:
void hmac_md5(unsigned char *text,
int text_len,
unsigned char *key,
int key_len,
caddr_t digest)
{
/* ... */
}
When converting old-style function declarations and definitions to use prototypes, you sometimes have to be careful about parameters with narrow types (float, and integer types narrower than int or unsigned int) due to the promotion rules. That doesn't apply in this particular case.
Note that you can leave the definition as it is if you like. Since you got the code from an internet draft, that might even be a good idea (if it ain't broke, don't fix it) -- but as I said you'll get no help from the compiler if you call it with the wrong number or type(s) of arguments.