Compiler reports error when I try to zero vector

5

The compilers accuse error when I try to zero a vector with the command vetor[10] = {0};

And it only accuses error in this line because I also used it in the int vetor[10] = {0} attribution and there does not acknowledge error.

GCC displays:

  

error: expected expression before '{' token   vector [10] = {0};

G ++ gives:

  

warning: extended initializer lists only available with -std = c ++ 11 or -std = gnu ++ 11 [enabled by default]      serves [4] = {0};

    
asked by anonymous 24.03.2015 / 21:15

3 answers

5

I made this code and tried:

int main(void) {
    int vetor[10] = {0};
    vetor[10] = {0}; //há um erro aqui mas compila em C++
    return 0;
}

Compiling in C does not really work because C does not have extended initializers . The syntax of C allows this syntax to be used in the declaration but can not be used in later assignment, so it explains why the first one you mentioned gives problem and the second example of yours works. See in the ideone .

If you do the same thing using a modern C ++, at least C ++ 11, this feature is available and can be used. See on ideone . Note that the error message shows that you should use at least C ++ 11.

Understand that C and C ++ are different languages. And even the language version difference can change the result. You can not use what is not in the default, has not been invented yet or has not been implemented in the compiler you are using. In C there is no solution other than initializing the elements in the traditional way. In C ++ just choose to compile with the newer version available in GCC.

The solution actually only allows to zero the array at startup. Then you can allocate numbers on all elements through a loop. In this case it need not necessarily be a zero. Another option is to call the function memset() which also only allows to zero all, after all it was made to work with strings and not numbers (actually the initialization of all array is internally replaced by memset() ). See correct examples:

#include <stdio.h>
#include <string.h>

int main(void) {
    int vetor[10] = {0};
    for (int i = 0; i < 10; i++) {
        printf("%d", vetor[i]);
    }
    for (int i = 0; i < 10; i++) {
        vetor[i] = 1;
    }
    for (int i = 0; i < 10; i++) {
        printf("%d", vetor[i]);
    }
    memset(vetor, 0, sizeof(vetor));
    for (int i = 0; i < 10; i++) {
        printf("%d", vetor[i]);
    }
    return 0;
}

See running on ideone .

I get better if I have more details.

    
24.03.2015 / 22:35
2

the stretch

int vetor[10] = {0};

is just to start the variable

To zero the array in another part of the code you can use memset

memset(vetor, 0, sizeof(vetor));
    
26.03.2015 / 14:16
1
int vetor[10] = {0}; // inicializacao
vetor[10] = 0; // atribuicao (vetor[10] nao "existe")

Initialization and assignment are different operations.

You can use {0} (or {1, 2, 3} ) at an initialization; you have to use a normal value in an assignment.

My answer applies to C. I do not know C ++.

    
24.03.2015 / 22:33