[英]C get another value on scanf_s
I have some code that takes an int and store it in a struct. 我有一些接受int并将其存储在结构中的代码。 When I read in the value the int becomes another int that is negative.
当我读入值时,int变为另一个为负的int。 I write in 2 int console but the int gets the value -842150451.
我在2 int控制台中编写,但int的值是-842150451。
Stock.c 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 Here the struct is defined. Stock.h在此定义了结构。
struct tShirt* addStock();
struct tShirt {
int size;
char color[10];
};
I tried to usr -> instead of dot but then the variable get red line under it. 我尝试使用usr->而不是dot,但是变量在其下显示红线。
Change the problem line to: 将问题行更改为:
scanf_s("%d", &location[i].size);
scanf reads into pointers. scanf读入指针。 You didn't provide a pointer in the size case.
您没有在大小写情况下提供指针。 This is a common scanf source of errors.
这是常见的scanf错误来源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.