"w" is in a static memory?
Yes, strings written "directly in 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, variable initialization and assignment are different things, even though both 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 you say that C is a simple and elegant language: -)
To make copies of strings after the variable is already declared, use strcpy or memcpy.
Is [0] name in a dynamic or static memory?
Depending on how nome
has been declared, it can be in an area of global variables, or in the automatic function call stack. These two are the most common cases, but in fact can be anywhere, after all, until "w" [0] is valid! (indexing a static string)
nome_dois[0] = "w"
Referencing "w" in the code in this situation, as in most cases (except initialization, as seen above), generates a pointer to the static string. If you use pointer operations on "w", fine. But here you assigned the pointer to a char (one of the array elements), which gave me the warning in GCC:
warning: assignment makes integer from pointer without a cast
(it calls the integer char because chars in C are like small integers)
So remember to compile with warnings enabled!
nome_dois[10] = "w"
If you do, we will have a combination of small errors that can lead to a beautiful disaster in your program. The first error is the same as above, unwanted conversion from pointer to integer, and the second error is to write a non-existent array element (if declared with [10], it goes from [0] to [9] - the wonders of indexing starting with zero ...)