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

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 errors posted are:

    
asked by anonymous 09.04.2015 / 11:51

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 it to be an array but rather the variable should indicate this. It's kind of weird but it's like that.

A function can not return an array , you could return a pointer but would have difficulty throwing it in array again. But think about it, when you pass an array as a parameter, in the background you are passing a pointer, that is, the array is passed by reference, so any changes made to it already will be present in the array that you passed since the change is made to the memory address from where it was created, there is no copy of the array for the called function. So you do not have 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;
}

See working on ideone .

    
09.04.2015 / 12:27