我在这里的问题似乎总是与使用函数有关。它仍然让我困惑!在本教科书练习中,我被要求按值传递结构,然后调整它并按引用传递。最初我设计的代码是在 main 中完成所有工作。现在我正在传递值。所以我添加了新函数,我认为我正确地传递了结构,但我在行中收到错误 void function1(struct Inventory inv){ 告诉我参数 1 (inv) 的类型不完整。请帮忙!
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void function1(struct Inventory inv);
struct Inventory{
char name[20];
int number;
float price;
float total;
}
void main(){
items;
void function1(items);
float total = items.number*items.price;
printf("Item\tNumber\tPrice\tTotal\tAddress of number\n");
printf("%s\t%d\t%.2f\t%.2f\t%X\n\n",items.name,items.number,items.price,total,&items.number);
getch();
}
void function1(struct Inventory inv) {
printf("Enter the name of the item: ");
scanf("%s", inv.name);
printf("Enter the number of items: ");
scanf("%d", &inv.number);
printf("Enter the price of each item: ");
scanf("%f", &inv.price);
}
最佳答案
在函数原型(prototype)中使用结构之前,您必须定义它。
struct Inventory{
char name[20];
int number;
float price;
float total;
}items;
void function1(struct Inventory inv);
关于C - 按值传递结构+成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27095292/