Compiler error when trying to reset vector

Asked

Viewed 389 times

5

Compilers error when I try to reset an array with the command vetor[10] = {0};

And he only makes a mistake in that line because I also used it in the assignment int vetor[10] = {0} and there does not accuse error.

The GCC displays:

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

The G++ gives:

Warning: Extended initializer lists only available with -Std=c++11 or -Std=gnu++11 [enabled by default] drawings[4] = {0};

  • Put your code in for us to see. Only with this you can not understand well where the problem is.

  • 2

    Note that an array defined with int vetor[10] does not have the element vetor[10] ... the elements of this array go from vetor[0] to vetor[9].

3 answers

5


I made this code and tested it:

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

Compiling in C doesn’t really work because C doesn’t have extended initiators. The syntax of C allows this syntax to be used in the declaration but cannot be used in later assignment, so it explains why the first one you quoted gives problem and the second example your works. See on ideone.

Already 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 language version difference can change the result. You cannot use what is not in the default, not invented yet, or not implemented in the compiler you are using. In C there is no solution but to initialize the elements in the traditional way. In C++ just choose to compile with the newest version available in GCC.

The actual solution only allows zero the array on startup. Then you can allocate numbers on all elements through a loop. In this case it doesn’t necessarily have to be a zero. Another option is to call the function memset() which also only allows zero all, after all it was made to work with strings and not numbers (actually the initialization of the whole array is replaced internally by a 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]);
}

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

I’ll be better if I have more details.

2

the stretch

int vetor[10] = {0};

is only to start the variable

to reset the array in another part of the code you can use memset

memset(vetor, 0, sizeof(vetor));
  • The memset(vetor, 0, sizeof(vetor)/sizeof(int)) does not incialise all elements of vetor. For that use memset(vetor, 0, sizeof(vetor));.

  • http://ideone.com/YxBwl

  • truth in my example to catching the number of vector elements and not the total memory size

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}) on a boot; you have to use a value normal on an assignment.

My answer applies to C. I don’t know C++.

Browser other questions tagged

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