gpt4 book ai didi

c - 尝试将 scanf_s 写入 struct* 时读取访问冲突写入位置?

转载 作者:行者123 更新时间:2023-11-30 19:33:40 25 4
gpt4 key购买 nike

每当我尝试运行此代码时,我都会遇到读取访问冲突:

#include<stdio.h>
#include<stdlib.h>

int i = 0;
struct basicValues{
float rate, hoursWorked, grossPay, basePay, overtimePay, taxesPaid, netPay;
char name[15];
};

void inputValues (struct basicValues *entered)
{
printf("Please enter your name, hourly pay, and hours worked this week: ");
scanf_s("%s %f %f", entered->name, entered->rate, entered->hoursWorked);

}



void main()
{
int i = 0;
struct basicValues workers[5];

for (i = 0; i < 5; ++i)
{
inputValues(&workers[i]);
printf("%c %f %f", workers[i].name, workers[i].rate, workers[i].hoursWorked);
system("pause");
}

}

我认为这与我的结构 inputValues 有关,但我不知道要更改什么。谢谢

最佳答案

您应该在原始值类型之前添加 &,因为 scanf 需要变量的地址。像这样

&entered->rate,   &entered->hoursWorked

打印字符串时也使用 %s,而不是 %c

关于c - 尝试将 scanf_s 写入 struct* 时读取访问冲突写入位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45131647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com