Click here to Skip to main content
15,885,065 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following code:

C++
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct persona
{
    char *nombre;
    int edad;
    int sexo;
} Persona;

typedef struct
{
    int size;
    Persona vecPersona[];
} Array;

Array* getArrayInstance()
{
    Array *vec;
    vec = (Array*) malloc (sizeof(Array));
    vec->size = 0;
    return vec;
}

void push_back(Array ** vec, Persona tipito)
{
    (*vec)->vecPersona[(*vec)->size] = tipito;
    (*vec)->size++;
    printf("%d-", (*vec)->size);
    int newSize = (*vec)->size+1;
    Array *tmp = realloc((*vec), newSize*sizeof(Persona));
    if(tmp)
        *vec = tmp;
    else
        (*vec)->size--;
}

void mostrarPersonas(Array *vec)
{
    int i;
    printf("\n\n");
    printf("%d", vec->size);
    for(i=0; i<vec->size; i++)
    {
        printf("(%d) Nombre: %s - Edad: %d - Sexo: ", i, vec->vecPersona[i].nombre, vec->vecPersona[i].edad);
        if(vec->vecPersona[i].sexo == 0)
            printf("Masculino");
        else
            printf("Femenino");
        printf("\n");
    }
}

void cargarPersonas(Array **vec)
{
    int i, edad, random;
    int cantPersonas = 3;
    Persona aux;
    char hombres[][20] = {"Ramiro","Pedro","Federico","Jose","Antonio","Pablo","Raul","Gustavo","Gonzalo","Airton"};
    char mujeres[][20] = {"Mariana","Jennifer","Luz","Roxana","Ana","Sol","Micaela","Romina","Melina","Camila"};
    for(i=0; i<cantPersonas; i++)
    {
        edad = rand()%80+1;
        aux.edad = edad;
        if( (random = rand()%10) %2 == 0) 
        {
            aux.nombre = hombres[random];
            aux.sexo = 0;
        }
        else
        {
            aux.nombre = mujeres[random];
            aux.sexo = 1;
        }

        push_back(vec, aux);
    }

}


int main()
{
    srand(time(NULL));
    Array *vecPersonas = getArrayInstance();

    cargarPersonas(&vecPersonas); 
    printf("%d", vecPersonas->size);
    mostrarPersonas(vecPersonas);

    return 0;
}

The code compiles without errors, but it gives me problems when I execute it. For example I get strange characters in the name field.
Sample output:

Nombre: ?ç!:. - Edad: 25 - Sexo: Maculino
Nombre: #´ç``'' - Edad: 15 - Sexo: Femenino
Nombre: ,.´`$ªº - Edad: 25 - Sexo: Maculino


I am using the IDE Codeblock version 13.12
Posted
Updated 29-Aug-15 3:07am
v4
Comments
Mohibur Rashid 29-Aug-15 16:20pm    
Try to debug
nv3 30-Aug-15 4:06am    
Try "char* hombres[] = {"Ramiro", ...};" which is better anyway as it doesn't limit the lengths of your names.

1 solution

Your problem is, that the names array goes out of scope. A quick fix is to make it global. The correct way is to allocated buffers or string objects for the names.

My tip is to use std:string.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900