How do I pick up two values obtained in a function through pointers?

Asked

Viewed 56 times

0

I am building a program, whose enunciation is to make the user provide 20 numbers and these are analyzed as pairs or odd. In this case, the intention is to create a vector A that takes 20 integers and then create two more vectors containing the even (B) and odd (C) numbers. I thought about asking this question with dynamic allocation, but I can’t think of a way to get these calculated values within the function (and the statement asks for it to be the operations).

My code is this:

#include <stdio.h>
#include <stdlib.h>

int parImpar (int *x);

int main()
{
    int a[5], **par, **impar;
    for (int i =0; i<5; i++){
        scanf ("%d", &a[i]);
    }
    parImpar(a);
    
}

int parImpar (int *x){
    int *B, *C;
    int **par, **impar; 
    int contPar=0 , contImpar=0;
    for (int z = 0; z<5; z++){
        if (x[z]%2 ==0){
            contPar++;
        }
        else{
            contImpar++;
        }
    }
    B = (int*) malloc(contPar*sizeof(int));
    C = (int*) malloc(contImpar*sizeof(int));

    //Tentei fazer usando ponteiro de ponteiros, mas não saiu tbm kkkkk
    **par = &B; 
    **impar = &C;
}

1 answer

-2

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

int par = 0, impar = 0;

int* pegandoNumerosImpares(int vetorImpar[]){ // int* <--- esse * tá especificando para nossa função que vamos retorna um ponteiro.
   int *vetor_impar;
   
   vetor_impar = calloc(TAM, sizeof(*vetor_impar)); // Sobre o calloc lembrando que quando a função aloca os blocos
                                                    // de memória, ele já vai inicializar eles com 0 cada um.
   if(vetor_impar == NULL){
        printf("Alocação mal sucedida");
   }

    for(int i = 0; i < TAM; i++){
        if(vetorImpar[i] % 2 != 0 ){
            vetor_impar[i] = vetorImpar[i];
            impar++;
        }
    }

    return vetor_impar;
}

int* pegandoNumerosPares(int vetorPar[]){
   int *vetor_par;
   
   vetor_par = calloc(TAM, sizeof(*vetor_par));

   if(vetor_par == NULL){
        printf("Alocação mal sucedida");
   }

    for(int i = 0; i < TAM; i++){
        if(vetorPar[i] % 2 == 0 ){
            vetor_par[i] = vetorPar[i];
            par++;
        }
    }

    return vetor_par;
}

int main(){
    int vetor[TAM], *vetor_par, *vetor_impar, i;

    for(i = 0; i < TAM; i++){
        printf("Preenchar o vetor[%d]: ", i);
        scanf("%d", &vetor[i]);
        __fpurge(stdin);// fflush(stdin); no window.
    }

    vetor_par = pegandoNumerosPares(vetor);
    vetor_impar = pegandoNumerosImpares(vetor);

    printf("\n\n\n");
    printf("-------------\\ Números Pares //-------------\n\n");
    for(i = 0; i < TAM; i++){
        if(*(vetor_par+i) != 0){
            printf("vetor[%d]: %d \n", i, *(vetor_par+i));
        }
    }
    printf("Números de elementos pares: %d", par);

    printf("\n\n\n");
    printf("-------------\\ Números Ímpares //-------------\n\n");
    for(i = 0; i < TAM; i++){
        if(*(vetor_impar+i) != 0){
            printf("vetor[%d]: %d \n", i, *(vetor_impar+i));
        }
    }
    printf("Números de elementos Ímpares: %d", impar);

  return 0;
}

Hello Ingrid, your code shows some errors:

int parImpar (int *x){

You do not need to pass a pointer as a parameter to take an array as an argument. Just pass the field of the vector as an argument that you will pass the address of the first position of the vector, since either by dynamic or static allocation we will have a sequence of adjacent blocks (one on the other side) in memory.

Take a look at this code!

 return vetor_par;

This return of a pointer, will be passing the first position of the block of memory allocated. For to change the position of the memory block, from one position to another example position:

*(vetor+i)

During compilation, the expression *(vetor+i) is transformed into *(vetor + i * sizeof(*vetor)) (Only agreeing on the level of knowledge).

If you have difficulty in pointer arithmetic take a look at this website!

  • Hello, Arthur! Mt thanks for the clarifications on this question, I liked mt the way you did! But it is because sometimes I run into this problem of in a function having two values, so I wanted to know how to return them to main, without the need to create two of them.

  • Well Ingrid, a function only returns one value at a time. Then I advise you to pass these values as reference, if these arguments are common variables pass as the address operated & x(&num). If they are pointers just pass their cup without the need of & ex x(ptr). There is another possibility that would leave these values as global variables. (I hope I helped.)

Browser other questions tagged

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