Is a array name is a constant pointer?
if not, so what is a array name on earth?
Is a array name is a constant pointer?
if not, so what is a array name on earth?
Well the biggest reason an array isn't a constant pointer is because the underlying assembler will be different.
Declaring an array usually means that more registers will be used to store the data contiguously (although it depends on what you do with the array - if you declare an array but only use one element, the array may not exist). You can plainly see the difference in the number of instructions just to define the variables.Code:; 5 : char msg_arr[] = "hello world"; mov eax, DWORD PTR ??_C@_0M@LACCCNMM@hello?5world?$AA@ mov DWORD PTR _msg_arr$[ebp], eax mov ecx, DWORD PTR ??_C@_0M@LACCCNMM@hello?5world?$AA@+4 mov DWORD PTR _msg_arr$[ebp+4], ecx mov edx, DWORD PTR ??_C@_0M@LACCCNMM@hello?5world?$AA@+8 mov DWORD PTR _msg_arr$[ebp+8], edx ; 6 : char *msg_ptr = "hello world"; mov DWORD PTR _msg_ptr$[ebp], OFFSET ??_C@_0M@LACCCNMM@hello?5world?$AA@ ; 7 : ; 8 : char a = msg_arr[1]; mov eax, 1 shl eax, 0 mov cl, BYTE PTR _msg_arr$[ebp+eax] mov BYTE PTR _a$[ebp], cl ; 9 : char b = msg_ptr[2]; mov eax, 1 shl eax, 1 mov ecx, DWORD PTR _msg_ptr$[ebp] mov dl, BYTE PTR [ecx+eax] mov BYTE PTR _b$[ebp], dl ; 10 : }
Array access is also different from pointer access in terms of the number and manner of instructions. In array access, an array name has to be the ebp, so the compiler will simply generate instructions to compute the offset and move the result. With pointers, the address that is the pointer's value is copied to a register before the offset is computed.
Hi,
What do you think with this?
So is it or not?Code:int main ( void ) { int array[10]; printf("%p\n", array); // check this? printf("%p\n", &array[0]); // check this? return 0; }
Since printf is a function, when that call is executed, the array will go through the normal decay to a pointer to the first element. The real difference is in the allocation of the data and the manner in which the data is accessed, especially in the scope of the original array definition.
Functions decay arrays to pointers because, in C, everything is passed by value, so rather than copy the entire array to give a function the opportunity to change it, pointers are used for this purpose, because pointers are faster to copy.
Last edited by whiteflags; 07-02-2015 at 01:56 AM.
Hi,
I don't have an issue with your explanation, but what jumped at me in the OP question are the following words:
array name and "constant" pointer. Which I know if the OP understands will not be asking the question. So IMHO, answering YES or NO will not have achieve any lasting purpose.
I did not simply answer yes or no.
In fact the assembly shows the difference between an array and the pointer versions. You can see that the data is stored in contiguous registers (copying by DWORDS). The pointer version is simply storing the address of the static string. Then, the assembler shows how the access is different, as I described in the earlier post. This is the tangible difference between arrays and pointers.
Then you came along with your own question, which I answered, and you apparently do not have an issue with the way that I answered it.
It seems to me that you want to speak on the OP's behalf, that my answer is somehow inadequate. With due respect to you, I would like to hear that from him.
I believe this is a minor misunderstanding, I had seen the question and thought to ask the OP to do better on the question instead of YES or NO, answer. In that process you had given an answer to which if the OP had known what a constant pointer s/he would not asked. I had not seen yours before getting my answer/question out.It seems to me that you want to speak on the OP's behalf, that my answer is somehow inadequate. With due respect to you, I would like to hear that from him.
Of course, arrays are arrays and pointers are pointers. In as much as I do not speak for the OP, IMHO, I think if one still struggles with 2 x 2, how then would they get 2 ^ 8 i.e (2 rasied to the power of 8) concept, to use an illustration. Or what do you think?
Last edited by 2teez; 07-02-2015 at 02:13 AM.
There is nothing wrong with the OP's question. The difference between pointers and arrays are a subject of common misunderstanding. This misunderstanding comes from the fact that arrays and pointers are closely related to each other. Pointers, for example, can simulate arrays - this feature makes it possible to use them in place of arrays as function arguments. Even if you don't use functions, the difference becomes muddy when you see code like ptr[0] and arr[0]. It is easy to sympathize with a new programmer because these lines of code are so similar, and if I did not tell you that arr was an array and ptr was a pointer, you likely would not notice a difference either. You likely would not notice a difference until you tried something that you cannot do with arrays and forced the compiler to emit an error. Like arr++ emits an error ostensibly because arr is not just a single variable.
I think, then, that it is only natural to ask the question once you have some concept of both array and pointer. The issue is that they appear to be the same concept and the asker needs someone to separate the two.
I will admit though, that you might need to know what a register is to understand my answer. But you can understand my answer on a high level. Think of a register like an array element. The registers will be stored next to each other, just as the array elements appear to be in the code. It so happens that you can also see this in the assembler, because a stack of registers is all an array is. "mov" just copies data around.
Last edited by whiteflags; 07-02-2015 at 02:45 AM.
Yes, pointers and array are related but not the same. And the fact that you use a ptr[0] doesn't make ptr a pointer except you have first declared it as a pointer. ptr is just like any other variable name... if I did not tell you that arrwas an array and ptr was a pointer, you likely would not notice a difference either. You likely would not notice a difference until you tried something that you cannot do with arrays and forced the compiler to emit an error. Like arr++ emits an error ostensibly because arr is not just a single variable.
More so, that one could successfully use indexing in pointers doesn't make array replace them and their concept entirely.
So, I am sure I would not have had a problem recognizing the difference between the two.
I will admit though, that you might need to know what a register is to understand my answer
I don't have an issue with that. I thought I mentioned that previously.
Last edited by 2teez; 07-02-2015 at 04:03 AM. Reason: added a line
This is nothing that I have said in the course of answering in the thread. You are putting words in my mouth now. You specifically asked me what I thought. I told you what I thought. It was a rather easy explanation of why the topic question is reasonable and not simply a matter of someone struggling with a foundational concept. I will repeat myself. In order to ask this question, you need to have some unclear concept of array and pointer, and someone needs to separate the two concepts.
So, I am sure I would not have had a problem recognizing the difference between the two.
My point was not about you specifically. Rather I was explaining the thought process of someone with this confusion.
I don't have an issue with that. I thought I mentioned that previously.
But I must say, I am getting tired of discussing the auspices of the question with you. I view it as a distraction to the subject.
Last edited by whiteflags; 07-02-2015 at 04:33 AM.
It is okay then... It been nice meeting you...But I must say, I am getting tired of discussing the auspices of the question with you. I view it as a distraction to the subject.
I think Question 6.9 is quite relevant too.
Your example is invalid since you're comparing to an non-constant pointer, not a constant pointer as the question asks. It should be:
Obviously (at least with reasonable optimizations) accessing a's elements through a or p would generate identical code. However p is still a pointer and a an array as evidenced by their sizeof values, for instance.Code:int a[10]; int * const p = a;