Global C ++ variable [closed]

1

I have this code

int main() {
    ListaVend lista;
    criarLista(&lista);
    for(int i = 0; i <= VEND_MAX; i++){
        Vendedor vend;
        vend.codVend = i;
        vend.nome = "Vendedor "+to_string(i);
        vend.salFinal = i;
        inserLista(&lista, &vend, i);
    }
    showLista(lista);

That way it works normal. Now I wanted Variavel to be accessed outside the mainstream as well, how can I do that?

Ps.

ListaVend lista;
int main() {
    criarLista(&lista);
    for(int i = 0; i <= VEND_MAX; i++){
        Vendedor vend;
        vend.codVend = i;
        vend.nome = "Vendedor "+to_string(i);
        vend.salFinal = i;
        inserLista(&lista, &vend, i);
    }
    showLista(lista);

This way you do not save any information in the List.

    
asked by anonymous 21.09.2016 / 01:12

1 answer

3

Overall your program is right, with the following caveats:

these functions

bool cheiaLista(ListaVend);    // BAD
bool vaziaLista(ListaVend);    // BAD
void showLista(ListaVend);     // BAD

should be declared with pointer or reference parameters, so

bool cheiaLista(ListaVend*);   // GOOD
bool vaziaLista(ListaVend*);   // GOOD
void showLista(ListaVend*);    // GOOD

or so

bool cheiaLista(ListaVend&);   // GOOD
bool vaziaLista(ListaVend&);   // GOOD
void showLista(ListaVend&);    // GOOD

to avoid copies in the passages of the parameters for these functions, which is an extremely business (in addition, in the general case, enforce copy copiers).

In addition there is a logic error: this here

for (int i = 0; i <= VEND_MAX; i++) {

It should be like this

for (int i = 0; i < VEND_MAX; i++) {

The way you've implemented your functions even seems to work, but this i <= VEND_MAX comparison in C ++ is counterintuitive, gets in the way of thinking, and is creating an additional salesperson that is not being added to the list.     

21.09.2016 / 02:50