开发者

Deferencing a pointer error

typedef struct car car_t;
struct Car {
   int carID[QUEUE_SIZE];
   int f[QUEUE_SIZE];
};
int main (){
 int Array[ARRIVAL_QUEUE_SIZE];
 car_t *ddd 开发者_开发问答= (car_t*)malloc(sizeof(car_t));
   for(int i =0; i<2; i++){
        int carid = ((CARID_NUMBER_MIN)+(int)((double)(NUMBER_MAX-NUMBER_MIN+1)*rand()/(RAND_MAX+1.0)));    
 Array[i] = carid;
 ddd-> carID[i] = Array[i];
 ddd-> f[i] = Array[i];
   }

}

it is complaining about a dereferencing pointer to incomplete type and invalid application of ‘sizeof’ to incomplete type ‘car_t’


Well, it's because car_t refers to a struct car which is an incomplete type.

Are you sure you didn't mean struct Car (with an uppercase 'C')?

The sizeof(car_t) will not be known in the malloc call since you haven't specified what's actually inside that structure.

The ISO C standard (C99, 6.2.5) defines incomplete types as those that "describe objects but lack information needed to determine their sizes". That's exactly what you have in this situation.


I think you need to capitalize the car in your typedef.

typedef struct Car car_t;


To fix your error, capitalize the 'c' in car in your typedef:

typedef struct Car car_t;

This compiled for me:

#include <stdlib.h>

typedef struct Car car_t;

static const int QUEUE_SIZE = 10;
static const int ARRIVAL_QUEUE_SIZE = 10;
static const int CARID_NUMBER_MIN = 10;
static const int NUMBER_MAX = 10;
static const int NUMBER_MIN = 0;

struct Car {
  int carID[QUEUE_SIZE];
  int f[QUEUE_SIZE];
};

int main (){
  int Array[ARRIVAL_QUEUE_SIZE];
  car_t *ddd = (car_t*)malloc(sizeof(car_t));

  for(int i =0; i<2; i++){
    int carid = ((CARID_NUMBER_MIN)+(int)((double)(NUMBER_MAX-NUMBER_MIN+1)*rand()/(RAND_MAX+1.0)));
    Array[i] = carid;
    ddd-> carID[i] = Array[i];
    ddd-> f[i] = Array[i];
  }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜