What is the difference in the assignment of an already started matrix to an uninitiated one?

Asked

Viewed 82 times

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?

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

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

  • 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.

2 answers

2

char nome[10]; nome = "w" --- Here occurs a Warning, why this ?

You’re not copying the "w" for the area reserved for nome, the copy is made with the function strcpy(). See more about in Difference between char array and char pointer and others linked on the subject. The Warning indicates that you are doing a potentially wrong operation, and indeed is wrong.

char nome_dois[10]; nome_dois[0] = "w" --- Here it’s normal, like expected.

Normal more or less, it will even work, but there may be situation that this bursts 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 the stack). See more in Using static or dynamic variables in my codes? What would be more efficient or better seen in the job market? and Dynamic allocation in C - allocating without knowing the total amount of elements.

when I call printf("%p", "w") prints a hexadecimal value which is the representation of "w" then and a constant in a static area?

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

  • actually it was supposed to be name_dois[10] = "w" because it’s right?

  • This is without context

  • I’m sorry I’m being "dumb," but see if my definitions are correct, the identifier of a one-dimensional matrix and a pointer pointing to the data stack and each element of that matrix and a pointer pointing to a static area that contains a constant or and an address that contains a constant that has been copied from the statistical area to area automatic?

0

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

Browser other questions tagged

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