-2

I can't figure out how I can erase an entry of an array of a structure type. I'm reading the book I bought but there's no chapter about that, and it seems I can't find any function to do that. I guess it's more manual related. I think about that and the only way I could think of is to change the position of the existing array to the one who want to be erase it, but I don't know how to do that.

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

struct coches {
        int    id;
       char   nombre[10];
       char   marca[10];
       char   color[10];
       float  kilometros;
};

int insertar_datos(struct coches concesionario[30]); 
int checkout(char login[10], char password[10]); 
void mostrar_datos(struct coches concesionario[30],int x);
void eliminar_datos(struct coches concesionario[30],int x);

int main (void){

struct coches concesionario[30];
int menu, credenciales, x = 0;
char login[10],password[10];


puts("\n<><><>Bienvenido<><><>\n");

puts("\nAntes de poder usar el sistema deber estar autentificado\n");
while (credenciales != 1){
puts("\nDime tu usuario:\n");
scanf("%10s", &login);
puts("\nDime tu password:\n");
scanf("%10s", &password);
credenciales=checkout(login, password);
}

while (menu != 4){
puts("\nQue deseas hacer?\n");
puts("\n1)Insertar datos\n2)Listar coches\n3)Eliminar entradas\n4)Salir del programa\n");
scanf("%d", &menu);
switch(menu){

             case 1:

                  x = insertar_datos(concesionario);
                  break;


             case 2:

                  mostrar_datos(concesionario,x);
                  break;

             case 3:

                  eliminar_datos(concesionario,x);
                  break;

             }

}    


}   

int checkout(char login[10], char password[10]){

    if ((strcmp(strlwr(login),"12345") ==0 ) && (strcmp(strlwr(password),"12345") ==0)){

    return 1;

    }else{

    return 0;

} 
}

int insertar_datos(struct coches concesionario[30]){
    char respuesta[3];
    int x = 0;
    system("cls");
    while (!strstr(respuesta,"no")){
    puts("\nDime la id:\n");
    fflush(stdin);
    scanf("%d", &concesionario[x].id);
    system("cls");
    puts("\nDime el modelo:\n");
    fflush(stdin);
    scanf("%10s", &concesionario[x].nombre);
    system("cls");
    puts("\nDime la marca:\n");
    fflush(stdin);
    scanf("%10s", &concesionario[x].marca);
    system("cls");
    puts("\nDime el color:\n");
    fflush(stdin);
    scanf("%10s", &concesionario[x].color);
    system("cls");
    puts("\nDime los kilometros:\n");
    fflush(stdin);
    scanf("%d", &concesionario[x].kilometros);
    system("cls");
    x++;
    puts("\nQuieres insertar algo mas?\n");
    fflush(stdin);
    scanf("%3s", &respuesta);
    system("cls");
    strlwr(respuesta);
}

return x;


}

void mostrar_datos(struct coches concesionario[30],int x){

    int i;

    if (x <= 0){


    system("cls");      
    puts("\nRegistro vacio\n");


    }else{


    for (i = 0; i < x; i++){

        printf("\nID: %d\n\nModelo: %s\n\nMarca: %s\n\nColor: "
           "%s\n\nKilometros: %f\n",
           concesionario[i].id, concesionario[i].nombre, concesionario[i].marca,
           concesionario[i].color, concesionario[i].kilometros);
     }
     system("pause");
}  

}

void eliminar_datos(struct coches concesionario[30],int x){

    int i, cuenta = 0;

    if (x <= 0){

    system("cls");      
    puts("\nRegistro vacio\n");

    }else{

    for (i = 0; i < x; i++){

        printf("\nEntrada numero:%d\nID: %d\n\nModelo: %s\n\nMarca: %s\n\nColor: "
           "%s\n\nKilometros: %f\n", cuenta,
           concesionario[i].id, concesionario[i].nombre, concesionario[i].marca,
           concesionario[i].color, concesionario[i].kilometros);
           cuenta++;

     }

     puts("\nQue entrada quieres borrar?\n");
}  

}
5
  • yup you pretty much just overwrite it and make all subsequent positions n-1. Welcome to the wonderful land of C
    – c0d3junk13
    Commented Aug 10, 2014 at 18:12
  • 1
    If you have an array of the structure, then you do, indeed, delete an element by shuffling the ones after down one space. If you have different organizations for the memory, you do it differently. If you have an array of pointers to the structures, you delete (free) the structure and shuffle the pointers down. If you have a linked list, you detach the item to be deleted from the list and then delete (free) the removed structure. Etc. Commented Aug 10, 2014 at 18:13
  • 1
    A simple array cannot have elements inserted or deleted (except, approximately the first or last) without shifting the others. If you expect to do this often, you may want to use a linked list. Commented Aug 10, 2014 at 18:14
  • I dont know it is asking a lot but...can you guys write me down the code for that so i can see how that works, thanks! Commented Aug 10, 2014 at 18:15
  • Im now but im in a point i dont know how to keep doing thats why im asking, i dont really like to do that. Commented Aug 10, 2014 at 18:35

1 Answer 1

0

Given some structure type struct Data, and an array a of that structure type:

struct Data a[20];
int n_data = 0;

…add data to array, incrementing `n_data`…

/* Delete item d from array */
assert(n_data > 0 && d >= 0 && d < n_data);
for (j = d + 1; j < n_data; j++)
    a[j-1] = a[j];
n_data--;

This is the simple-minded way to do it. An alternative is to use memmove() — and not memcpy() because in general the areas being moved overlap — like this:

assert(d >= 0 && d < n_data);

memmove(&a[d], &a[d+1], n_data - d - 1);
n_data--;

Note that if there is allocated memory in the structure (e.g. a char * member), you need to free any allocated memory from the deleted structure before you overwrite it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.