array's

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • las

    array's

    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 :)



  • Stewart Gordon

    #2
    Re: array's

    las wrote:
    [color=blue]
    > Hello, Im trying to figure out the difference between int[] and char[][/color]

    int[] is an array of integers.
    char[] is an array of characters.
    [color=blue]
    > #include <iostream>
    > #include <stdlib.h>
    >
    > int main()
    > {
    > int i[10] = {1};
    > char c[10] = {'1'};
    >
    > cout << "&int \t" << &i << endl;[/color]
    <snip>[color=blue]
    > I get this output :[/color]

    That's interesting. On mine, it doesn't even compile.
    [color=blue]
    > &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 ?[/color]
    <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.

    Comment

    • John Harrison

      #3
      Re: array's


      "las" <laasunde@onlin e.no> wrote in message
      news:cERQa.1328 8$Hb.228337@new s4.e.nsc.no...[color=blue]
      > 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 :)
      >[/color]

      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


      Comment

      • Default User

        #4
        Re: array's



        John Harrison wrote:
        [color=blue]
        > 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).[/color]


        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

        Comment

        • John Harrison

          #5
          Re: array's


          "Default User" <first.last@com pany.com> wrote in message
          news:3F143FA3.C 6B13AEC@company .com...[color=blue]
          >
          >
          > John Harrison wrote:
          >[color=green]
          > > First your code is wrong. The only way to guarantee the behaviour you[/color][/color]
          see is[color=blue][color=green]
          > > to do this
          > >
          > > char c[10] = {'1', '\0'};
          > >
          > > This makes c a 'null terminated string' ('\0' is the null).[/color]
          >
          >
          > 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.
          >[/color]

          OK, I stand corrected.

          john


          Comment

          Working...