Thread: Is a array name is a constant pointer?

  1. #1
    Registered User
    Join Date
    May 2014
    Posts
    10

    Is a array name is a constant pointer?

    Is a array name is a constant pointer?
    if not, so what is a array name on earth?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Bob Kang View Post
    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.
    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   : }
    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.

    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.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    9
    Hi,

    What do you think with this?

    Code:
    int main ( void ) {
    
        int array[10];
    
        printf("%p\n", array);  // check this?
        printf("%p\n", &array[0]);  // check this?
      
        return 0;
    }
    So is it or not?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    9
    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.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by 2teez View Post
    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.

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    9
    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.

    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.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

  9. #9
    Registered User
    Join Date
    Jun 2015
    Posts
    9
    .. 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.
    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.

    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

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by 2teez View Post
    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.

    More so, that one could successfully use
    indexing in pointers doesn't make array replace them and their concept entirely.


    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.

    I don't have an issue with that. I thought I mentioned that previously.

    My point was not about you specifically. Rather I was explaining the thought process of someone with this confusion.

    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.

  11. #11
    Registered User
    Join Date
    Jun 2015
    Posts
    9
    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.
    It is okay then... It been nice meeting you...

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by Bob Kang View Post
    Is a array name is a constant pointer?
    if not, so what is a array name on earth?
    Question 6.3
    Question 6.2

  13. #13
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    701
    Quote Originally Posted by Matticus View Post
    I think Question 6.9 is quite relevant too.

  14. #14
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by whiteflags View Post
    Well the biggest reason an array isn't a constant pointer is because the underlying assembler will be different....
    Your example is invalid since you're comparing to an non-constant pointer, not a constant pointer as the question asks. It should be:

    Code:
        int a[10];
        int * const p = a;
    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.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by algorism View Post
    Your example is invalid since you're comparing to an non-constant pointer, not a constant pointer as the question asks.
    The only difference that makes is the string is stored in the const segment. It does not otherwise affect the object code.

    Although you are right about what compile-time optimizations will do to it. I purposefully didn't use any, in order to make a teaching tool.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-18-2012, 05:13 PM
  2. modify pointer to a string/character constant.
    By xsouldeath in forum C Programming
    Replies: 12
    Last Post: 10-03-2007, 02:41 AM
  3. Constant pointer to constant value
    By tretton in forum C Programming
    Replies: 10
    Last Post: 12-23-2005, 01:45 PM
  4. Constant pointer function
    By Roaring_Tiger in forum C Programming
    Replies: 0
    Last Post: 02-16-2005, 05:14 PM
  5. constant pointer?
    By thebudbottle in forum C Programming
    Replies: 3
    Last Post: 11-07-2004, 10:25 PM