How to manipulate a vector of structs in an external function? Follow example

1

I am doubtful in an issue. The following is the statement:

Implement a library control system with functions to register, query, change, and remove books.

The attributes of a book are:

  • Name (Book title, with spaces)
  • Author (Full name with spaces)
  • ISBN (11 digit number)
  • Volume (Roman numeral)
  • Date
  • Publisher (Text with space)
  • Number of Pages (Integer)

And the struct I created for this problem was this:

struct informacoes
{
    char nome[100];
    char autor[100];
    long long int isbn;
    char volume[10];
    int data;
    char editora[100];
    int paginas;
};

I would like to know how to pass a vector of this struct as a parameter to a function, can anyone help me?

    
asked by anonymous 04.07.2017 / 22:59

1 answer

0

The vector is passed as if it were a vector of another normal type such as int . So:

void func(struct informacoes arr[]){
     //fazer algo com o array de informações
}

int main(){
     struct informacoes arr[10];

     func(arr); //chamando so com o nome do array
     return 0;
}

With pointers it would become:

void func(struct informacoes *arr){
     //fazer algo com o array de informações
}

int main(){
     struct informacoes *arr = malloc(sizeof(struct informacoes)*10);

     func(arr); //chamando so com o nome do ponteiro
     return 0;
}

It is important to mention that passing the normal vector or the pointer is actually the same thing. Vector notation is just an easier way to manipulate the array, the compiler then treats everything as pointers.

    
04.07.2017 / 23:06