Pergunta

Suppose we have a line in which is an array definition:

char v[100];

What is the subsequent of evaluating this definition? I think that char v definition evaluated first and further to uninitialized variable v operator [] is applied. But it is nonsense. I want to understand, why this definition returned the char*. Please give if it possible references to spec.

Foi útil?

Solução

char v[100] is a variable declaration.

There is no statement or expression here, hence nothing needs to be evaluated.

char v[100] = "abc" would be evaluated.

v[3] = 'd' would be evaluated.

return v[4] would be evaluated.

But not char v[100].

For example, here is how char v[100] = "abc" is evaluated by the Microsoft Visual C++ compiler:

    char v[100] = "abc";
001B1DA8  mov         eax,dword ptr [string "abc" (1B695Ch)]  
001B1DAD  mov         dword ptr [ebp-6Ch],eax  
001B1DB0  push        60h  
001B1DB2  push        0  
001B1DB4  lea         eax,[ebp-68h]  
001B1DB7  push        eax  
001B1DB8  call        @ILT+135(_memset) (1B108Ch)  
001B1DBD  add         esp,0Ch  

You can view the disassembly of char v[100] for yourself, and see that there is "no code behind it".

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top