#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.
– Ingrid Amorim
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 & exx(ptr)
. There is another possibility that would leave these values as global variables. (I hope I helped.)– Arthur_cant