Why is it not possible to assign a string vector after being declared a character?

Asked

Viewed 116 times

1

char vetor[10];
vetor = 10 /*ERROR*/ 

Why does this happen? It has to do with vectors being composed?

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

3 answers

4

Attributing in this way is not possible simply because in C and C++ the array name is a pointer to the beginning of the array 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]);

Exit:

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 array (very) probably won’t be this one, but whatever it is, the instructions will print the same result.

Very good! Knowing that the array starts at this address and also knowing that the array has 10 type elements char, it is easy to find out where each element of the array will be stored in the computer’s memory:

for (int i = 0; i < 10; i++)
    printf("0x%x\n", &vetor[i]);

Exit:

0xdbeedb00
0xdbeedb01
0xdbeedb02
0xdbeedb03
0xdbeedb04
0xdbeedb05
0xdbeedb06
0xdbeedb07
0xdbeedb08
0xdbeedb09

Thus, the instruction vetor = 10; ends up overwriting the address of the beginning of the array, which is a very serious problem. Later, when using the array, the program will end up accessing parts of the memory that it has no right to use.

Remember that when you want to store values within the array you need 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;
...

4

It is possible, but it does not do what you want. The error there is that it accesses an area of memory that it should not. A array is always accessed through an address. The value of the variable is this address. When you try to play 10 in the variable is saying that the array starts at address 10 of the memory, which is certainly a place that you can not access, then gives problem even.

If you want to put a value in the array you have to access the element, something like this:

vetor[0] = 'A';
vetor[1] = 65;
*vetor = 'A';
*(vetor + 1) = 'A';

And if you wish to play string all for it needs to make element by element, or a string, character by character. usually the strcpy().

Note that pointer arithmetic considers the size to type the pointer points to, it has an implicit multiplication by sizeof(tipo apontado). If I had to be explicit I would write like this:

*(vetor + (1 * sizeof(char))) = 'A';

I put in the Github for future reference.

In the case of char it’s always 1, and I wouldn’t have to, but if it was a int, could be 2, or 4 ( the most common), or more.

  • And why say vector is a compound type?

  • Because it is composed of several elements, it is not one. Then I give an improved answer, I walk with chores these days. Cover me after the carnival if I forget.

0

A vector in C of 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 the C, What exists is style char
If you want to declare a string style array 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 printing the vector as every will appear the text TEST at the end of your program.

  • Define print.

  • Show on the program output screen the famous C language "prinf("hello World");"

  • 1

    Then it will not produce the expected result.

  • i know, I just commented how it works but or less the vector, assigning character by character and dps when printing it shows the string as whole in the output

Browser other questions tagged

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