Questions about 3D arrays in C [closed]

2019-09-27 16:34发布

my code and my output

I use empty to assume that i don't know howmany items stored in the arrays[cr. Kinjal Dixit]

1条回答
再贱就再见
2楼-- · 2019-09-27 17:23

You are accessing your array out-of-bounds.

eg

char name[HAVE_DISCOUNT][HAVENT_DISCOUNT][MAX_LENGTH];

//...
sscanf( input, "%s", name[i][HAVENT_DISCOUNT] );  // <-- out of bounds

You have to index your array with positive integers less than the size of that dimension.

Perhaps you wanted something like this:

typedef enum {
    HAVE_DISCOUNT,
    HAVENT_DISCOUNT,
    NUM_DISCOUNT_TYPES
} DiscountType;

const int MAX_NAMES = 32;

char name[MAX_NAMES][NUM_DISCOUNT_TYPES][MAX_LENGTH];

// etc...
查看更多
登录 后发表回答