0

So, I have this structure:

typedef struct{
    int serialNumber;
    char name[100];
    float price;
    int quantity;
 }Products;

And I created an array of structures dynamically.

The task was to 'simulate' a grocery store, with the user able to add and edit the items sold by the store. The following code snippets are for editing structure data.

void overwrite(Products store){
   printf("Enter new serial number: ");
   scanf("%d", &(store.serialNumber));

   getchar();

   printf("Enter new product name: ");
   fgets(store.name, 100, stdin);
   store.name[strlen(store.name)-1]='\0';

   printf("Enter new product price: ");
   scanf("%f", &(store.price));

   printf("Enter new product quantity: ");
   scanf("%d", &(store.quantity));
 }

void editData(Products *store, int storeCapacity){ //storeCapacity needed to invoke printData(), assume a working code for the function.

  int choice;

  printData(store, storeCapacity);
  printf("Enter slot number of product here: ");
  scanf("%d", &choice);

  overwrite(store[choice]);
}

Here's the catch, even though this code works, when I try to print the data, the data displays the values which should be overwritten. Have I forgotten to do something? I wish you could help me.

BTW, I code on an Android phone.

  • 1
    Possible duplicate of [What exactly is the difference between "pass by reference" in C and in C++?](http://stackoverflow.com/questions/13654138/what-exactly-is-the-difference-between-pass-by-reference-in-c-and-in-c) – Jack Oct 21 '15 at 22:55

3 Answers3

1
void overwrite(Products store){

C is pass by value, you need to pass a pointer to Products (i.e., Products *store) and modify the overwrite call in editData accordingly.

ouah
  • 142,963
  • 15
  • 272
  • 331
1

Basically the problem is that in C you pass arguments by value. So when you specify this signature

void overwrite(Products store)

and you invoke it somewhere:

Products myStore;
overwrite(myStore);

what happens is that a copy of myStore is created and placed on the stack, then the value is passed to the function. This means that every modification done to the Products object inside overwrite applies on the passed copy, not on the original object. This copy is then discarded when exiting the scope of overwrite function.

To solve this problem you must pass a pointer to the object, that is passed by value but being an address will point to the exact same myStore object. This is done in this way:

void overwrite(Products* store)
{
  ..
  scanf("%f", &store->price);
  ..
}

Products myStore;
overwrite(&myStore);
Jack
  • 131,802
  • 30
  • 241
  • 343
  • What's the point to answer after requesting to close as duplicate? – ouah Oct 21 '15 at 23:01
  • @ouah: If the question gets closed it's irrelevant if I answered or not, since I spent my time answering I don't see your point. I would argue: what's the point of answering to a question obviously already asked before without searching for a duplicate and marking it, as you did. – Jack Oct 21 '15 at 23:04
  • For your question, simple, my point is not to explain or re-explain the pass-by-value vs pass-by-reference, my goal is to spot the bug in his code and to show to OP where it is. – ouah Oct 21 '15 at 23:09
0

According to Ouah, I passed the structure as the value itself, which did happen in my code.

So what I did is...

void overwrite(Products * store){ //formal parameter changed into a pointer
  //codes here
}

And...

overwrite(&(store[choice])); /* actual parameter changed into a pointer by affixing ampersand*/

Further explanations of the codes' misbehavior were explained by Jack. I extend my gratitudes to you. The code now worked as it should be.