-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
 }
9
  • 1
    I gave up reading this code too. Have you heard of indentation? Commented Nov 2, 2017 at 14:20
  • Please format your code correctly. Commented Nov 2, 2017 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. Commented Nov 2, 2017 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.... Commented Nov 2, 2017 at 14:25
  • 1
    Didn't you ask the same question 2 days ago? Commented Nov 2, 2017 at 14:33

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

YESS YESS YESS !! this is exactly what i wantedddd..where ever you are !!!! i hope you have an awsome day!!!!
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
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.