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

Asked

Viewed 647 times

1

I am doubtful on a question. Follow 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 (Date)
  • Publisher (Text with space)
  • Number of Pages (Whole Number)

And the struct that 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 wonder how to pass a vector of this struct as a function parameter, someone can help me?

  • There’s a code you’re making for us to see where you’re having trouble?

  • I do, but, this problem has been solved, the doubt I have left is about the question of strings, I can not read the strings of the code register function I will pass next

  • @Luizcampos so your doubt is now another? Another question, better another question, no?

  • It’s true, I’ll post another question

1 answer

0

The vector is passed as if it were a vector of another normal type like int for example. Thus:

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 be:

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 pointer is actually the same thing. Vector notation is just an easier way to manipulate the array, the compiler then treats everything as pointers.

  • Thank you very much, I wonder if I can do that too using a pointer, if yes, like?

  • Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.