C - 传递结构+值的成员
问题描述:
我的问题在这里似乎总是关于使用函数。它仍然让我困惑!在这本教科书练习中,我被要求按价值传递一个结构,然后调整它并通过引用传递。最初,我设计了一些代码,让所有工作都以主要方式完成。现在,我正在传递价值。所以我添加了新函数,并且我想我正确地传递了结构,但是我在 void function1(struct Inventory inv){告诉我参数1(inv)具有不完整类型时出错。请帮忙!C - 传递结构+值的成员
#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);
}
答
您必须在您的函数原型中使用它之前定义您的结构。
struct Inventory{
char name[20];
int number;
float price;
float total;
}items;
void function1(struct Inventory inv);
这是你在这个网站已经第三个问题。在发布任何内容之前,请*学习缩进您的代码。 – 2014-11-23 22:43:20
按值传递任何非基元类型是低效和毫无意义的。学习使用指针。 – Havenard 2014-11-23 22:59:04
我的猜测是,它教会了我通过价值传递和参照传递之间的差异。尽管指针是最好的选择。 – steeele 2014-11-23 23:31:42