A pointer to the first element of an array is a pointer to the array itself?

This is a discussion on A pointer to the first element of an array is a pointer to the array itself? within the C++ Programming forums, part of the General Programming Boards category; Just thinking, but could I get away with the claim that: Given type array[10]; array is a type *const to ...

  1. #16
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Just thinking, but could I get away with the claim that:

    Given type array[10];

    array is a type *const to the first element of the array? I would think this would address any potential l-value problems.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  2. #17
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    I'm afraid you couldn't get away with that claim either, eg,
    Code:
    template<int N>
    void foo(int (&arr) [N] )
    {
    }
    
    int main()
    {
        int bar[5]; 
        foo( bar );   // ok
    
        int* const blah = bar;
    //    foo( blah ) ;   //error
    }
    The type, int[], is different to int* const - While int[] may implicitly cast to int* const without problems, the opposite does not hold true



    On the other hand, a claim you might be able to get away with, could be that given a declaration, T array[10]; , that the type of 'array' is equivalent to T(&)[10] (Perhaps someone else would like to support or dismiss that claim)
    Last edited by Bench82; 07-19-2007 at 10:32 AM.

  3. #18
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Quote Originally Posted by Bench82 View Post
    The type, int[], is different to int* const - While int[] may implicitly cast to int* const without problems, the opposite does not hold true

    I never claimed the opposite. If a then b does not imply if b then a.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  4. #19
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by AverageSoftware View Post
    I never claimed the opposite. If a then b does not imply if b then a.
    That wasn't quite what I was getting at though. The main point, is that type information is the overriding factor. An array may implicitly cast to a pointer because it loses type information in the process, regarding the number of elements in the array. (information that a pointer isn't equipped to hold)

    As a result, the pointer contains only the information regarding the first element. This is why an array type is not a pointer type.

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You're just using the wrong terminology.

    array is an l-value denoting the array.

    &array[0] is an r-value denoting the address of the first element of the array.

    Whenever array is converted to an r-value, the resulting r-value is the same as &array[0].

    But value alone is an ambiguous term.

    array == &array[0] is only true because the first term has been implicitly converted to an r-value.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 08:33 AM
  2. pointer to first element in array
    By someprogr in forum C Programming
    Replies: 4
    Last Post: 01-10-2009, 02:52 AM
  3. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  4. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  5. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 09:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21