POINTER EQUALS AN ARRAY

Discussion in 'C++' started by chessc4c6, Nov 3, 2005.

  1. chessc4c6

    chessc4c6 Guest

    The program below creates a char pointer call charPtr...... i then
    declare an char array string[10] "Good Luck"

    When i assign charPtr = string, I expect an error. However, It
    actually runs and outputs:

    G
    Good Luck


    Can someone explain to me how charPtr = string actually works, but if I
    create
    char string = 'x';
    wouldn't work.



    #include<math.h>
    #include<iostream>

    using namespace std;
    void main()
    {
    int value;
    value = 500;
    char* charPtr;
    char string[10] = "Good Luck";
    charPtr = string;
    cout<< *charPtr<<endl;
    cout<<charPtr<<endl;

    }
    chessc4c6, Nov 3, 2005
    #1
    1. Advertising

  2. chessc4c6

    Mike Wahler Guest

    "chessc4c6" <> wrote in message
    news:...
    > The program below creates a char pointer call charPtr...... i then
    > declare an char array string[10] "Good Luck"
    >
    > When i assign charPtr = string, I expect an error.


    Why?

    > However, It
    > actually runs and outputs:
    >
    > G
    > Good Luck
    >
    >
    > Can someone explain to me how charPtr = string actually works, but if I
    > create
    > char string = 'x';
    > wouldn't work.


    In almost every context,, the name of an array is
    automatically converted to a pointer to its first
    element.

    char string = 'x'; does not denote an array.

    >
    >
    >
    > #include<math.h>
    > #include<iostream>
    >
    > using namespace std;
    > void main()


    int main()

    > {
    > int value;
    > value = 500;


    IMO better is:

    int value = 500;

    > char* charPtr;
    > char string[10] = "Good Luck";
    > charPtr = string;


    assigns to 'charPtr' the address of 'string[0]'

    > cout<< *charPtr<<endl;


    prints G (dereferences what 'charPtr' points to, a type 'char' object)

    > cout<<charPtr<<endl;


    prints Good Luck because the << operator taking type 'char*'
    argument is defined to print the 'C-style' string it points to.
    Also note that because of the automatic conversion, the same
    output results from:

    cout << string << endl;

    >
    > }


    Do not be misled into believing that an array and a pointer
    are the same thing. They are most certainly not.

    What you are seeing is an automatic conversion.

    Anyway, in C++ the preferred way of dealing with strings
    is with the 'std::string' type, not arrays of characters.
    Also, pointers are much less needed in C++ than in C.

    -Mike
    Mike Wahler, Nov 4, 2005
    #2
    1. Advertising

  3. chessc4c6

    Default User Guest

    chessc4c6 wrote:

    > The program below creates a char pointer call charPtr...... i then
    > declare an char array string[10] "Good Luck"
    >
    > When i assign charPtr = string, I expect an error. However, It
    > actually runs and outputs:


    > #include<math.h>


    You don't use this header.

    > #include<iostream>
    >
    > using namespace std;
    > void main()


    main() returns it.

    > {
    > int value;
    > value = 500;
    > char* charPtr;
    > char string[10] = "Good Luck";
    > charPtr = string;
    > cout<< *charPtr<<endl;
    > cout<<charPtr<<endl;
    >
    > }


    In most cases, the name of an array is converted to a pointer to the
    first element. Assignment is one such case, the name "string" (don't
    use that as it is the same as the standard string class) is converted
    to a pointer to the first element, making it a char pointer. Naturally,
    it's ok to then assign it to a char pointer.

    What book are you using that doesn't explain this?



    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
    Default User, Nov 4, 2005
    #3
  4. chessc4c6 wrote:

    > The program below creates a char pointer call charPtr...... i then
    > declare an char array string[10] "Good Luck"
    >
    > When i assign charPtr = string, I expect an error.


    Why? Consider the following:

    int a = 0;
    double b;

    b = a;

    'b' and 'a' have different types. But do you expect an error when you assign 'a'
    to 'b'? I hope you don't. You probably understand that when you assign an 'int'
    value to a 'double' object the former gets implicitly converted to the type of
    the latter.

    In general case, when you assign a value of type 'T' to an object of different
    type 'U', it is either 1) an error or 2) a perfectly correct piece of code that
    uses an implicit conversion. The implicit conversion is automatically introduced
    by the compiler. The specification of C++ language contains a list of all such
    conversions, which formally referred to as 'standard' conversions.

    One of the standard conversion is so called "array-to-pointer" conversion.
    Applied to our example, it says that if you have a value of array type (say
    'T[N]') as right-hand side of the assignment and an object of corresponding
    pointer type (would be 'T*') as left-hand side of the assignment, an implicit
    array-to-pointer conversion is performed and the pointer received the address of
    the very first element of the array. That's exactly what happens in your case.

    > Can someone explain to me how charPtr = string actually works, but if I
    > create
    > char string = 'x';
    > wouldn't work.


    Because the original array was... well, an array. And it was implicitly
    converted to pointer type by the aforementioned standard array-to-pointer
    conversion.

    Now you have an object of type 'char' as right-hand side of the assignment.
    There no implicit (or even explicit) conversion in C++ that can convert a value
    of type 'char' to pointer type. Hence the error.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 4, 2005
    #4
  5. Default User <> wrote:

    > > void main()


    > main() returns it.


    What is the program's termination status? I'll take "Typos" for 1000,
    Alex. :)

    (For OP: main() returns int.)

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Nov 4, 2005
    #5
  6. On 3 Nov 2005 15:45:17 -0800, "chessc4c6" <> wrote:

    >Can someone explain to me how charPtr = string actually works


    The C++ language defines an automatic array-to-pointer conversion (see section
    4.2).


    >char string = 'x';
    >wouldn't work.


    Actually, that should work just fine.

    -dr
    Dave Rahardja, Nov 5, 2005
    #6
  7. >
    >>char string = 'x';
    >>wouldn't work.

    >
    >
    > Actually, that should work just fine.
    >


    Have you tried it?

    john
    John Harrison, Nov 5, 2005
    #7
  8. On Sat, 05 Nov 2005 07:20:44 GMT, John Harrison <>
    wrote:

    >>
    >>>char string = 'x';
    >>>wouldn't work.

    >>
    >>
    >> Actually, that should work just fine.
    >>

    >
    >Have you tried it?
    >


    Yes. Here's a complete program that compiles:

    int main()
    {
    char string = 'x';
    return 0;
    }
    Dave Rahardja, Nov 6, 2005
    #8
  9. chessc4c6

    Mike Wahler Guest

    "Dave Rahardja" <> wrote in message
    news:...
    > On Sat, 05 Nov 2005 07:20:44 GMT, John Harrison
    > <>
    > wrote:
    >
    >>>
    >>>>char string = 'x';
    >>>>wouldn't work.
    >>>
    >>>
    >>> Actually, that should work just fine.
    >>>

    >>
    >>Have you tried it?
    >>

    >
    > Yes. Here's a complete program that compiles:
    >
    > int main()
    > {
    > char string = 'x';
    > return 0;
    > }


    That's out of context of the original question.

    -Mike
    Mike Wahler, Nov 6, 2005
    #9
    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. Edward A Thompson
    Replies:
    4
    Views:
    516
    Tony Morris
    Feb 11, 2004
  2. Alberto Luaces
    Replies:
    6
    Views:
    377
    Fraser Ross
    Jun 19, 2006
  3. =?big5?B?r0W84Q==?=

    Why the address of array equals array.

    =?big5?B?r0W84Q==?=, Mar 14, 2007, in forum: C Programming
    Replies:
    8
    Views:
    414
    =?big5?B?r0W84Q==?=
    Mar 15, 2007
  4. Replies:
    2
    Views:
    360
  5. , India

    pointer to an array vs pointer to pointer

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

Share This Page