相关疑难解决方法(0)

使用结构将sizeof应用于不完整类型无效

我有一个结构,我把所有关于球员的信息.那是我的结构:

struct player{
   int startingCapital;
   int currentCapital;
   int startingPosition;
   int currentPosition;
   int activePlayer; 
   int canPlay;      
};
Run Code Online (Sandbox Code Playgroud)

这是我的主要内容:

#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;
}
Run Code Online (Sandbox Code Playgroud)

我要求用户给出玩家数量,然后我尝试分配所需的内存.但我得到这个编译错误,我无法弄清楚:

invalid application of `sizeof' to incomplete type `player'  
Run Code Online (Sandbox Code Playgroud)

c struct calloc

31
推荐指数
2
解决办法
9万
查看次数

'sizeof'无效应用于不完整类型'int []'当访问指针指向的整数数组时

我正在尝试学习C中的指针并且正在编写这个小整数数组指针练习,但遇到了一个无效的sizeof类型int[]问题的应用程序.请告诉我哪里出错了以及如何解决.谢谢.

#include <stdio.h>

int intA[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int intB[];

void int_copy(const int *source, int *destionation, int nbr)
{
    int i;
    for(i=0;i<nbr;i++)
    {
        *destionation++ = *source++;
    }
}

int main()
{
    int *ptrA = intA;
    int *ptrB = intB;

    int sizeA = sizeof(intA);
    int nbrA = sizeof(intA)/sizeof(int);
    printf("\n\n");
    printf("[Debug]The size of intA is:%d\n", sizeA);
    printf("[Debug]That means the number of elements is:%d\n", nbrA);

    printf("\n\nThe values of intA are:\n");
    int i; …
Run Code Online (Sandbox Code Playgroud)

c pointers

1
推荐指数
1
解决办法
2万
查看次数

标签 统计

c ×2

calloc ×1

pointers ×1

struct ×1