C - How to pass a vector in which contains pointers to struct as a function parameter?

2

First I created a struct vector. Next I have created a vector of pointers, in which each element of this vector points to each element of the struct vector.

Finally, I need to pass both the vector of structs and the vector of pointers as parameters of a function. I've tried some of the ways of searching here on the site, but it was an error. I just do not know how to do it.

Follow the code:

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

struct dados
{
   int dia, mes, ano;
   char nome_mes[50];
   char remetente[100];
   char destinatario[100];
};

void ordenar(struct dados *cartas, ***O QUE COLOCAR AQUI***, int n){
   //DUVIDA NESSA FUNÇÃO
}


void main(){

    int n, i;
    char lixo[5];
    scanf("%d\n", &n);

    struct dados cartas[n], *ponteiros[n];

    for(i = 0; i < n; i++){
    ponteiros[i] = &cartas[i];
    }

    for(i = 0; i < n; i++){
        scanf("%d de ", &cartas[i].dia);
        scanf("%s", cartas[i].nome_mes);
        scanf("%s", lixo);
        scanf(" %d\n", &cartas[i].ano);
        gets(cartas[i].remetente);
        gets(cartas[i].destinatario);
    }

    //DÚVIDA AQUI !!!
    ordenar(cartas, ponteiros, n);

}

How to do this?

    
asked by anonymous 17.09.2018 / 05:52

1 answer

2

You have two possible syntaxes to do this. The first one is identical to the one you used for the cartas parameter like this:

void ordenar(struct dados *cartas, struct dados **ponteiros, int n){
//                                               ^---

As being a pointer pointer.

Another way is to use pointer array notation, almost the same as when you declared:

void ordenar(struct dados *cartas, struct dados *ponteiros[], int n){
//                                              ^---------^

In both forms it is always necessary to pass the size to be able to go through the amount of elements that exist, and the use of the parameter within function a will be the same.

As a last resort, if the code is not for recreational and / or educational purposes, remember that the qsort to sort vectors, which greatly simplifies the work.

    
17.09.2018 / 11:43