对具有结构的不完整类型应用 sizeof 无效

发布于 2024-12-28 02:23:36 字数 758 浏览 3 评论 0原文

我有一个结构,其中放置有关玩家的所有信息。这是我的结构:

struct player{
   int startingCapital;
   int currentCapital;
   int startingPosition;
   int currentPosition;
   int activePlayer; 
   int canPlay;      
};

这就是我的主要内容:

#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;
}

我要求用户提供玩家数量,然后我尝试分配所需的内存。但我遇到了这个我无法弄清楚的编译器错误:

invalid application of `sizeof' to incomplete type `player'  

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'  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

宫墨修音 2025-01-04 02:23:36

这意味着包含 main 的文件无权访问 player 结构定义(即不知道它是什么样子)。

尝试将其包含在 header.h 中,或者创建一个类似构造函数的函数,如果它是一个不透明对象,则对其进行分配。

编辑

如果您的目标是隐藏结构的实现,请在有权访问该结构的 C 文件中执行此操作:

struct player *
init_player(...)
{
    struct player *p = calloc(1, sizeof *p);

    /* ... */
    return p;
}

但是,如果不应该隐藏实现 - 即 main 应该合法地说 p->canPlay = 1 最好将结构体的定义放在header.h中。

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.

兔姬 2025-01-04 02:23:36

诸如“Invalid application of sizeof to incomplete type with a struct ...”之类的错误的原因总是缺少 include 语句。尝试找到合适的库来包含。

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.

等风来 2025-01-04 02:23:36

当尝试访问未初始化的外部数组的 sizeof() 时,也会显示您的错误:

extern int a[];
sizeof(a);
>> error: invalid application of 'sizeof' to incomplete type 'int[]'

请注意,如果没有 ,您将收到 array size Missing 错误extern 关键字。

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.

东风软 2025-01-04 02:23:36

在这里聚会真的很晚了,但是上面引用的这个错误原因的一个特殊情况就是简单地引用上面定义了结构的 sizeof() 结构:

int numElements = sizeof(myArray)/sizeof(myArray[0]);
.
.
.
myArray[] = 
{
    {Element1},
    {Element2},
    {Element3}
};

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}
};
心在旅行 2025-01-04 02:23:36

我认为问题在于您将 #ifdef 而不是 #ifndef 放在 header.h 文件的顶部。

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

<逆流佳人身旁 2025-01-04 02:23:36

我是初学者,可能不清楚语法。
参考上面的资料,我还是不太清楚。

/*
 * 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)

两者都是“arrA”和“arrA”可以访问“arrB”(打印出来)。但是,无法获得“arrB”的大小。

那里有什么问题吗?

  1. 'char[]' 是不完整类型吗?或者
  2. “sizeof”不接受外部变量/标签?

在我的程序中,“arrA”& “arrB”是常量列表并在编译之前固定。我想使用标签(让我易于维护并节省 RAM 内存)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文