What's the difference in assigning an already started array to an uninitiated array?

0
char nome[10];
nome = "w" //Aqui ocorre um warning, por quê isso ?

char nome_dois[10];
nome_dois[0] = "w" //  Aqui e normal,  como esperado. 

"w" is in a static memory?

How much of% is in a dynamic or static memory?

When I call nome[0] it prints me a hexadecimal value that is the representation of printf("%p", "w") so is a constant in a static area?

    
asked by anonymous 25.05.2018 / 05:32

2 answers

2
  

char nome[10]; nome = "w" --- Here's a warning, why?

You are not copying the "w" to the area reserved for nome , the copy is done with the function strcpy() . See more on Difference between char array and char pointer and other linked on the subject. The warning indicates that you are doing a potentially wrong operation, and in fact it is wrong indeed.

  

char nome_dois[10]; nome_dois[0] = "w" --- Here and normal, as   expected.

Normal, more or less, it will even work, but it may have a situation where this will pop the memory reserved for this string .

  

"w" is in a static memory? nome[0] is in a dynamic memory or   static?

Yes, "w" is in (effectively) static area. nome[0] is in automatic area (which always ends up being stack ). See more on Use static or dynamic variables in my codes? What would be most efficient or better seen in the job market? and Dynamic allocation in C - allocating without knowing the quantity total elements .

  

When I call printf("%p", "w") it prints me a hexadecimal value that is the   representation of "w" then a constant in a static area?

What is printing is the address (indicated by "%p" ) where the string is and the value is constant in static area. in this area everything is constant.

    
25.05.2018 / 06:13
0
  

"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 ...)

    
23.11.2018 / 14:57