I
ifmusic
i have this little .c, it's in spanish but you'll get it anyway
#include <stdio.h>
typedef struct {
char cadena[20];
int enterito;
} registro;
//this puts a new element to the list
int agregarElemento(registro **lista, registro *aux, int *lMax);
int main(void) {
registro **lista;
registro *aux;
int lMax;
int i,a,j;
char buf[15];
//creo una lista de 1 posicion
// i put a single element first
lista = (registro**) malloc (sizeof(registro*));
aux = (registro*) malloc(sizeof(registro));
aux->enterito = 0;
strcpy(aux->cadena, "Cadena_0\0");
lista[0]=aux;
printf("Cadena: %s y el numero es: %d\n",lista[0]->cadena,
lista[0]->enterito);
lMax = 1;
// i'll put N elements more.
//agrego N elementos MAS hasta 0.
printf("Ingresa un numero:\n");
scanf("%s",buf);
j = atoi(buf);
while (j)
{
for (i=0; i< j ; i++)
{
//i fill a record.
sprintf(buf,"Cadena_%d\0",lMax);
aux = (registro*) malloc(sizeof(registro));
aux->enterito = lMax;
strcpy(aux->cadena,buf );
// This is the line that's bugging me ////////////////////////////
lista = (registro**) realloc ( lista, sizeof(registro*) * ((lMax)+1)
);
agregarElemento(lista,aux,&lMax);
}
printf("Ingresa un numero:\n");
scanf("%s",buf);
j = atoi(buf);
}
for (i=0; i< lMax; i++) printf("Cadena: %s y el numero es:
%d\n",lista->cadena, lista->enterito);
return 1;
}
int agregarElemento(registro **lista, registro *aux, int *lMax)
{
lista[*lMax]=aux;
(*lMax)++;
return *lMax;
}
Ok, this works fine, i mean, i can add and print as many items as i
want BUT!
if i want to take the Realloc of the Index (pointer to pointer var.) to
a function like "agregarElemento" <add element >, eventualy it SEGS
FAULT . So there must be something i dont know about pointers, what
happens when you pass it to a function outside ? Because when i do
....... BUM!
If you want i can post a version of the software that fails. I guess
the question is:
How can i make a safe <add_a_element> function?
#include <stdio.h>
typedef struct {
char cadena[20];
int enterito;
} registro;
//this puts a new element to the list
int agregarElemento(registro **lista, registro *aux, int *lMax);
int main(void) {
registro **lista;
registro *aux;
int lMax;
int i,a,j;
char buf[15];
//creo una lista de 1 posicion
// i put a single element first
lista = (registro**) malloc (sizeof(registro*));
aux = (registro*) malloc(sizeof(registro));
aux->enterito = 0;
strcpy(aux->cadena, "Cadena_0\0");
lista[0]=aux;
printf("Cadena: %s y el numero es: %d\n",lista[0]->cadena,
lista[0]->enterito);
lMax = 1;
// i'll put N elements more.
//agrego N elementos MAS hasta 0.
printf("Ingresa un numero:\n");
scanf("%s",buf);
j = atoi(buf);
while (j)
{
for (i=0; i< j ; i++)
{
//i fill a record.
sprintf(buf,"Cadena_%d\0",lMax);
aux = (registro*) malloc(sizeof(registro));
aux->enterito = lMax;
strcpy(aux->cadena,buf );
// This is the line that's bugging me ////////////////////////////
lista = (registro**) realloc ( lista, sizeof(registro*) * ((lMax)+1)
);
agregarElemento(lista,aux,&lMax);
}
printf("Ingresa un numero:\n");
scanf("%s",buf);
j = atoi(buf);
}
for (i=0; i< lMax; i++) printf("Cadena: %s y el numero es:
%d\n",lista->cadena, lista->enterito);
return 1;
}
int agregarElemento(registro **lista, registro *aux, int *lMax)
{
lista[*lMax]=aux;
(*lMax)++;
return *lMax;
}
Ok, this works fine, i mean, i can add and print as many items as i
want BUT!
if i want to take the Realloc of the Index (pointer to pointer var.) to
a function like "agregarElemento" <add element >, eventualy it SEGS
FAULT . So there must be something i dont know about pointers, what
happens when you pass it to a function outside ? Because when i do
....... BUM!
If you want i can post a version of the software that fails. I guess
the question is:
How can i make a safe <add_a_element> function?