DVillano
DVillano

Reputation: 47

How to add more spaces to array using realloc?

I'm trying to make a simple program to ask for user input (name and age) using a structure and allocating memory. I start with an array of 2 spaces, however when i try to assign more it overwrite the previous input.

Can someone show me where i'm being wrong?

#include <stdio.h>
#include <stdlib.h>
#include <stdio_ext.h>
typedef struct
{
    char nombre[20];
    int edad;
}Persona;

int main()
{
    int sizeArrayPersonas = 2;
    Persona listaPersonas[sizeArrayPersonas];
    Persona* pArrayPersona = NULL;
    char respuesta = 's';

    int i;

    pArrayPersona = listaPersonas;


    while(respuesta == 's')
    {
        pArrayPersona = (Persona*)malloc(sizeof(Persona)*sizeArrayPersonas);
        for(i=0;i<sizeArrayPersonas;i++)
        {
            printf("Ingrese nombre: ");
            __fpurge(stdin);
            fgets((pArrayPersona+i)->nombre,20,stdin);

            printf("Ingrese edad: ");
            scanf("%d", &(pArrayPersona+i)->edad);
        } 

        printf("Desea ingresar otro usuario? (s o n): ");
        __fpurge(stdin);
        scanf("%c", &respuesta);

        if(respuesta == 's')
        {
            Persona* pAuxArray;
            pAuxArray = (Persona*)realloc(pArrayPersona,sizeof(Persona)*(sizeArrayPersonas+1));
            if(pAuxArray != NULL)
            {
                pArrayPersona = pAuxArray;
                sizeArrayPersonas++;
                pAuxArray = NULL;   
            }
        }
    }

    //Mostrar array de estructura 
    for(i=0;i<sizeArrayPersonas;i++)
    {
        printf("Nombre: %s Edad: %d \n", (pArrayPersona+i)->nombre, (pArrayPersona+i)->edad);
    }

    return 0;
}

Upvotes: 2

Views: 209

Answers (1)

Hitokiri
Hitokiri

Reputation: 3699

when i try to assign more it overwrite the previous input.

Because in your code below:

for(i=0;i<sizeArrayPersonas;i++)
{
      printf("Ingrese nombre: ");
       __fpurge(stdin);
      fgets((pArrayPersona+i)->nombre,20,stdin);

      printf("Ingrese edad: ");
      scanf("%d", &(pArrayPersona+i)->edad);
} 

You always input the value for pArrayPersona from 0 to sizeArrayPersonas - 1

In the next iteration, you have to input value for pArrayPersona[sizeArrayPersonas], it means the value of old sizeArrayPersonas+1_th elements (for example, in first iteration you input 2 values, then in the next iteration, after type s, you have to input for 3rd element, you do not need to re-enter the value of 1st and 2nd elements of pArrayPersona).

Your code in if condition can change it to:

Persona* pAuxArray = realloc(pArrayPersona,sizeof(Persona)*(sizeArrayPersonas+1));
if(pAuxArray == NULL) {
   free(pArrayPersona);
   return -1;
} else {
   pArrayPersona = pAuxArray;
   sizeArrayPersonas++;
}

You have to malloc for pArrayPersona before while loop instead of at the beginning of the while loop.

BTW, your while loop can be:

    int i = 0;
    pArrayPersona = malloc(sizeof(Persona)*sizeArrayPersonas);
    while(respuesta == 's')
    {
        for( ;i<sizeArrayPersonas;i++)
        {
            printf("Ingrese nombre: ");
            fgets((pArrayPersona+i)->nombre,20,stdin);

            printf("Ingrese edad: ");
            scanf("%d", &(pArrayPersona+i)->edad);
        }

        printf("Desea ingresar otro usuario? (s o n): ");
        scanf(" %c", &respuesta);

        if(respuesta == 's')
        {
            Persona* pAuxArray = realloc(pArrayPersona,sizeof(Persona)*(sizeArrayPersonas+1));
            if(pAuxArray == NULL) {
                free(pArrayPersona);
                return -1;
            } else {
                pArrayPersona = pAuxArray;
                sizeArrayPersonas++;
            }
        }
    }

Do not forget to free(pArrayPersona); at the end of program (when you do not need to use it).

Upvotes: 1

Related Questions