pointers

O

Olaf El Blanco

I thought i was really understanding the pointers...
So I take this exercise from a spanish university and i can't do it just
with pointers....
I don't want to spent a byte more than i need.

Thank you


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


struct empresa {
        int id_emp;
        char nombre[15];
        char direc_emp[30];
        long telefono;
};

/* empresa = enterprise
nombre = name
direc_emp = address
telefono = telephone number
*/


typedef struct empresa Empresa;

/* Return and enterprise=empresa */
Empresa agregar();

/* show the enterprise */
void mostrar(Empresa x);

/* Add some enterprises for not write in all tests */
void agregando(Empresa *listado, int *cantidad);

/* show the enterprises list */
void ver_listado(Empresa *listado, int cantidad);

/* program */
int menu(Empresa *listado, int *cantidad);

/* Show a menu */
void imprime_menu(void);

/* Inside the menu */
void MENU_agregar(Empresa *listado, int *cantidad);
void MENU_eliminar(Empresa *listado, char *nombre);
void MENU_ver(Empresa *listado, char *nombre);

int main()
{
    /* enterprises array */
    Empresa *listado;
    /* how many enterprises we have at the moment? */
int cant = 1;
    /* Space for one, minimun one (why not cero? */
    listado=(Empresa*)malloc(sizeof(Empresa)*cant);
    /* Adding some enterprises for test */
    agregando(listado, &cant);
    /* Start the program */
    menu(listado, &cant);
    getch();
    return 0;
}


*****************************************************************************************
/* show list, why I need the numbers of enterprises at the moment, for print
the list? why I can't use while (listad != NULL)?
void ver_listado(Empresa *listado, int cantidad)
{
     int n;
     for (n=0; n<cantidad; n++)
      mostrar(*(listado+n));
     getch();
}
*****************************************************************************************
/* Addinf enterprises */
void agregando(Empresa *listado, int *nueva_cantidad)
{
     /* If you add any paragrapah, increase 'parrafos' */
     int parrafos = 4;
/* More memory for more enterprises */
     listado = (Empresa *)realloc(listado, (sizeof(Empresa) * parrafos));

     (*(listado+0)).id_emp = 0001;
     strcpy ( (*(listado+0)).nombre, "WotansFOLK SRL");
     strcpy ( (*(listado+0)).direc_emp, "Bennisa 11 PB \"E\"");
     (*(listado+0)).telefono = 335256981;

     (*(listado+1)).id_emp = 0002;
     strcpy ( (*(listado+1)).nombre, "Audery gayo y CIA");
     strcpy ( (*(listado+1)).direc_emp, "Salto de la Madri");
     (*(listado+1)).telefono = 687267122;

     (*(listado+2)).id_emp = 0044;
     strcpy ( (*(listado+2)).nombre, "Skywalker");
     strcpy ( (*(listado+2)).direc_emp, "Death STAR");
     (*(listado+2)).telefono = 335256981;

     (*(listado+3)).id_emp = 0022;
     strcpy ( (*(listado+3)).nombre, "Zorra golfa");
     strcpy ( (*(listado+3)).direc_emp, "San Joan d Alacant");
     (*(listado+3)).telefono = 6668122;

/* new numbers of enterprises */
     *nueva_cantidad = parrafos;
}
*****************************************************************************************
Empresa agregar(void)
{
/* I use an auxiliar register, the one that ill return */
 Empresa aux; 
 printf ("ID             : "); scanf ("%d", &aux.id_emp);
 printf ("Nombre   : "); scanf ("%s", aux.nombre);
 printf ("Direccion : "); scanf ("%s", aux.direc_emp);
 printf ("Tel           : "); scanf ("%l", &aux.telefono);
 return aux;
}
*****************************************************************************************
/* Show enterprise */
void mostrar(Empresa x)
{
    printf ("%s\n", x.nombre);
 printf ("\t%s\n\t%d(ID)\n\tTel:%d\n", x.direc_emp, x.id_emp, x.telefono);
}
*****************************************************************************************

void imprime_menu(void)
{
   system ("cls");
   printf ("\t1. Add Enterprise\n");
   printf ("\t2. Delete Enterprise //not yet//\n");
   printf ("\t3. Show enterprise\n");
   printf ("\t4. Show full enterprise list \n");
   printf ("\t5. Exit\n");
}
*****************************************************************************************
int menu(Empresa *listado, int *cantidad)
{
/* choice */
   char eleccion;
/* How many enterprises? */
   int cant = *cantidad;
/* Show menu */
   imprime_menu();
   while (eleccion != '5') {
      imprime_menu();
      eleccion=getch();
      switch (eleccion) {
          case '1' : MENU_agregar(listado, &cant);
                     break;
/*        case '2' : break;
          case '3' : break;  */
          case '4' : ver_listado(listado, cant);
                     break;
          case '5' : return 0;

      }
   }
}
*****************************************************************************************
/* Add one, is the error in here? */
void MENU_agregar(Empresa *listado, int *cantidad)
{
     ++*cantidad;
     listado = (Empresa *)realloc(listado, (sizeof(Empresa) * (*cantidad)));
     *(listado+(*cantidad)) = agregar();
     printf ("Cantidad actual de Empresas en listado: %d\n", *cantidad);
     system ("pause");
}
*****************************************************************************************







*****************************************************
"...Winds and storms, embrace us now,
Lay waste the light of day.
Open gates to darker lands...
We spread our wings and fly away..."
*****************************************************
 
G

Guest

the scanf ("%l", &aux.telefono);in functhion Empresa agregar(void)
doesn`t been performed
 
P

Pedro Graca

Olaf said:
I thought i was really understanding the pointers...
So I take this exercise from a spanish university and i can't do it just
with pointers....
I don't want to spent a byte more than i need.

Thank you


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


struct empresa {
        int id_emp;
^ ^ ^ ^ ^ ^ ^ ^ ^
UB! The character with decimal code '240' invokes UB when not part of a
identifer, character constant, string literal, header name, comment, or
preprocessing token.
I replaced them with a space for the following code.
char nombre[15];
char direc_emp[30];
long telefono;
}; [snip comments]
typedef struct empresa Empresa; [snip prototypes]
int main()
{
/* enterprises array */
Empresa *listado;
/* how many enterprises we have at the moment? */
int cant = 1;
/* Space for one, minimun one (why not cero? */
listado=(Empresa*)malloc(sizeof(Empresa)*cant);

Do not cast the return from malloc and friends.
The pointer returned from malloc and friends is suitable for any kind of
variable. Also you need to test if malloc failed.
/* Adding some enterprises for test */
agregando(listado, &cant);
/* Start the program */
menu(listado, &cant);
getch();

This function is not part of the standard library.
Also there's no prototype in scope for it.
return 0;
}


*****************************************************************************************

Compile error.
/* show list, why I need the numbers of enterprises at the moment, for print
the list? why I can't use while (listad != NULL)?

Comment not closed.
void ver_listado(Empresa *listado, int cantidad)
{
int n;
for (n=0; n<cantidad; n++)
mostrar(*(listado+n));
getch();
}

Snip the rest. Taking the "|" characters away is a lot of trouble.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top