-4

i'm trying to pass an array of stract to a function, but i just gave up. i´m trying to store data inside the structure function "fun()", so i can later pull up the data in function lo(); when ever i need too.

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


struct Operador
{

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

struct Operador* fun( ) {// im using this function to store the data

    struct Operador* pItems = malloc(3 * sizeof(struct Operador));//is it necessary to use malloc
    int 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(struct Operador pItems)//and this function to display the data
{
    struct Operador Items = pItems;     
    int j;
    printf("\n\n");
    printf("Name is: %s \n", Items->nome);
    printf("telefone is: %s \n", Items->telefone);
    printf("age is: %s \n", Items->idade);
    printf("\n\n");

    return pItems;
 }
 main()           
 {

    fun(); //here i call out the function for the user to type in information
    printf("\n\n click any key to see data");
    system("pause");
    lo(); // and this function is supposed to display information
 }
Karthick
  • 1,010
  • 1
  • 8
  • 24
MARCIO QUITEQUE
  • 75
  • 1
  • 1
  • 8
  • 1
    I gave up reading this code too. Have you heard of indentation? – Eugene Sh. Nov 02 '17 at 14:20
  • Please format your code correctly. – Jabberwocky Nov 02 '17 at 14:20
  • Please read up on how to use pointers. Because you keep using instances of the struct, but try to use a pointer to access. – AntonH Nov 02 '17 at 14:21
  • Please read a C tutorial and come back when you have understood the difference between a simple variable, an array and a pointer. This: `struct Operador fun( ) { struct Operador pItems[2] ; struct Operador* pItems = malloc(3 * sizeof(struct Operador)); ... return pItems;}` contains 4 errors in 4 lines.... – Serge Ballesta Nov 02 '17 at 14:25
  • @SergeBallesta ..damn – MARCIO QUITEQUE Nov 02 '17 at 14:29
  • 1
    Didn't you ask the same question 2 days ago? – ad absurdum Nov 02 '17 at 14:33
  • i know guys i need to get some studying down – MARCIO QUITEQUE Nov 02 '17 at 14:35
  • @MARCIOQUITEQUE: Seriously, fun should return a pointer to an allocated array, because C functions cannot return an array (only a pointer to it) and returning a pointer to an automatic array (neither static, nor dynamically allocated) ends in a *dangling* pointer because its memory is freed at the end of the function. And a single identifier cannot be a true array and a pointer at the same time. – Serge Ballesta Nov 02 '17 at 14:50
  • @DavidBowling lmao my homework was more complex then i thought – MARCIO QUITEQUE Nov 02 '17 at 14:50

1 Answers1

1

You probably want this:

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

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

struct Operador *fun()
{
  struct Operador* pItems = malloc(sizeof(struct Operador));  // allocate space for ONE structure
  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(struct Operador *pItems)
{
  printf("\n\n");
  printf("Name is: %s \n", pItems->nome);
  printf("telefone is: %s \n", pItems->telefone);
  printf("age is: %s \n", pItems->idade);
  printf("\n\n");
}    

int main()
{
  struct Operador *op = fun(); // op points to new structure filled in by user

  lo(op);                      // display structure
  free(op);                    // free structure
  system("pause");
}

This code is still bad (no error checking, usage of %s format specifier without length limitation, poor choice of names, age field as a string instead of an int and probably a few more), but it works as expected.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • YESS YESS YESS !! this is exactly what i wantedddd..where ever you are !!!! i hope you have an awsome day!!!! – MARCIO QUITEQUE Nov 02 '17 at 14:33
  • if i free the structure i wont be able to reuse the data correct, cause the purpose of this whole program is to be able to see the data stored when i need to – MARCIO QUITEQUE Nov 02 '17 at 14:53
  • Of course you don't free the structure while you still need it. In my answer the structure is freed at the end of the program, where we don't need it anymore. Generally anything you allocate with `malloc` should be freed at some point, but latest at the end of the program. Read the chapter dealing with dynamic memory allocation in your C text book. – Jabberwocky Nov 02 '17 at 14:58