Pass and return vector of type defined by struct as parameter of a function in C

Asked

Viewed 3,534 times

1

I’m finding errors for the following code:

#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>
#include <locale.h>
#define true 1
#define false 0

const int limite = 100;

//Definição da estrutura Conjunto como um tipo de variável Conjunto
typedef struct Conjunto {
   int valor;
   short preenchido; //Flag para identificar se o valor do struct foi preenchido
} Conjunto;

/**
Esta função faz a leitura dinâmica de um conjunto de até 100 números inteiros.
*/
Conjunto[] lerConjunto(Conjunto[] cj) {
    char resposta;
    int i;

    //Limpar o vetor de eventual lixo de memória
    for(i = 0; i < limite; i++) {
        cj[i].valor = 0;
        cj[i].preenchido = 0;
    }

    i = 0;

    //Iniciar processo de leitura de valores
    do {
        printf("Insira o %d° valor do conjunto",i);
        scanf("%d", &cj[i].valor);  //Lê o valor
        cj[i].preenchido = true;    //Indica que naquela posição do vetor houve um valor lido

        printf("\n\nDeseja Continuar? Sim, ou Não?");
        scanf("%c", &resposta);

        system("cls"); //Limpa a tela
    } while((resposta == 's' || resposta == 'S') && i < limite);

    return cj; //Retorna o conjunto lido
}

int main() {
    //Definição de linguagem para aparecer na tela (Acentos e afins)
    setlocale(LC_ALL,"portuguese");

    //Vetor de Struct Conjunto
    Conjunto cj1[100];
    cj1 = lerConjunto(cj1);

    Conjunto cj2[100];
    cj2 = lerConjunto(cj2);

    system("PAUSE");
    return 0;
}

The mistakes made are:

1 answer

2


Your problem is how to manipulate the array.

There is a syntax error in the parameter. It is not the type that should declare that it is a array but yes the variable should indicate this. It’s kind of weird but it’s like this.

A function cannot return a array, you could return a pointer but would have difficulty throwing it on array again. But think about it, when you pass a array as parameter, in the background is passing a pointer, ie, is passed the array by reference, then any change made to it, will already be present in the array that you have passed since the change is made to the memory address from which it was created, there is no copy of the array to the called function. So you don’t need to return anything.

#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>
#include <locale.h>
#define true 1
#define false 0

const int limite = 100;

//Definição da estrutura Conjunto como um tipo de variável Conjunto
typedef struct Conjunto {
   int valor;
   short preenchido; //Flag para identificar se o valor do struct foi preenchido
} Conjunto;

/**
Esta função faz a leitura dinâmica de um conjunto de até 100 números inteiros.
*/
void lerConjunto(Conjunto cj[]) {
    char resposta;
    int i;

    //Limpar o vetor de eventual lixo de memória
    for(i = 0; i < limite; i++) {
        cj[i].valor = 0;
        cj[i].preenchido = 0;
    }

    i = 0;

    //Iniciar processo de leitura de valores
    do {
        printf("Insira o %d° valor do conjunto",i);
        scanf("%d", &cj[i].valor);  //Lê o valor
        cj[i].preenchido = true;    //Indica que naquela posição do vetor houve um valor lido

        printf("\n\nDeseja Continuar? Sim, ou Não?");
        scanf("%c", &resposta);

        system("cls"); //Limpa a tela
    } while((resposta == 's' || resposta == 'S') && i < limite);

}

int main() {
    //Definição de linguagem para aparecer na tela (Acentos e afins)
    setlocale(LC_ALL,"portuguese");

    //Vetor de Struct Conjunto
    Conjunto cj1[100];
    lerConjunto(cj1);

    Conjunto cj2[100];
    lerConjunto(cj2);

    system("PAUSE");
    return 0;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • You helped me a lot, man! Thank you.

Browser other questions tagged

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