C - 按值传递结构+成员

标签 c structure argument-passing

我在这里的问题似乎总是与使用函数有关。它仍然让我困惑!在本教科书练习中,我被要求按值传递结构,然后调整它并按引用传递。最初我设计的代码是在 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/

相关文章:

c - 如何在 C 中使用 strstr() 计算另一个文件中的关键字?

c - 正在打印的结构中我的 int** 的垃圾输出

c++ - 变量中的数据不一致

C++ - 将数组传递给方法

c++ - 将数组作为参数传递

workflow-foundation-4 - 使用参数安排从外部 XAML 加载的子 DynamicActivity

c - 矩阵排序段错误

int 的转换,unsigned int 用于 c 中的指针

c - 如何在 Mac OS X 上安装 Pebble SDK

c - 尝试填充结构中包含的矩阵时出现段错误