我试图在C语言中使用结构和指针结构进行一些练习.我遇到了一个困扰我的问题.我已经设置了一个函数,它从指向结构的指针中获取3个值并返回一个浮点数.但是,在调用此函数时,它会修改原始结构的值,最终导致访问冲突和一些讨厌的字符反流.单步执行代码后,值就可以了,直到我返回函数的值.
这是我的结构:
struct product
{
int ID;
char name[MAXNAME];
char department[MAXDEPT];
float price;
int taxable;
int free_shipping_eligible;
};
typedef struct product Product;
Run Code Online (Sandbox Code Playgroud)
功能原型:
Product * getInput(void);
float transactionTotal(const float price, const int taxable,
const int free_shipping);
void displayTransaction(const Product * inventory,
const float total_price);
Run Code Online (Sandbox Code Playgroud)
主要:
int main(void)
{
// Declare a pointer to a struct.
Product * invPoint = NULL;
float grand_total = 0.0;
// Get the value of the address of a struct.
invPoint = getInput();
// Use that …Run Code Online (Sandbox Code Playgroud)