[英]C get another value on scanf_s
我有一些接受int並將其存儲在結構中的代碼。 當我讀入值時,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->而不是dot,但是變量在其下顯示紅線。
將問題行更改為:
scanf_s("%d", &location[i].size);
scanf讀入指針。 您沒有在大小寫情況下提供指針。 這是常見的scanf錯誤來源。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.