Invalid application of sizeof to incomplete type with a struct

前端 未结 5 1740
你的背包
你的背包 2020-12-06 04:06

I have a struct where I put all the information about the players. That\'s my struct:

struct player{
   int startingCapital;
   int currentCapital;
   int s         


        
相关标签:
5条回答
  • 2020-12-06 04:21

    I think that the problem is that you put #ifdef instead of #ifndef at the top of your header.h file.

    0 讨论(0)
  • 2020-12-06 04:33

    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?

    1. Is 'char[]' incomplete type? or
    2. '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).

    0 讨论(0)
  • 2020-12-06 04:39

    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.

    0 讨论(0)
  • 2020-12-06 04:42

    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.

    0 讨论(0)
  • 2020-12-06 04:47

    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.

    0 讨论(0)
提交回复
热议问题