pointer to array types

Discussion in 'C++' started by Paul, Mar 19, 2011.

  1. Paul

    Paul Guest

    There seems to have been much confusion about pointer to array types in C++.
    Ahem many C++ experts seem to think the following is true:

    /*Very wrong and very bad stuff here*/
    int arr[6];
    int (*p)[6]= &arr;
    p[2] = 56;
    /*Very wrong and very bad stuff here*/

    The above seems to be the opinion of many of the *experts* in this
    newsgroups of how to use pointers to arrays in C++ but this is very bad and
    very wrong.
    A pointer to an array of 1 dimension is always of base type of the array,
    for example int*. The above code should be done like this:
    int arr[6];
    int* p= arr;
    p[2] = 56;


    A pointer of type int(*p)[6], is a pointer to a 2 dim array like so:
    int arr[3][6];
    int (*p)[6] = arr;
    This is NOT a pointer to an array of int[6].


    It has to be this way because when we index and array with the subscript the
    array is converted to a pointer+index and then dereferenced.
    With:
    int arr[6];
    arr[2] = 0;
    The value returned from the dereferenced pointer must be an int type, it is
    not an array type.

    So remember a pointer to an array is always n-1 dim less than the array it
    points to.

    Please refer to your ISO C++ standards for more clarification.
    TY and HTH.

    P.S. I can't believe so many *experts* don't understand these basics.
     
    Paul, Mar 19, 2011
    #1
    1. Advertising

  2. On Mar 18, 10:13 pm, "Paul" <> wrote:
    [snip]

    Please ignore our resident troll Paul, except for the one part where
    he is right:

    >Please refer to your ISO C++ standards for more clarification.


    He refuses to use the same language of discourse as the rest of us,
    and that frequently makes him not right, but not even wrong.

    http://rationalwiki.org/wiki/Not_even_wrong
     
    Joshua Maurice, Mar 19, 2011
    #2
    1. Advertising

  3. Paul

    SG Guest

    On 19 Mrz., 06:13, Paul wrote:
    > There seems to have been much confusion about
    > pointer to array types in C++.


    This is not the case (see below).

    > Ahem many C++ experts seem to think the following is true:
    >
    > /*Very wrong and very bad stuff here*/
    > int arr[6];
    > int (*p)[6]= &arr;
    > p[2] = 56;
    > /*Very wrong and very bad stuff here*/


    This code fragment won't even compile and I'm sure that other experts
    know exactly why this is. It seems, you got something wrong and/or
    spotted a typo in someone else's writing. Since you have a pointer to
    an _array_ and not a pointer to an _int_ ...

    int* : type of pointer to int
    int(*)[6] : type of pointer to array of int

    ....you need to dereference it first in order to store something into
    the array:

    (*p)[2] = 56;

    just like it is the case with every other pointer:

    int* q = ...;
    *q = 1729;

    p: pointer to array
    *p: the array itself (lvalue)
    q: pointer to int
    *q: the int itself (lvalue)

    Keep in mind that when everybody (except you) uses the words "pointer
    to array" they attatch a different meaning to these words (terminology
    issue). And the disagreements you are faced with are only about the
    meanings we attatch to words, not about "how C++ works". You
    apparently use the words "pointer to array" as a shortcut for "pointer
    that points to an element of an array". But this is just *imprecise*
    and people are trying to explain this to you.

    > The above seems to be the opinion of many of the *experts*


    This does not make any sense. How can a code fragment be an opinion?
    What opinion are you talking about? Very imprecise wording as we have
    come to expect from you.

    > A pointer to an array of 1 dimension is always of base type of the
    > array, for example int*.
    > The above code should be done like this:
    > int arr[6];
    > int* p= arr;
    > p[2] = 56;
    >
    > A pointer of type int(*p)[6], is a pointer to a 2 dim array like so:
    > int arr[3][6];
    > int (*p)[6] = arr;
    > This is NOT a pointer to an array of int[6].
    > It has to be this way because when we index and array with the subscript the
    > array is converted to a pointer+index and then dereferenced.
    > [...]
    > So remember a pointer to an array is always n-1 dim less than
    > the array it points to.


    The actual message you are trying to convey here is well-known. It's
    just your bad choice of words that everybody disagrees with.

    > P.S. I can't believe so many *experts* don't understand these basics.


    You don't need to believe that because it is not true. It's not that
    these experts don't understand how things work. It's that they
    disagree with your choice of words when it comes to describing how
    things work. This has always been the case. Always.

    SG
     
    SG, Mar 19, 2011
    #3
  4. Paul

    Nobody Guest

    On Sat, 19 Mar 2011 05:13:36 +0000, Paul wrote:

    > There seems to have been much confusion about pointer to array types in C++.
    > Ahem many C++ experts seem to think the following is true:
    >
    > /*Very wrong and very bad stuff here*/
    > int arr[6];
    > int (*p)[6]= &arr;
    > p[2] = 56;


    The last line should be:

    (*p)[2] = 56;
    or:
    p[0][2] = 56;

    > A pointer to an array of 1 dimension is always of base type of the array,
    > for example int*. The above code should be done like this:
    > int arr[6];
    > int* p= arr;
    > p[2] = 56;


    You don't seem to understand the difference between a pointer to an array
    and a pointer to an element of an array. Either is valid.

    > A pointer of type int(*p)[6], is a pointer to a 2 dim array


    No, it's a pointer to an array of 6 integers. The array to which it points
    may or may not be an element of a larger array.

    > like so:
    > int arr[3][6];
    > int (*p)[6] = arr;
    > This is NOT a pointer to an array of int[6].


    Yes it is. "arr" is an array of 3 elements, each of which is an array of 6
    integers. In the above code, p points to the first element of arr, i.e. it
    points to an array of 6 integers.

    > It has to be this way because when we index and array with the subscript the
    > array is converted to a pointer+index and then dereferenced.
    > With:
    > int arr[6];
    > arr[2] = 0;
    > The value returned from the dereferenced pointer must be an int type, it is
    > not an array type.


    In an lvalue context, arr is int[6]. In an expression context, it is
    converted to int*.

    > So remember a pointer to an array is always n-1 dim less than the array it
    > points to.


    Er, no. Again, you don't appear to understand the difference between a
    pointer to an array and a pointer to an array element.
     
    Nobody, Mar 19, 2011
    #4
  5. Paul

    Rolf Magnus Guest

    Paul wrote:

    > There seems to have been much confusion about pointer to array types in
    > C++. Ahem many C++ experts seem to think the following is true:
    >
    > /*Very wrong and very bad stuff here*/
    > int arr[6];
    > int (*p)[6]= &arr;
    > p[2] = 56;
    > /*Very wrong and very bad stuff here*/
    >
    > The above seems to be the opinion of many of the *experts* in this
    > newsgroups of how to use pointers to arrays in C++ but this is very bad
    > and very wrong.


    I hope not. The last line in your example is an error. It would have to be:

    (*p)[2] = 56;

    But pointer to arrays are rarely used. One could use a typedef to simplify
    it a bit:

    typedef int array[6];
    array arr;
    array* p = &arr;

    > A pointer to an array of 1 dimension is always of base type of the array,
    > for example int*.


    That's not a pointer to an array. It's a pointer to an int - a single int.
    It might happen to be the first (or second or tenth) element of an array,
    but that doesn't influence the pointer type. It's still a pointer to an int.
    Often, people use sloppy wording and talk of a "pointer to an array" when
    they actually mean a pointer to its first element.

    > The above code should be done like this:
    > int arr[6];
    > int* p= arr;
    > p[2] = 56;


    Yes, it should be done with a pointer to int.

    > A pointer of type int(*p)[6], is a pointer to a 2 dim array like so:


    No, it's not.

    > int arr[3][6];
    > int (*p)[6] = arr;
    > This is NOT a pointer to an array of int[6].


    Yes it is.

    > It has to be this way because when we index and array with the subscript
    > the array is converted to a pointer+index and then dereferenced.


    No, it's because you can use array notation on a pointer for convenience.
    The normal way to dereference a pointer is like that:

    int i;
    int* p = &i;
    *p = 5;

    As you can see, no array involved. But since such a pointer is often used to
    point to the beginning of an array (i.e. its first element), you would often
    have to write something like that:

    int arr[10];
    int* p = arr;
    *(p+5) = 50;

    And so as a convenient alternative,

    p[5] = 50;

    is equivalent.

    > So remember a pointer to an array is always n-1 dim less than the array it
    > points to.


    How about the following code:

    int* p = new int;
    *p = 3;
    delete p;

    Where do you see an array here? Do you suggest I have to use delete[]
    instead of delete, because I'm deleting an array, since p is a pointer to
    one?

    > Please refer to your ISO C++ standards for more clarification.


    I did. Maybe you should too. A good example can be found in §8.1 "Type
    names":

    ****************************************************************************
    int // int i
    int * // int *pi
    int *[3] // int *p[3]
    int (*)[3] // int (*p3i)[3]
    int *() // int *f()
    int (*)(double) // int (*pf)(double)

    name respectively the types “int,†“pointer to int,†“array of 3 pointers to
    int,†“pointer to array of 3 int,†“function of (no parameters) returning
    pointer to int,†and “pointer to a function of (double) returning int.’’ ]
    ****************************************************************************
     
    Rolf Magnus, Mar 19, 2011
    #5
  6. Paul

    Paul Guest

    "SG" <> wrote in message
    news:...
    > On 19 Mrz., 06:13, Paul wrote:
    >> There seems to have been much confusion about
    >> pointer to array types in C++.

    >
    > This is not the case (see below).
    >
    >> Ahem many C++ experts seem to think the following is true:
    >>
    >> /*Very wrong and very bad stuff here*/
    >> int arr[6];
    >> int (*p)[6]= &arr;
    >> p[2] = 56;
    >> /*Very wrong and very bad stuff here*/

    >
    > This code fragment won't even compile and I'm sure that other experts
    > know exactly why this is. It seems, you got something wrong and/or
    > spotted a typo in someone else's writing. Since you have a pointer to
    > an _array_ and not a pointer to an _int_ ...
    >
    > int* : type of pointer to int
    > int(*)[6] : type of pointer to array of int
    >

    As I said it is wrong and very bad code.

    > ...you need to dereference it first in order to store something into
    > the array:
    >
    > (*p)[2] = 56;
    >

    What you have here is a pointerto a pointerto an array, and this is why you
    need to dereference it twice to access the data, let me explain:

    int x[1]= {5};
    int* p_x = x; /*create a pointerto the array*/
    int** p_p_x = &p_x; /*a pointerto pointerto the array */
    std::cout<<(*p_p_x)[0]; /*Dereferenced twice: explicitly and implicitly*/

    You are confused by the following two terms:
    a) pointer to array
    b) pointer to array-type

    > just like it is the case with every other pointer:
    >
    > int* q = ...;
    > *q = 1729;
    >
    > p: pointer to array
    > *p: the array itself (lvalue)
    > q: pointer to int
    > *q: the int itself (lvalue)
    >

    I don't know what the above code is suppposed to be suggesting.

    > Keep in mind that when everybody (except you) uses the words "pointer
    > to array" they attatch a different meaning to these words (terminology
    > issue).

    Your terminology is incorrect , you are confused between:
    a) a pointerto an array
    b)a pointerto array-type

    A pointer, of type int(*)[5], stores exactly one memory address.
    A pointer, of type int* , stores exactly one memory address.
    They can both point to the same initial element of a 2dim array. The
    difference is int* is a pointerto a single dimensional array, int(*)[5] is a
    pointer to a 2dim array.

    With a pointerto an array, the pointer-type is always n-1 dimensions less
    than the array it points to.
    <snip>
     
    Paul, Mar 19, 2011
    #6
  7. Paul

    Paul Guest

    "Joshua Maurice" <> wrote in message
    news:...
    On Mar 18, 10:13 pm, "Paul" <> wrote:
    [snip]

    Please ignore our resident troll Paul, except for the one part where
    he is right:

    You're the troll because my whole post is right, and you cannot prove
    otherwise.
     
    Paul, Mar 19, 2011
    #7
  8. Paul

    RaZiel Guest

    On 19.03.2011 06:13, Paul wrote:
    > There seems to have been much confusion about pointer to array types in
    > C++. Ahem many C++ experts seem to think the following is true:
    >
    > /*Very wrong and very bad stuff here*/
    > int arr[6];
    > int (*p)[6]= &arr;
    > p[2] = 56;
    > /*Very wrong and very bad stuff here*/
    >
    > The above seems to be the opinion of many of the *experts* in this
    > newsgroups of how to use pointers to arrays in C++ but this is very bad
    > and very wrong.
    > A pointer to an array of 1 dimension is always of base type of the
    > array, for example int*. The above code should be done like this:
    > int arr[6];
    > int* p= arr;
    > p[2] = 56;
    >
    >
    > A pointer of type int(*p)[6], is a pointer to a 2 dim array like so:
    > int arr[3][6];
    > int (*p)[6] = arr;
    > This is NOT a pointer to an array of int[6].
    >
    >
    > It has to be this way because when we index and array with the subscript
    > the array is converted to a pointer+index and then dereferenced.
    > With:
    > int arr[6];
    > arr[2] = 0;
    > The value returned from the dereferenced pointer must be an int type, it
    > is not an array type.
    >
    > So remember a pointer to an array is always n-1 dim less than the array
    > it points to.
    >
    > Please refer to your ISO C++ standards for more clarification.
    > TY and HTH.
    >
    > P.S. I can't believe so many *experts* don't understand these basics.
    >
    >
    >
    >
    >


    Why don't you just give up? Why waste your saturday morning on feeble
    minds, as you see it? If you used your dedication and energy for
    something fruitful, I'm sure you could achieve something. But here,
    honestly, you achieve nothing.

    - RaZ
     
    RaZiel, Mar 19, 2011
    #8
  9. Paul

    Paul Guest

    "Nobody" <> wrote in message
    news:p...
    > On Sat, 19 Mar 2011 05:13:36 +0000, Paul wrote:
    >
    >> There seems to have been much confusion about pointer to array types in
    >> C++.
    >> Ahem many C++ experts seem to think the following is true:
    >>
    >> /*Very wrong and very bad stuff here*/
    >> int arr[6];
    >> int (*p)[6]= &arr;
    >> p[2] = 56;

    >
    > The last line should be:
    >
    > (*p)[2] = 56;
    > or:
    > p[0][2] = 56;
    >

    This is because a pointerto a 2dim array is used to access a 1dim array.
    This is not how it should be , this whole concept is wrong and very bad
    programming practise.


    >> A pointer to an array of 1 dimension is always of base type of the array,
    >> for example int*. The above code should be done like this:
    >> int arr[6];
    >> int* p= arr;
    >> p[2] = 56;

    >
    > You don't seem to understand the difference between a pointer to an array
    > and a pointer to an element of an array. Either is valid.
    >

    You are confusing the two terms:
    a) pointerto an array
    b)pointerto an array-type.

    It's not that I don't understand , it's that your terminology is inaccurate.


    >> A pointer of type int(*p)[6], is a pointer to a 2 dim array

    >
    > No, it's a pointer to an array of 6 integers. The array to which it points
    > may or may not be an element of a larger array.


    This is a pointerto array-type int[6]. It doesn't point to 6 memory
    locations, it's value has one memory address.
    When this type of pointer is dereferenced it returns a pointer to int[6].

    Clarification:

    int arr[1][5]= {0};
    int (*p)[5]= arr;
    std::cout<< "typeid of p:\t" <<typeid(p).name()<<std::endl;
    std::cout<< "typeid of *p:\t" <<typeid((*p)).name();

    This is a pointer to a 2dim array. It points to an *array-type* int[5], that
    is what it yields when dereferenced..


    From the C++ standards:
    "If E is an ndimensional array of rank i´ j´ . . . ´k, then E appearing in
    an expression is converted to a pointer to an (n - 1 )dimensional
    array with rank j´ . . . ´k. If the * operator, either explicitly or
    implicitly as a result of subscripting, is applied to this pointer, the
    result is the pointedto
    (n - 1 )dimensional array, which itself is immediately converted into a
    pointer."


    >
    >> like so:
    >> int arr[3][6];
    >> int (*p)[6] = arr;
    >> This is NOT a pointer to an array of int[6].

    >
    > Yes it is. "arr" is an array of 3 elements, each of which is an array of 6
    > integers. In the above code, p points to the first element of arr, i.e. it
    > points to an array of 6 integers.


    It's not its a pointer to array-type int[6], see above. It points to exactly
    one memory address.


    <snip>
     
    Paul, Mar 19, 2011
    #9
  10. Paul

    Paul Guest

    "Rolf Magnus" <> wrote in message
    news:im1ngc$1ah$01$-online.com...
    > Paul wrote:
    >
    >> There seems to have been much confusion about pointer to array types in
    >> C++. Ahem many C++ experts seem to think the following is true:
    >>
    >> /*Very wrong and very bad stuff here*/
    >> int arr[6];
    >> int (*p)[6]= &arr;
    >> p[2] = 56;
    >> /*Very wrong and very bad stuff here*/
    >>
    >> The above seems to be the opinion of many of the *experts* in this
    >> newsgroups of how to use pointers to arrays in C++ but this is very bad
    >> and very wrong.

    >
    > I hope not. The last line in your example is an error. It would have to
    > be:
    >
    > (*p)[2] = 56;
    >
    > But pointer to arrays are rarely used. One could use a typedef to simplify
    > it a bit:
    >
    > typedef int array[6];
    > array arr;
    > array* p = &arr;
    >
    >> A pointer to an array of 1 dimension is always of base type of the array,
    >> for example int*.

    >
    > That's not a pointer to an array. It's a pointer to an int - a single int.


    No it's a pointer to int-type, if the value of that pointer is the address
    of an array it's a pointer to an array.


    > It might happen to be the first (or second or tenth) element of an array,
    > but that doesn't influence the pointer type. It's still a pointer to an
    > int.


    You are clearly confused between:
    a) pointer to int
    b) pointer to int-type
    A pointer to an array is not a pointer to int but it may well be a pointer
    to int-type

    > Often, people use sloppy wording and talk of a "pointer to an array" when
    > they actually mean a pointer to its first element.
    >

    A pointer to an array is a pointer to an address within the bounds of the
    array. It is not neccessarrilly the first element.

    >> The above code should be done like this:
    >> int arr[6];
    >> int* p= arr;
    >> p[2] = 56;

    >
    > Yes, it should be done with a pointer to int.


    More accurate terminology is ... it should be done with pointer to int-type.

    >
    >> A pointer of type int(*p)[6], is a pointer to a 2 dim array like so:

    >
    > No, it's not.
    >
    >> int arr[3][6];
    >> int (*p)[6] = arr;
    >> This is NOT a pointer to an array of int[6].

    >
    > Yes it is.


    This is the major stumbling block where people get confused with
    pointerto-array and pointerto-array-types.
    I will explain again:

    int arr[1][5]= {0};
    int (*p)[5]= arr;
    std::cout<< "typeid of p:\t" <<typeid(p).name()<<std::endl;
    std::cout<< "typeid of *p:\t" <<typeid((*p)).name();

    This is a pointer to a 2dim array. It points to an *array-type* int[5], that
    is what it yields when dereferenced..

    From the C++ standards:
    "If E is an ndimensional array of rank i´ j´ . . . ´k, then E appearing in
    an expression is converted to a pointer to an (n - 1 )dimensional
    array with rank j´ . . . ´k. If the * operator, either explicitly or
    implicitly as a result of subscripting, is applied to this pointer, the
    result is the pointedto
    (n - 1 )dimensional array, which itself is immediately converted into a
    pointer."

    >
    >> It has to be this way because when we index and array with the subscript
    >> the array is converted to a pointer+index and then dereferenced.

    >
    > No, it's because you can use array notation on a pointer for convenience.
    > The normal way to dereference a pointer is like that:
    >
    > int i;
    > int* p = &i;
    > *p = 5;
    >
    > As you can see, no array involved. But since such a pointer is often used
    > to
    > point to the beginning of an array (i.e. its first element), you would
    > often
    > have to write something like that:
    >
    > int arr[10];
    > int* p = arr;
    > *(p+5) = 50;
    >
    > And so as a convenient alternative,
    >
    > p[5] = 50;
    >
    > is equivalent.
    >

    This shows that you are capable of understanding that the pointer to "arr"
    above is indeed a pointer to int-type.


    <snip>
     
    Paul, Mar 19, 2011
    #10
  11. Paul

    Paul Guest

    "RaZiel" <> wrote in message
    news:im2ctv$e7$...
    > On 19.03.2011 06:13, Paul wrote:
    >> There seems to have been much confusion about pointer to array types in
    >> C++. Ahem many C++ experts seem to think the following is true:
    >>
    >> /*Very wrong and very bad stuff here*/
    >> int arr[6];
    >> int (*p)[6]= &arr;
    >> p[2] = 56;
    >> /*Very wrong and very bad stuff here*/
    >>
    >> The above seems to be the opinion of many of the *experts* in this
    >> newsgroups of how to use pointers to arrays in C++ but this is very bad
    >> and very wrong.
    >> A pointer to an array of 1 dimension is always of base type of the
    >> array, for example int*. The above code should be done like this:
    >> int arr[6];
    >> int* p= arr;
    >> p[2] = 56;
    >>
    >>
    >> A pointer of type int(*p)[6], is a pointer to a 2 dim array like so:
    >> int arr[3][6];
    >> int (*p)[6] = arr;
    >> This is NOT a pointer to an array of int[6].
    >>
    >>
    >> It has to be this way because when we index and array with the subscript
    >> the array is converted to a pointer+index and then dereferenced.
    >> With:
    >> int arr[6];
    >> arr[2] = 0;
    >> The value returned from the dereferenced pointer must be an int type, it
    >> is not an array type.
    >>
    >> So remember a pointer to an array is always n-1 dim less than the array
    >> it points to.
    >>
    >> Please refer to your ISO C++ standards for more clarification.
    >> TY and HTH.
    >>
    >> P.S. I can't believe so many *experts* don't understand these basics.
    >>
    >>
    >>
    >>
    >>

    >
    > Why don't you just give up? Why waste your saturday morning on feeble
    > minds, as you see it? If you used your dedication and energy for something
    > fruitful, I'm sure you could achieve something. But here, honestly, you
    > achieve nothing.
    >

    I guess I just like to prove who is correct and who isn't.
    I'm sure some of them will eventually understand and they will remember in
    future.
    What better things is there to do while I wait for the football to start on
    a Saturday? :)
     
    Paul, Mar 19, 2011
    #11
  12. Paul

    RaZiel Guest

    On 19.03.2011 15:39, Paul wrote:
    >
    > "RaZiel" <> wrote in message
    > news:im2ctv$e7$...
    >> On 19.03.2011 06:13, Paul wrote:
    >>> There seems to have been much confusion about pointer to array types in
    >>> C++. Ahem many C++ experts seem to think the following is true:
    >>>
    >>> /*Very wrong and very bad stuff here*/
    >>> int arr[6];
    >>> int (*p)[6]= &arr;
    >>> p[2] = 56;
    >>> /*Very wrong and very bad stuff here*/
    >>>
    >>> The above seems to be the opinion of many of the *experts* in this
    >>> newsgroups of how to use pointers to arrays in C++ but this is very bad
    >>> and very wrong.
    >>> A pointer to an array of 1 dimension is always of base type of the
    >>> array, for example int*. The above code should be done like this:
    >>> int arr[6];
    >>> int* p= arr;
    >>> p[2] = 56;
    >>>
    >>>
    >>> A pointer of type int(*p)[6], is a pointer to a 2 dim array like so:
    >>> int arr[3][6];
    >>> int (*p)[6] = arr;
    >>> This is NOT a pointer to an array of int[6].
    >>>
    >>>
    >>> It has to be this way because when we index and array with the subscript
    >>> the array is converted to a pointer+index and then dereferenced.
    >>> With:
    >>> int arr[6];
    >>> arr[2] = 0;
    >>> The value returned from the dereferenced pointer must be an int type, it
    >>> is not an array type.
    >>>
    >>> So remember a pointer to an array is always n-1 dim less than the array
    >>> it points to.
    >>>
    >>> Please refer to your ISO C++ standards for more clarification.
    >>> TY and HTH.
    >>>
    >>> P.S. I can't believe so many *experts* don't understand these basics.
    >>>
    >>>
    >>>
    >>>
    >>>

    >>
    >> Why don't you just give up? Why waste your saturday morning on feeble
    >> minds, as you see it? If you used your dedication and energy for
    >> something fruitful, I'm sure you could achieve something. But here,
    >> honestly, you achieve nothing.
    >>

    > I guess I just like to prove who is correct and who isn't.

    Why?
    > I'm sure some of them will eventually understand and they will remember
    > in future.

    Why? Does it matter to you if they don't understand? Will their
    understanding affect you somehow? I'm sure they will remember you, but
    for other reasons than you think.
    > What better things is there to do while I wait for the football to start
    > on a Saturday? :)
    >
    >

    Sleep, walk, go out eating, go to the market/mall, have fun with your
    family/friends, go to the local pub to watch the football match, etc..

    Nevertheless, enjoy the match.

    - RaZ
     
    RaZiel, Mar 19, 2011
    #12
  13. Paul

    Paul Guest

    "RaZiel" <> wrote in message
    news:im2hrr$ce5$...
    > On 19.03.2011 15:39, Paul wrote:
    >>
    >> "RaZiel" <> wrote in message
    >> news:im2ctv$e7$...
    >>> On 19.03.2011 06:13, Paul wrote:
    >>>> There seems to have been much confusion about pointer to array types in
    >>>> C++. Ahem many C++ experts seem to think the following is true:
    >>>>
    >>>> /*Very wrong and very bad stuff here*/
    >>>> int arr[6];
    >>>> int (*p)[6]= &arr;
    >>>> p[2] = 56;
    >>>> /*Very wrong and very bad stuff here*/
    >>>>
    >>>> The above seems to be the opinion of many of the *experts* in this
    >>>> newsgroups of how to use pointers to arrays in C++ but this is very bad
    >>>> and very wrong.
    >>>> A pointer to an array of 1 dimension is always of base type of the
    >>>> array, for example int*. The above code should be done like this:
    >>>> int arr[6];
    >>>> int* p= arr;
    >>>> p[2] = 56;
    >>>>
    >>>>
    >>>> A pointer of type int(*p)[6], is a pointer to a 2 dim array like so:
    >>>> int arr[3][6];
    >>>> int (*p)[6] = arr;
    >>>> This is NOT a pointer to an array of int[6].
    >>>>
    >>>>
    >>>> It has to be this way because when we index and array with the
    >>>> subscript
    >>>> the array is converted to a pointer+index and then dereferenced.
    >>>> With:
    >>>> int arr[6];
    >>>> arr[2] = 0;
    >>>> The value returned from the dereferenced pointer must be an int type,
    >>>> it
    >>>> is not an array type.
    >>>>
    >>>> So remember a pointer to an array is always n-1 dim less than the array
    >>>> it points to.
    >>>>
    >>>> Please refer to your ISO C++ standards for more clarification.
    >>>> TY and HTH.
    >>>>
    >>>> P.S. I can't believe so many *experts* don't understand these basics.
    >>>>
    >>>>
    >>>>
    >>>>
    >>>>
    >>>
    >>> Why don't you just give up? Why waste your saturday morning on feeble
    >>> minds, as you see it? If you used your dedication and energy for
    >>> something fruitful, I'm sure you could achieve something. But here,
    >>> honestly, you achieve nothing.
    >>>

    >> I guess I just like to prove who is correct and who isn't.

    > Why?

    A matter of technical correctness.

    >> I'm sure some of them will eventually understand and they will remember
    >> in future.

    > Why? Does it matter to you if they don't understand? Will their
    > understanding affect you somehow? I'm sure they will remember you, but for
    > other reasons than you think.


    They won't remeber me because they don't know me, they may remember some
    posts in a newsgroup.
    If they chose to think these posts originate from some kind of subhuman
    entity who will always be incorrect,, that's their problem.
    But this is just some misconception constructed in their minds.

    >> What better things is there to do while I wait for the football to start
    >> on a Saturday? :)
    >>
    >>

    > Sleep, walk, go out eating, go to the market/mall, have fun with your
    > family/friends, go to the local pub to watch the football match, etc..
    >
    > Nevertheless, enjoy the match.
    >

    TY , you have a nice day too whatever you chose to do . :)
     
    Paul, Mar 19, 2011
    #13
  14. Paul

    Rolf Magnus Guest

    Paul wrote:

    >> typedef int array[6];
    >> array arr;
    >> array* p = &arr;
    >>
    >>> A pointer to an array of 1 dimension is always of base type of the
    >>> array, for example int*.

    >>
    >> That's not a pointer to an array. It's a pointer to an int - a single
    >> int.

    >
    > No it's a pointer to int-type, if the value of that pointer is the address
    > of an array it's a pointer to an array.


    But you didn't use the address-of operator on an array to get it.

    >> It might happen to be the first (or second or tenth) element of an array,
    >> but that doesn't influence the pointer type. It's still a pointer to an
    >> int.

    >
    > You are clearly confused between:


    I'm not at all confused.

    > a) pointer to int
    > b) pointer to int-type


    Which is the same thing.

    > A pointer to an array is not a pointer to int but it may well be a pointer
    > to int-type


    So it does not point to an int, but to something that is of type int?
    Strange wording.

    > This is the major stumbling block where people get confused with
    > pointerto-array and pointerto-array-types.
    > I will explain again:
    >
    > int arr[1][5]= {0};


    That's an array of arrays, i.e. a multi-dimensional array.

    > int (*p)[5]= arr;


    You could also write that as:

    int (*p)[5] = &arr[0];

    i.e. the address of the element of arr that is at index 0.

    > std::cout<< "typeid of p:\t" <<typeid(p).name()<<std::endl;
    > std::cout<< "typeid of *p:\t" <<typeid((*p)).name();
    >
    > This is a pointer to a 2dim array.


    You didn't use an address operator on a 2-dimensional array.

    > From the C++ standards:
    > "If E is an ndimensional array of rank i´ j´ . . . ´k, then E appearing in
    > an expression is converted to a pointer to an (n - 1 )dimensional
    > array with rank j´ . . . ´k. If the * operator, either explicitly or
    > implicitly as a result of subscripting, is applied to this pointer, the
    > result is the pointedto (n - 1 )dimensional array, which itself is
    > immediately converted into a pointer."


    Right. Just as I said. The example that's following that paragraph clears
    that up. I've marked the most important parts:

    ****************************************************************************
    [Example: consider

    int x[3][5];

    Here x is a 3×5 array of integers. When x appears in an expression, it is
    converted to a pointer to (the first of three) five-membered arrays of
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    integers.
    In the expression x, which is equivalent to *(x+i), x is
    first converted to a pointer as described; then x+i is converted to the type
    of x, which involves multiplying i by the length of the object to which the
    pointer points, namely five integer objects. The results are added
    and indirection applied to yield an array (of five integers), which in turn
    is converted to a pointer to the first of the integers. If there is another
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    subscript the same argument applies again; this time the result is an inte-
    ger. ]
    ****************************************************************************

    Note the wording. Both times, you get a pointer to *the first element* of
    the array. Not a "pointer to the array" as you claim.
     
    Rolf Magnus, Mar 19, 2011
    #14
  15. Paul

    Paul Guest

    "Rolf Magnus" <> wrote in message
    news:im2qgh$vgc$02$-online.com...
    > Paul wrote:
    >
    >>> typedef int array[6];
    >>> array arr;
    >>> array* p = &arr;
    >>>
    >>>> A pointer to an array of 1 dimension is always of base type of the
    >>>> array, for example int*.
    >>>
    >>> That's not a pointer to an array. It's a pointer to an int - a single
    >>> int.

    >>
    >> No it's a pointer to int-type, if the value of that pointer is the
    >> address
    >> of an array it's a pointer to an array.

    >
    > But you didn't use the address-of operator on an array to get it.


    You don't need to, it's explicitly converted to a pointer:
    int arr[5];
    int* p = arr;
    std::cout<<"&arr:\t"<< &arr << "\np:\t"<< p;


    >
    >>> It might happen to be the first (or second or tenth) element of an
    >>> array,
    >>> but that doesn't influence the pointer type. It's still a pointer to an
    >>> int.

    >>
    >> You are clearly confused between:

    >
    > I'm not at all confused.
    >
    >> a) pointer to int
    >> b) pointer to int-type

    >
    > Which is the same thing.
    >

    There you go , there is the source of your confusion.
    It's not the same thing, if you have a pointer pointing to memory location
    0F0000, the entity at this location is what the pointer points to.

    In the following, the pointer "p" points to an array:
    int arr[5];
    int* p = arr; /*Pointer to an array*/

    And next it points to an int:
    int x;
    p=&x; /*Pointer to an int*/

    The same pointer type can point to different entities. The allocation
    defines what entity the pointer points to, not the pointer-type.

    Consider the following:

    class A{
    public:
    virtual ~A(){std::cout<<"A destructor\n";}
    };
    class B:public A{~B(){std::cout<<"B destructor\n";}};
    class C:public A{~C(){std::cout<<"C destructor\n";}};

    int main()
    {
    A* p_a = new B;
    delete p_a;
    p_a = new C;
    delete p_a;
    std::cout<< "typeid p_a:\t"<< typeid(p_a).name();
    }

    In the above code p_a
    a) is a pointer to A type
    b) points to different types of objects

    A pointer to object != pointer to object type.




    >> A pointer to an array is not a pointer to int but it may well be a
    >> pointer
    >> to int-type

    >
    > So it does not point to an int, but to something that is of type int?
    > Strange wording.
    >

    No it's quite simple, it points to an array of ints. People are making it
    confusing with erroneous statements like.. "it's not an array but just one
    single integer".

    >> This is the major stumbling block where people get confused with
    >> pointerto-array and pointerto-array-types.
    >> I will explain again:
    >>
    >> int arr[1][5]= {0};

    >
    > That's an array of arrays, i.e. a multi-dimensional array.

    Yes.
    >
    >> int (*p)[5]= arr;

    >
    > You could also write that as:
    >
    > int (*p)[5] = &arr[0];
    >

    Yes you could.

    > i.e. the address of the element of arr that is at index 0.
    >

    Elements need two levels of indirection i.e:
    &arr[0][0].

    >> std::cout<< "typeid of p:\t" <<typeid(p).name()<<std::endl;
    >> std::cout<< "typeid of *p:\t" <<typeid((*p)).name();
    >>
    >> This is a pointer to a 2dim array.

    >
    > You didn't use an address operator on a 2-dimensional array.
    >

    correct.

    >> From the C++ standards:
    >> "If E is an ndimensional array of rank i´ j´ . . . ´k, then E appearing
    >> in
    >> an expression is converted to a pointer to an (n - 1 )dimensional
    >> array with rank j´ . . . ´k. If the * operator, either explicitly or
    >> implicitly as a result of subscripting, is applied to this pointer, the
    >> result is the pointedto (n - 1 )dimensional array, which itself is
    >> immediately converted into a pointer."

    >
    > Right. Just as I said. The example that's following that paragraph clears
    > that up. I've marked the most important parts:
    >
    > ****************************************************************************
    > [Example: consider
    >
    > int x[3][5];
    >
    > Here x is a 3×5 array of integers. When x appears in an expression, it is
    > converted to a pointer to (the first of three) five-membered arrays of
    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    > integers.
    > In the expression x, which is equivalent to *(x+i), x is
    > first converted to a pointer as described; then x+i is converted to the
    > type
    > of x, which involves multiplying i by the length of the object to which
    > the
    > pointer points, namely five integer objects. The results are added
    > and indirection applied to yield an array (of five integers), which in
    > turn
    > is converted to a pointer to the first of the integers. If there is
    > another
    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    > subscript the same argument applies again; this time the result is an
    > inte-
    > ger. ]
    > ****************************************************************************
    >
    > Note the wording. Both times, you get a pointer to *the first element* of
    > the array. Not a "pointer to the array" as you claim.
    >

    Where does it say "a pointer to the first element of an array is not a
    pointer to the array" ? You must be reading something other than you've
    posted.

    Do you really think that if a pointer points to the initial element of an
    array it no longer points to the array? :-S
    The only we can point to the whole array simultaneously is to use an array
    of pointers.

    The quote I posted actually confirms what I said re:

    "the result is the pointedto (n - 1 )dimensional array"
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    The term "the pointedto array" clarifies all.
     
    Paul, Mar 19, 2011
    #15
  16. Paul

    Fred Zwarts Guest

    "Paul" <> wrote in message
    news:ff8hp.196005$2...
    >
    > "Rolf Magnus" <> wrote in message
    > news:im2qgh$vgc$02$-online.com...
    >> Paul wrote:
    >>>> It might happen to be the first (or second or tenth) element of an
    >>>> array,
    >>>> but that doesn't influence the pointer type. It's still a pointer to an
    >>>> int.
    >>>
    >>> You are clearly confused between:

    >>
    >> I'm not at all confused.
    >>
    >>> a) pointer to int
    >>> b) pointer to int-type

    >>
    >> Which is the same thing.
    >>

    > There you go , there is the source of your confusion.
    > It's not the same thing, if you have a pointer pointing to memory location
    > 0F0000, the entity at this location is what the pointer points to.


    Sorry, in C++ pointers don't point to types. Pointers point to objects.
    So, a pointer to int-type is another language.

    The fact that the pointer contains the address of an object, does not mean
    that it is a pointer to that object.
    Consider

    union AllTypes {
    int Integer;
    int IntArray[5];
    float Floating;
    float FloatArray[18];

    etc. etc.

    }

    AllTypes x;

    int * IntPointer = &x.Integer;

    Now IntPointer is a pointer to an int object.
    IntPointer is not a pointer to a float object.
    IntPointer is not a pointer to an float array.
    etc. etc.
    IntPointer is not a pointer to an int array.

    Only, by redefining the meaning of "pointer to",
    you can deny this.
     
    Fred Zwarts, Mar 21, 2011
    #16
  17. Paul

    Paul Guest


    >"Fred Zwarts" <> wrote in message
    >news:im72jf$4l3$...


    <snip>
    >>>>> It might happen to be the first (or second or tenth) element of an
    >>>>> array,
    >>>>> but that doesn't influence the pointer type. It's still a pointer to
    >>>>> an
    >>>>> int.
    >>>>
    >>>> You are clearly confused between:
    >>>
    >>> I'm not at all confused.
    >>>
    >>>> a) pointer to int
    >>>> b) pointer to int-type
    >>>
    >>> Which is the same thing.
    >>>

    >> There you go , there is the source of your confusion.
    >> It's not the same thing, if you have a pointer pointing to memory
    >> location 0F0000, the entity at this location is what the pointer points
    >> to.

    >
    >>Sorry, in C++ pointers don't point to types. Pointers point to objects.
    >>So, a pointer to int-type is another language.


    No, pointers point to an entity, that entity is not necessarrilly an object.
    A pointer-to int-type means exactly the same thing as pointer-to entity of
    int-type.

    >>The fact that the pointer contains the address of an object, does not mean
    >>that it is a pointer to that object.

    You mean thats what you've been thinking about?
    >>Consider

    >
    >union AllTypes {
    > int Integer;
    > int IntArray[5];
    > float Floating;
    > float FloatArray[18];
    >
    > etc. etc.
    >
    >}
    >
    >AllTypes x;
    >
    >int * IntPointer = &x.Integer;
    >
    >Now IntPointer is a pointer to an int object.
    >IntPointer is not a pointer to a float object.
    >IntPointer is not a pointer to an float array.
    >etc. etc.
    >IntPointer is not a pointer to an int array.


    At the end of the day who cares what it's NOT a pointer to, what are you
    trying to demonstrate?
     
    Paul, Mar 21, 2011
    #17
  18. Paul

    Fred Zwarts Guest

    "Paul" <> wrote in message
    news:hTGhp.72868$%2...
    >
    >>"Fred Zwarts" <> wrote in message
    >>news:im72jf$4l3$...

    >
    > <snip>
    >>>>>> It might happen to be the first (or second or tenth) element of an
    >>>>>> array,
    >>>>>> but that doesn't influence the pointer type. It's still a pointer to
    >>>>>> an
    >>>>>> int.
    >>>>>
    >>>>> You are clearly confused between:
    >>>>
    >>>> I'm not at all confused.
    >>>>
    >>>>> a) pointer to int
    >>>>> b) pointer to int-type
    >>>>
    >>>> Which is the same thing.
    >>>>
    >>> There you go , there is the source of your confusion.
    >>> It's not the same thing, if you have a pointer pointing to memory
    >>> location 0F0000, the entity at this location is what the pointer points
    >>> to.

    >>
    >>>Sorry, in C++ pointers don't point to types. Pointers point to objects.
    >>>So, a pointer to int-type is another language.

    >
    > No, pointers point to an entity, that entity is not necessarrilly an
    > object.
    > A pointer-to int-type means exactly the same thing as pointer-to entity of
    > int-type.
    >
    >>>The fact that the pointer contains the address of an object, does not
    >>>mean that it is a pointer to that object.

    > You mean thats what you've been thinking about?
    >>>Consider

    >>
    >>union AllTypes {
    >> int Integer;
    >> int IntArray[5];
    >> float Floating;
    >> float FloatArray[18];
    >>
    >> etc. etc.
    >>
    >>}
    >>
    >>AllTypes x;
    >>
    >>int * IntPointer = &x.Integer;
    >>
    >>Now IntPointer is a pointer to an int object.
    >>IntPointer is not a pointer to a float object.
    >>IntPointer is not a pointer to an float array.
    >>etc. etc.
    >>IntPointer is not a pointer to an int array.

    >
    > At the end of the day who cares what it's NOT a pointer to, what are you
    > trying to demonstrate?


    Who cares? I thought you would care.
    If not, finally, you don't care that IntPointer[2] can be used to access an
    array element,
    but that at the same time IntArray is not a pointer to an array.
     
    Fred Zwarts, Mar 21, 2011
    #18
  19. Paul

    Paul Guest

    "Fred Zwarts" <> wrote in message
    news:im7ee1$1il$...

    <snip>
    >>>>>> It might happen to be the first (or second or tenth) element of an
    >>>>>> array,
    >>>>>> but that doesn't influence the pointer type. It's still a pointer to
    >>>>>> an
    >>>>>> int.
    >>>>>
    >>>>> You are clearly confused between:
    >>>>
    >>>> I'm not at all confused.
    >>>>
    >>>>> a) pointer to int
    >>>>> b) pointer to int-type
    >>>>
    >>>> Which is the same thing.
    >>>>
    >>> There you go , there is the source of your confusion.
    >>> It's not the same thing, if you have a pointer pointing to memory
    >>> location 0F0000, the entity at this location is what the pointer points
    >>> to.

    >>
    >>>Sorry, in C++ pointers don't point to types. Pointers point to objects.
    >>>So, a pointer to int-type is another language.

    >
    > No, pointers point to an entity, that entity is not necessarrilly an
    > object.
    > A pointer-to int-type means exactly the same thing as pointer-to entity of
    > int-type.
    >
    >>>The fact that the pointer contains the address of an object, does not
    >>>mean that it is a pointer to that object.

    > You mean thats what you've been thinking about?
    >>>Consider

    >>
    >>union AllTypes {
    >> int Integer;
    >> int IntArray[5];
    >> float Floating;
    >> float FloatArray[18];
    >>
    >> etc. etc.
    >>
    >>}
    >>
    >>AllTypes x;
    >>
    >>int * IntPointer = &x.Integer;
    >>
    >>Now IntPointer is a pointer to an int object.
    >>IntPointer is not a pointer to a float object.
    >>IntPointer is not a pointer to an float array.
    >>etc. etc.
    >>IntPointer is not a pointer to an int array.

    >
    > At the end of the day who cares what it's NOT a pointer to, what are you
    > trying to demonstrate?


    --Who cares? I thought you would care.
    --If not, finally, you don't care that IntPointer[2] can be used to access
    an
    --array element,
    --but that at the same time IntArray is not a pointer to an array.


    I don't know what you are trying to state , sorry.
     
    Paul, Mar 21, 2011
    #19
  20. Paul

    Fred Zwarts Guest

    "Paul" <> wrote in message
    news:BgHhp.188127$2...
    >
    > "Fred Zwarts" <> wrote in message
    > news:im7ee1$1il$...
    >
    > <snip>
    >>>>>>> It might happen to be the first (or second or tenth) element of an
    >>>>>>> array,
    >>>>>>> but that doesn't influence the pointer type. It's still a pointer to
    >>>>>>> an
    >>>>>>> int.
    >>>>>>
    >>>>>> You are clearly confused between:
    >>>>>
    >>>>> I'm not at all confused.
    >>>>>
    >>>>>> a) pointer to int
    >>>>>> b) pointer to int-type
    >>>>>
    >>>>> Which is the same thing.
    >>>>>
    >>>> There you go , there is the source of your confusion.
    >>>> It's not the same thing, if you have a pointer pointing to memory
    >>>> location 0F0000, the entity at this location is what the pointer points
    >>>> to.
    >>>
    >>>>Sorry, in C++ pointers don't point to types. Pointers point to objects.
    >>>>So, a pointer to int-type is another language.

    >>
    >> No, pointers point to an entity, that entity is not necessarrilly an
    >> object.
    >> A pointer-to int-type means exactly the same thing as pointer-to entity
    >> of int-type.
    >>
    >>>>The fact that the pointer contains the address of an object, does not
    >>>>mean that it is a pointer to that object.

    >> You mean thats what you've been thinking about?
    >>>>Consider
    >>>
    >>>union AllTypes {
    >>> int Integer;
    >>> int IntArray[5];
    >>> float Floating;
    >>> float FloatArray[18];
    >>>
    >>> etc. etc.
    >>>
    >>>};
    >>>
    >>>AllTypes x;
    >>>
    >>>int * IntPointer = &x.Integer;
    >>>
    >>>Now IntPointer is a pointer to an int object.
    >>>IntPointer is not a pointer to a float object.
    >>>IntPointer is not a pointer to an float array.
    >>>etc. etc.
    >>>IntPointer is not a pointer to an int array.

    >>
    >> At the end of the day who cares what it's NOT a pointer to, what are you
    >> trying to demonstrate?

    >
    > --Who cares? I thought you would care.
    > --If not, finally, you don't care that IntPointer[2] can be used to access
    > an
    > --array element,
    > --but that at the same time IntArray is not a pointer to an array.
    >
    >
    > I don't know what you are trying to state , sorry.


    OK, let me see if I can say it even more clearly.

    IntPointer is a pointer to an object of type int.
    IntPointer now points to the int x.Integer.
    Because of the union, IntPointer[2] accesses an element of x.IntArray.
    I said that IntPointer is not a pointer to the array x.IntArray.
    You said: Who cares?
    But earlier you said that if a pointer points to a memory location
    that contains an array, it must be a pointer to an array.
    So, I got the impression that you would care about this.

    Because of the union
    IntPointer also points to a memory location of a float
    and to a memory location of a float array, etc. etc.
    According to your reasoning,
    IntPointer would be also a pointer to a float,
    and to a float array, etc. etc.

    After the assignment
    x.Floating=3.14;
    the memory location IntPointer points to, contains a float object.
    Still, IntPointer is not a pointer to a float
    and cannot be used like that without a cast.
    So, the fact that the value of a pointer is an address
    of a memory location that happens to contain an object of some type,
    does not mean that the pointer is a pointer to such an object.
    Saying that an int* is a pointer to a float is not the intention of the C++
    language,
    even if the address contained in it happens to be a location of a float.

    So, may I conclude that an int* pointer that happens to contain the address
    of an int array is not a pointer to an array according to the terminology of
    C++?
    Or do you still say: Who cares?
     
    Fred Zwarts, Mar 21, 2011
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sangeetha

    Array of pointer Vs Pointer to Array

    sangeetha, Oct 8, 2004, in forum: C Programming
    Replies:
    9
    Views:
    364
    Tim Rentsch
    Oct 9, 2004
  2. Replies:
    11
    Views:
    1,427
    James Kanze
    Jun 7, 2007
  3. erfan

    Array of pointer and pointer of array

    erfan, Jan 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    691
    Martin Ambuhl
    Jan 28, 2008
  4. RSL
    Replies:
    14
    Views:
    3,363
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    496
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page