char vetor[10];
vetor = 10 /*ERROR*/
Why is this happening? Is it about vectors being composed?
char vetor[10];
vetor = 10 /*ERROR*/
Why is this happening? Is it about vectors being composed?
Assigning this way is not possible simply because in C and C ++ the array name is a pointer to the beginning of it in memory .
To clarify this question, the instructions below perform exactly the same task: print the address of the beginning of the array on the screen (in hexadecimal):
printf("0x%x\n", vetor);
printf("0x%x\n", &vetor);
printf("0x%x\n", &vetor[0]);
Output:
0xdbeedb00
0xdbeedb00
0xdbeedb00
As you can see, these instructions are equivalent. It is important to note that on your computer, the initial address of the (very) array will probably not be this, but whatever it is, the instructions will print the same result.
Very well! Knowing that the array starts at this address and also knowing that the array has 10 elements of type char
, it is easy to find out where each array element will be stored in the computer's memory:
for (int i = 0; i < 10; i++)
printf("0x%x\n", &vetor[i]);
Output:
0xdbeedb00
0xdbeedb01
0xdbeedb02
0xdbeedb03
0xdbeedb04
0xdbeedb05
0xdbeedb06
0xdbeedb07
0xdbeedb08
0xdbeedb09
In this way, the vetor = 10;
statement ends up overwriting the array start address, which is a very serious problem. Later, when using the array, the program will end up accessing parts of the memory that it does not have the right to use.
Remember that when you want to store values inside the array it is necessary to use the []
brackets to indicate the position within the array that will store a new value:
vetor[0] = 10;
vetor[9] = 100;
vetor[5] = 60;
vetor[1] = 20;
...
It is possible, but it does not do what you want. The error here is that it accesses an area of memory that it should not. An array is always accessed through an address. The value of the variable is this address. When I try to play 10 on the variable it says that the array starts at address 10 in memory, which is certainly a place you can not access,
If you want to put a value in the vector you have to access the element, something like this:
vetor[0] = 'A';
vetor[1] = 65;
*vetor = 'A';
*(vetor + 1) = 'A';
And if you want to play an entire string all it needs to do element by element, or a string , character by character. generally used to strcpy()
.
Note that pointer arithmetic considers the size to type that the pointer points to, it has a multiplication implied by sizeof(tipo apontado)
. If it had to be explicit it would write like this:
*(vetor + (1 * sizeof(char))) = 'A';
In case of char
is always 1, nor would it need, but if it was a int
, it could be 2, or 4 (most common), or more.
A vector in C declares this way:
tipo vetor[TAMANHO];
the TYPE would be the type of variable that will be that vector as integer, char, etc.
remembering that String does not exist in C , what exists is the char style
If you want to declare an array of the string style you will have to assign each vector position to only 1 character of that string that would look like this:
vetor[0] = 'T';
vetor[1] = 'E';
vetor[2] = 'S';
vetor[3] = 'T';
vetor[4] = 'E';
When you start the vector, the text TEST appears at the end of your program.