-4

my questions here always seem to be about using functions. It still confuses me! In this textbook exercise i am asked to pass a structure by value, then adjust it and pass by reference. Initially I designed the code to have everything done in main. Now I am passing by value. So I added the new function, and I figured I passed the structure correctly but I am getting an error at line void function1(struct Inventory inv){ that tells me parameter 1 (inv) has incomplete type. please help!

#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);
}
syntagma
  • 23,346
  • 16
  • 78
  • 134
steeele
  • 31
  • 1
  • 7
  • 2
    This is your third question on this site already. Please *learn to indent your code* before you post any more. – Kerrek SB Nov 23 '14 at 22:43
  • Passing any non-primitive type by value is inefficient and pointless. Learn to use pointers. – Havenard Nov 23 '14 at 22:59
  • my guess is that it is teaching me the difference between passing by value and by reference. Even though pointers is the best way to go. – steeele Nov 23 '14 at 23:31
  • I suggest you first get a program compiled and working that uses a structure but no functions. Then start to build a function that takes a structure argument. – Weather Vane Nov 23 '14 at 23:36
  • thats exactly what i did but i'm having trouble understanding how to accomplish this in context of structures. With regular variables i understand functions well enough but with structures it confuses me – steeele Nov 23 '14 at 23:43
  • I suggested that because your structure declaration does not compile. Get your structure manipulation working simply, then build a function to do it. – Weather Vane Nov 23 '14 at 23:54

1 Answers1

1

You have to define your struct BEFORE you use it in your function prototype.

struct Inventory{
char name[20];
int number;
float price;
float total;
}items;

void function1(struct Inventory inv);
Philipp Murry
  • 1,660
  • 9
  • 13