I can not print, in the main function, values coming from a vector created in another function received by a double pointer

3

The following source code is the miniaturization of a major problem that I am working on, and I have not been able to solve the problem for two days.

I need to print the values of the vector generated by the "makeVector" function, in the "main" function.

But for the code to resemble the actual problem I'm working on, there are two restrictions that must be met. These are discussed in the following code.

#include <stdio.h>
#include <stdlib.h>
#define TAM 3

int fazVetor(int **vet){
    int *array = malloc(sizeof(int) * TAM);

    array[0] = 4;
    array[1] = 7;
    array[2] = 8;

    /* nesta função somente a linha a seguir PODE ser alterada. */
    *vet = array;
}

int main()
{
    int **qq;

    /* Na função main, somente a linha a seguir NÃO PODE ser alterada. */
    fazVetor(&qq);

    printf("\n--==[Valores do Vetor]==--\n\n");
    for(int i = 0; i < TAM; i++){
        printf(" %d", (qq[i]));
    }

    printf("\n\n");
    return 0;
}

The above code works, but not the way it should. That is, I can not print the three vector values. At most, I can print out memory addresses.

If anyone can help, it will be of great value!

    
asked by anonymous 03.06.2018 / 21:05

1 answer

4

In the main function, the qq variable is declared as a double pointer, and by calling the fazVetor() function with the &qq operator, it generates a triple pointer.

Since the fazVetor() function is given a double pointer, this generates the error when printing the contents of the vector.

The solution is to change the declaration of variable qq to simple pointer (with only * ):

int *qq;

After this change, the result prints correctly:

--==[Valores do Vetor]==--

 4 7 8
    
03.06.2018 / 21:47