array's

Discussion in 'C++' started by las, Jul 15, 2003.

  1. las

    las Guest

    Hello, Im trying to figure out the difference between int[] and char[]

    #include <iostream>
    #include <stdlib.h>

    int main()
    {
    int i[10] = {1};
    char c[10] = {'1'};

    cout << "&int \t" << &i << endl;
    cout << "int \t " << i << endl;
    cout << "int[0]\t" << i[0] << endl;
    cout << endl;
    cout << "&char \t" << &c << endl;
    cout << "char\t " << c << endl;
    cout << "char[0]\t" << c[0] << endl;
    cout << endl;
    system("PAUSE");
    return 0;
    }

    I get this output :

    &int 0x254fda8
    int 0x254fda8
    int[0] 1

    &char 0x254fd98
    char 1
    char[0] 1

    I belive long, float, double etc work the same way as int does in my above
    example. How is char[] different for these other variables ? Any links to
    related tutorials would be great aswell.

    Cheers :)
    las, Jul 15, 2003
    #1
    1. Advertising

  2. las wrote:

    > Hello, Im trying to figure out the difference between int[] and char[]


    int[] is an array of integers.
    char[] is an array of characters.

    > #include <iostream>
    > #include <stdlib.h>
    >
    > int main()
    > {
    > int i[10] = {1};
    > char c[10] = {'1'};
    >
    > cout << "&int \t" << &i << endl;

    <snip>
    > I get this output :


    That's interesting. On mine, it doesn't even compile.

    > &int 0x254fda8
    > int 0x254fda8
    > int[0] 1
    >
    > &char 0x254fd98
    > char 1
    > char[0] 1
    >
    > I belive long, float, double etc work the same way as int does in my above
    > example. How is char[] different for these other variables ?

    <snip>

    It isn't, apart from the syntactic sugar and standard libraries enabling
    char arrays to be used as strings.

    When you send an int, long, float or double to a stream, the output
    defaults to the numerical value in decimal.

    OTOH, when a char is sent, the output is the character itself, as
    distinct from the numerical value, which is the ASCII (or other platform
    character set) code of the character.

    When a pointer is sent, the default is the memory address in
    hexadecimal. The name of an array by itself denotes a pointer. The
    exception is a pointer to char, IWC the char array is output as a
    null-terminated string.

    As for why &char and char gave different values, &c and c are the same
    memory address but are of different types. They are char[10]* (pointer
    to array of 10 chars) and char* respectively. This difference in
    referent size means that incrementing a char* will increment by one
    char, whereas incrementing a char[10]* will increment by a block of 10
    chars.

    Stewart.

    --
    My e-mail is valid but not my primary mailbox. Please keep replies on
    on the 'group where everyone may benefit.
    Stewart Gordon, Jul 15, 2003
    #2
    1. Advertising

  3. "las" <> wrote in message
    news:cERQa.13288$...
    > Hello, Im trying to figure out the difference between int[] and char[]
    >
    > #include <iostream>
    > #include <stdlib.h>
    >
    > int main()
    > {
    > int i[10] = {1};
    > char c[10] = {'1'};
    >
    > cout << "&int \t" << &i << endl;
    > cout << "int \t " << i << endl;
    > cout << "int[0]\t" << i[0] << endl;
    > cout << endl;
    > cout << "&char \t" << &c << endl;
    > cout << "char\t " << c << endl;
    > cout << "char[0]\t" << c[0] << endl;
    > cout << endl;
    > system("PAUSE");
    > return 0;
    > }
    >
    > I get this output :
    >
    > &int 0x254fda8
    > int 0x254fda8
    > int[0] 1
    >
    > &char 0x254fd98
    > char 1
    > char[0] 1
    >
    > I belive long, float, double etc work the same way as int does in my above
    > example. How is char[] different for these other variables ? Any links to
    > related tutorials would be great aswell.
    >
    > Cheers :)
    >


    Such a simple question, such a complex answer.

    First your code is wrong. The only way to guarantee the behaviour you see is
    to do this

    char c[10] = {'1', '\0'};

    This makes c a 'null terminated string' ('\0' is the null). In C++ arrays of
    chars are often used as null terminated strings. operator<< knows this an
    given an array of char will attempt (rightly or wrongly) to interpret it as
    a null terminated string and print out all of the string. Gvien any other
    sort of array operator<< will print out the address of the first element of
    the array, that's what you see with your int array.

    You might want to experiment with other values for c, e.g.

    char c[10] = {'h', 'e', 'l', 'l', 'o', '\0'};

    john
    John Harrison, Jul 15, 2003
    #3
  4. las

    Default User Guest

    John Harrison wrote:

    > First your code is wrong. The only way to guarantee the behaviour you see is
    > to do this
    >
    > char c[10] = {'1', '\0'};
    >
    > This makes c a 'null terminated string' ('\0' is the null).



    Nope nope nope. If you init any values in an array, the remainder of the
    elements are initialized to the type-specific 0 value. So the addition
    of the null-terminator here is superfluous.




    Brian Rodenborn
    Default User, Jul 15, 2003
    #4
  5. "Default User" <> wrote in message
    news:...
    >
    >
    > John Harrison wrote:
    >
    > > First your code is wrong. The only way to guarantee the behaviour you

    see is
    > > to do this
    > >
    > > char c[10] = {'1', '\0'};
    > >
    > > This makes c a 'null terminated string' ('\0' is the null).

    >
    >
    > Nope nope nope. If you init any values in an array, the remainder of the
    > elements are initialized to the type-specific 0 value. So the addition
    > of the null-terminator here is superfluous.
    >


    OK, I stand corrected.

    john
    John Harrison, Jul 15, 2003
    #5
    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. Daniel
    Replies:
    1
    Views:
    1,049
    Kevin Collins
    Aug 14, 2003
  2. S300
    Replies:
    4
    Views:
    21,991
    Roedy Green
    Aug 19, 2003
  3. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    488
    David RF
    Sep 3, 2009
  4. Tom
    Replies:
    3
    Views:
    207
    salsablr
    Dec 20, 2004
  5. Tuan  Bui
    Replies:
    14
    Views:
    471
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page