"w" is in a static memory?
Yes, strings written "directly into code" are stored in a static area. You can use these strings as pointers or by copying to dynamic buffers, for example.
char nome[10];
nome = "w"; //Aqui ocorre um warning, por quê isso ?
In C, the initialization of the variable and the assignment are different things, although the two things are done with =
.
char nome[10] = "w"; // copia a string estática "w" para uma variável na pilha (ou global)
nome = "w"; // usa a string "w" como ponteiro e tenta atribuir a um array (operação inválida na linguagem, dá erro)
Remember this difference when they say that C is a simple and elegant language :-)
To make copies of strings after the variable is already declared, use strcpy or memcpy.
name[0] is in a dynamic or static memory?
Depending on how nome
has been declared, may be in an area of global variables, or in the function call stack. These two are the most common cases, but actually can be anywhere, after all, until "w"[0] is valid! (index a static string)
nome_dois[0] = "w"
Referencing "w" in the code in this situation, as in most cases (except on startup, as seen above), generates a pointer to the static string. If you use "w" pointer operations, fine. But here you assigned the pointer to a char (one of the array elements), which gave me Warning in GCC:
Warning: assignment makes integer from Pointer without a cast
(he calls the char integer because chars in C are like small integers)
So remember to compile with warnings enabled!
nome_dois[10] = "w"
If you do that, we’ll have a combination of small mistakes that could lead to a major disaster on your show. The first error is the same as above, undesirable conversion from pointer to integer and the second error is to write to a non-existent array element (if declared with [10], it goes from [0] to [9] - the wonders of indexing starting with zero...)
Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.
– Maniero