0

I was trying to send a structure to a function and fill out the structure with the right value in the function and then display the value in main, but it was not is not working when i run it.(i cant use pointer functions).

 struct inventory{
                char name[20];
                int number;

                   };
void function(struct inventory items);
int main()
{
int x,y,z;
 struct inventory items;
function(items);
printf("\nam in main\n");
printf("\n%s\t",(items).name);
printf(" %i\t",(items).number); 
getch();    

    }
   void function(struct inventory items)
    {
printf(" enter the item name\n ");
scanf(" %s ",&(items).name );
printf(" enter the number of items\n ");
scanf("%i",&(items).number );
    }

(ie: i am not allowed to use pointer function ) how can i display the name and the number without pointer.

2 Answers2

3
struct inventory
{
    char name[20];
    int number;
};

struct inventory function();

int main()
{
    int x,y,z;
    struct inventory items;
    items=function();
    printf("\nam in main\n");
    printf("\n%s\t",(items).name);
    printf(" %d\t",(items).number); 
    getch();
}

struct inventory function()
{
    struct inventory items;
    printf(" enter the item name\n ");
    scanf(" %s ",&items.name );
    printf(" enter the number of items\n ");
    scanf("%d",&items.number );
    return items;
}
Presse
  • 418
  • 1
  • 4
  • 23
0

you can return the structure reference from the function, considor the code given below

 struct inventory{
  char name[20];
  int number;
 };

struct inventory function(struct inventory items);

int main()
{
 int x,y,z;
 struct inventory items,ret_itm;
 ret_itm=function(items);
 printf("\nam in main\n");
 printf("\n%s\t",(ret_itm).name);
 printf(" %i\t",(ret_itm).number); 
 getch();    

}

struct inventory function(struct inventory items)
{
  printf(" enter the item name\n ");
  scanf(" %s ",&(items).name );
  printf(" enter the number of items\n ");
  scanf("%i",&(items).number );
  return items;
}
Balayesu Chilakalapudi
  • 1,386
  • 3
  • 19
  • 43