Pointers + dealing with integer arrays and strings

Discussion in 'C++' started by Slain, Jul 12, 2007.

  1. Slain

    Slain Guest

    I am a beginner and have some confusion with respect to pointers and
    strings. It seems that the pointers with dealing with integer arrays
    behave differently, as opposed to strings. Can some one explain me the
    difference?

    Sample Program:

    int main()
    {
    int array[]={1,2,3,4,5};
    char array1[]={"Name is Max"};

    int *ptr;
    char *ptr1;
    ptr=array;
    ptr1=array1;

    cout<<"The array is "<<array<<endl;
    cout<<"ptr is "<<ptr<<endl;
    cout<<"*ptr is "<<*ptr<<endl;

    cout<<"The array is "<<array1<<endl;
    cout<<"ptr1 is "<<ptr1<<endl;
    cout<<"*ptr1 is "<<*ptr1<<endl;
    }
    Output:
    The array is 0xbfe09380
    ptr is 0xbfe09380
    *ptr is 1

    The array is Name is Max
    ptr1 is Name is Max
    *ptr1 is N


    I can understand that in both the cases, *ptrx points to the element
    in it's address location. But why in the case of "ptr1", when I am
    using a string ptr1 refers to "Name is Max" and not the address of the
    "array1', like it did with "array".

    Thanks
    Manny
     
    Slain, Jul 12, 2007
    #1
    1. Advertising

  2. Slain wrote:

    > I am a beginner and have some confusion with respect to pointers and
    > strings. It seems that the pointers with dealing with integer arrays
    > behave differently, as opposed to strings. Can some one explain me the
    > difference?
    >
    > Sample Program:
    >
    > int main()
    > {
    > int array[]={1,2,3,4,5};
    > char array1[]={"Name is Max"};
    >
    > int *ptr;
    > char *ptr1;
    > ptr=array;
    > ptr1=array1;
    >
    > cout<<"The array is "<<array<<endl;


    array decays into a pointer to its first value. As cout has a way to output
    const void* (== void const*), but not int*, the pointer is implicitly
    converted to const void*.

    > cout<<"ptr is "<<ptr<<endl;


    Here, you already have an int*. It is converted to a const void* and
    printed.

    > cout<<"*ptr is "<<*ptr<<endl;


    *ptr is an int, and cout has a << operator to print ints.

    > cout<<"The array is "<<array1<<endl;


    As with array, array1 also decays to a pointer to its first element. But
    this time, the type is char*, and that can convert to a const char* (==
    char const*), for which cout has a special output operator: It is treated
    as a null terminated string.

    > cout<<"ptr1 is "<<ptr1<<endl;


    Will call the same operator as array1 did.

    > cout<<"*ptr1 is "<<*ptr1<<endl;


    And this uses the char output operator.

    > }
    > Output:
    > The array is 0xbfe09380
    > ptr is 0xbfe09380
    > *ptr is 1
    >
    > The array is Name is Max
    > ptr1 is Name is Max
    > *ptr1 is N
    >
    >
    > I can understand that in both the cases, *ptrx points to the element
    > in it's address location. But why in the case of "ptr1", when I am
    > using a string ptr1 refers to "Name is Max" and not the address of the
    > "array1', like it did with "array".


    Because the standard says const char* are to be treated specially by cout
    and other ostream objects. In the same manner, wostream and wcout treat
    const wchar_t* specially.

    --
    rbh
     
    Robert Bauck Hamar, Jul 12, 2007
    #2
    1. Advertising

  3. * Slain:
    > I am a beginner and have some confusion with respect to pointers and
    > strings. It seems that the pointers with dealing with integer arrays
    > behave differently, as opposed to strings. Can some one explain me the
    > difference?


    There's no difference in the behavior of the pointers or arrays, at the
    language level.

    There is a difference in how typical output functions deal with them.

    A typical output function will assume that a pointer to char points to a
    zero-terminated string.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jul 12, 2007
    #3
    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. agent349
    Replies:
    5
    Views:
    9,715
    asingh0412
    Mar 10, 2009
  2. Bill Reyn
    Replies:
    3
    Views:
    2,314
    Bob Hairgrove
    Jun 22, 2004
  3. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    400
  4. Replies:
    1
    Views:
    672
    -berlin.de
    Mar 28, 2005
  5. Philipp
    Replies:
    21
    Views:
    1,174
    Philipp
    Jan 20, 2009
Loading...

Share This Page