C Changing value of array of struct through reference

81 Views Asked by At

I asked a similar question before, when I didnt know what was the problem of my code. Just as I was recommended I will give it in a better format.

This is an example of what happens to my code.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct
{
    int id_prod;
} producto;

void carga_producto(producto **productos)
{
    int n = 1;
    printf("Productos before the calloc: %d\n ", productos);
    *productos = (producto *)calloc(1, sizeof(producto));
    printf("Productos after the calloc: %d\n ", productos);
    while (n < 3)
    {
        printf("Value of n %d\n", n);
        producto *temp = realloc(*productos, (n + 1) * sizeof(producto));
        *productos = temp;
        printf("position of temp: %d\n", temp);
        printf("Productos: %d\n ", productos);
        n++;
    }
}

int main()
{
    producto *productos;
    carga_producto(&productos);
}

As you can clearly see, no pointer is being changed neither by the realloc or the = . both *productos and *temp stay intact.

Since their values dont change I cant add more. Both the calloc and realloc arent doing anything.

I tried playing with the realloc and malloc and checking for ways to fix this but I cant find a way to change the ptrs.

//THIS IS THE RESULT I OBTAINED

PS C:\Users\Usuario\Desktop\Esizon\test> ./datos.exe
Productos before the calloc: 719321432
Productos after the calloc: 719321432
Value of n 0
position of temp: 1280342752
Productos: 719321432
 Value of n 1
position of temp: 1280342752
Productos: 719321432

//I WAS EXPECTING A CHANGE OF VALUES SUCH AS THIS,

where the calloc finds a positions for the pointer and then, when Temp is updated, asigning the value of the ptr temp to the pointer productos.

And on the second cycle I expected it to change the value of temp when it was assigned more memory, but it didnt do anything.

PS C:\Users\Usuario\Desktop\Esizon\test> ./datos.exe
Productos before the calloc: 719321432
Productos after the calloc: 426416321
Value of n 0
position of temp: 1280342752
Productos: 1280342752
 Value of n 1
position of temp: 512346132
Productos: 512346132

Applying your changes

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct
{
    int id_prod;
} producto;

void carga_producto(producto **productos)
{
    int n = 0;
    printf("Productos before the calloc: %d\n ", productos);
    *productos = (producto *)calloc(1, sizeof(producto));
    printf("Productos after the calloc: %d\n ", productos);
    while (n < 2)
    {
        printf("Value of n %d\n", n);
        producto *temp = realloc(*productos, (n + 1) * sizeof(producto));
        *productos = temp;
        printf("position of temp: %d\n", *temp);
        printf("Productos: %d\n ", *productos);
        n++;
    }
}

int main()
{
    producto *productos;
    carga_producto(&productos);
}

The result is

PS C:\Users\Usuario\Desktop\Esizon\test> ./datos.exe
Productos before the calloc: -1270876504
 Productos after the calloc: -1270876504
 Value of n 0
position of temp: 0
Productos: -1072923936
 Value of n 1
position of temp: 0
Productos: -1072923936

Still no change to productos and temp is not even initialized.

try it:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX 3

typedef struct {
    int id_prod;
} producto;

producto* carga_producto(int max) {
    producto *productos = calloc(1, sizeof(producto));
    if (productos) {
      printf("Producto[0]: id=%d\n", productos->id_prod);
      for (int n = 1; n < max; n++) {
          producto *temp = productos;
          productos = realloc(temp, (n + 1) * sizeof(producto));
          if (productos) productos[n].id_prod = n; else { printf("%s\n", "Something wrong"); free(temp); break; }
          printf("Producto[%d]: id=%d\n", n, productos[n].id_prod);
      }
    }
    return productos;
}

int main() {
    producto *productos = carga_producto(MAX);
    if (!productos) return 1;
    free(productos);
    return 0;
}

This works instead of sending the data through ref I just receive a product array. It is not a fix (and I have tried wwhat every other comment said one by one till 5 am) but it will do the work I need. Thanks for the help guys.

1

There are 1 best solutions below

0
Eric Postpischil On

The value of productos in carga_producto is the address passed to it by main.

*productos = (producto *)calloc(1, sizeof(producto)); does not assign a value to productos. It assigns a value to the thing productos points to, *productos.

To see the value, print *productos, not productos.

Use %p to print a pointer, not %d, and convert the pointer to void *, as in:

printf("Productos before the calloc: %p\n ", (void *) *productos);

It is an incorrect deduction to conclude that because the address of an allocation did not change, more memory was not reserved. The memory management software may reserve more memory at the same starting address.