0

I'm trying to call two functions, the first function will receive data and the second will show the data. But my second function is equal to the first, so it doesn't do what I intend to do. Any suggestions guys?

  struct Operador
{

    char nome[32];
    char telefone[15];
    char idade[3];
};

struct Operador* fun( ) {
    struct Operador* pItems = malloc( 3 * sizeof(struct Operador));
    int n;
    for(n=0;n<1;n++){
        printf(" name: "); gets(pItems[n].nome);
        printf(" telefone: "); gets(pItems[n].telefone);
        printf(" age: "); gets(pItems[n].idade);
    }
    return pItems;
}


//*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*fim_operador
void lo()
{
        struct Operador* pItems =fun();
    int j;
    printf("\n\n");
        for(j=0;j<1;j++){

    printf(pItems[j].nome);
    printf(pItems[j].telefone);
    printf(pItems[j].idade);
    printf("\n\n");
    }
free(pItems);
}







 main()           
{


    fun();
    lo();/ i want this function to simply display data
MARCIO QUITEQUE
  • 75
  • 1
  • 1
  • 8

1 Answers1

0

Some alterations in your code:

  • in function fun() instead of gets maybe it would be better to use scanf.
  • in function fun() and lo() you access your pointer to a struct with the '.' operator. you should use the '->' operator instead.
  • i do not understand why you use a for loop to get and print the values while you have only one value but i suppose you have your reasons (maybe to add more structs at a future implementation.

below i suggest a code you could use:

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


struct Operador
{
  char nome[32];
  char telefone[15];
  char idade[3];
};

struct Operador* fun() {
  struct Operador* pItems = malloc(3 * sizeof(struct Operador));
  //int n;
  //for (n = 0; n<1; n++){
     printf(" give nome: ");
     scanf("%s", pItems->nome);
     printf(" give telefone: ");
     scanf("%s", pItems->telefone);
     printf(" give age: ");
     scanf("%s", pItems->idade);
 //}
return pItems;  
}


//*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-

void lo(void)
{
   struct Operador* pItems = fun();
  //    int j;
 printf("\n\n");
 // for (j = 0; j<1; j++){      //no need for "for"?

    printf("Name is: %s \n", pItems->nome);
    printf("telefone is: %s \n", pItems->telefone);
    printf("age is: %s \n", pItems->idade);
    printf("\n\n");
//  }

    free(pItems);
    return; 
}




main()
{
  //fun(); //no need to call, is called in lo()
  lo();
}

the output is:

give nome: jim
give telefone: 2673673
give age: 32


Name is: jim
telefone is: 2673673
age is: 32


Press any key to continue . . .
Chatz
  • 56
  • 4
  • hey thanks for the correction!! and would it be possible to store the information i saved to then consult them when i need them....thats why i added the function lo() ....so lets say the program reads..............select a. to see people registered.... my plan was to then pull the lo(); function – MARCIO QUITEQUE Nov 02 '17 at 12:46
  • i suppose the simplest you can do is to return pItems, at the end of lo() just like you did with fun(). If you do something like this do not use free(pItems) before returning. However, it is not good to not use free after malloc, depends on what you want to do with your code. – Chatz Nov 02 '17 at 12:55
  • just a warning though, if you intend to heavilly use fun, and lo many times, by using malloc each time, and not freeing memory, you will possibly end up with an overflow. You will need a different approach then. – Chatz Nov 02 '17 at 13:03
  • i'm trying to create something like a database. that allows me to consult or add more data in the database – MARCIO QUITEQUE Nov 02 '17 at 13:04
  • I believe you should definitely change your code then... use fun() to insert users (without using malloc- just create a struct as an automatic variable), return an Operator struct, save it wherever you want (files would be a good idea) and alter lo() function to type each user. You don't have to call fun() in lo(), it would be better for each function to do something very specific. That is just a suggestion of course. If you are satisfied with the answer i would appreciate it if you accepted it – Chatz Nov 02 '17 at 13:14