pointers

Discussion in 'C Programming' started by Olaf El Blanco, Mar 8, 2006.

  1. 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..."
    *****************************************************
    Olaf El Blanco, Mar 8, 2006
    #1
    1. Advertising

  2. the scanf ("%l", &aux.telefono);in functhion doesn`t been performed
    =?utf-8?B?6Zq8?=, Mar 8, 2006
    #2
    1. Advertising

  3. the scanf ("%l", &aux.telefono);in functhion Empresa agregar(void)
    doesn`t been performed
    =?utf-8?B?6Zq8?=, Mar 8, 2006
    #3
  4. Ok, sorry it was a %d

    隼 <> el Miércoles, 8 de Marzo de 2006 14:28 wrote:

    > the scanf ("%l", &aux.telefono);in functhion Empresa agregar(void)
    > doesn`t been performed
    Olaf El Blanco, Mar 8, 2006
    #4
  5. Olaf El Blanco

    Pedro Graca Guest

    Olaf El Blanco wrote:
    > 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.

    --
    If you're posting through Google read <http://cfaj.freeshell.org/google>
    Pedro Graca, Mar 8, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil
    Replies:
    1
    Views:
    644
    llewelly
    Sep 16, 2003
  2. muser
    Replies:
    3
    Views:
    760
    Ron Natalie
    Sep 18, 2003
  3. A
    Replies:
    3
    Views:
    455
    Alan Kelon
    Oct 29, 2003
  4. Xamalek
    Replies:
    7
    Views:
    685
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    672
Loading...

Share This Page