C在scanf_s上获得另一个值

时间:2014-12-10 19:54:50

标签: c

我有一些代码,它接受一个int并将其存储在一个struct中。当我读入值时,int变为另一个负数的int。我在2个int控制台中编写,但int得到的值为-842150451。

Stock.c

struct tShirt* addStock() {
    int amount;
    struct tShirt *location;
    int i;

    printf("Amount of T-shirts to register: ");
    scanf_s("%d", &amount);

    location = (struct tShirt *) malloc(amount * sizeof(struct tShirt));

    for (i = 0; i < amount; i++) {
        printf("~~~~~~~~~~~~~~~~~~~~\n");
        printf("Color on shirt:  ");
        scanf_s("%s", location[i].color, 10);
        printf("Shirt size: ");
        fflush(stdin);
        scanf_s("%d", location[i].size); //Problem!
        printf("Color: %s and size: %d", location[i].color, location[i].size);
    }
    return location;
}

Stock.h这里定义了结构。

struct tShirt* addStock();

struct tShirt {
    int size;
    char color[10];
};

我试过usr - &gt;而不是点,然后变量在它下面变成红线。

0 个答案:

没有答案