pointer and array

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

    #16
    Re: pointer and array

    On Sun, 19 Dec 2004 19:39:02 +0000, Joona I Palaste wrote:
    [color=blue]
    > Leon Brodskiy <ivan@rogers.co m> scribbled the following:[color=green]
    >> If we leave char array pointer for a second, can we say that the program
    >> will always work correctly?[/color]
    >
    > No. The C standard allows for an implementation to behave erroneously
    > when a pointer value is assigned to a variable of incompatible type.[/color]

    A compiler can refuse to compile the program, indeed this is a reasonable
    thing for a compiler to do in this case.
    [color=blue][color=green]
    >> Would it be correct to say that a difference between str and &str is
    >> that str is char* (points to one single character) and &str is a
    >> pointer to an array(points to array of 10 characters)?[/color]
    >
    > Yes.
    >[color=green]
    >> If this is a true then in the
    >> program should not be any difference if we use &str or str - both of
    >> them will send to the function the address of the first element in the
    >> array.[/color][/color]

    No, the value of str is a pointer to the first element of the array, &str
    is a pointer to the array as a whole. There is no requirement that these
    two types of pointer have the same representation or are passed in the
    same way in a function call. Often things happen to work, but that is not
    the same thing as the source code being correct.
    [color=blue]
    > That is not true in the general case. At least I think it's not. Maybe
    > some of the real C gurus here can answer my original question about
    > whether it is undefined behaviour?[/color]

    The program contains a constraint violation which means that it isn't a
    valid C program. So issues like behaviour (from a C language
    perspective) don't apply. OTOH since it isn't a valid C program a C
    compiler can do what it likes with it after issuing the required
    diagnostic. So from that perspective it is very much like undefined
    behaviour.

    Lawrence

    Comment

    • Barry Schwarz

      #17
      Re: pointer and array

      On Sun, 19 Dec 2004 14:02:20 -0800, "Leon Brodskiy" <ivan@rogers.co m>
      wrote:
      [color=blue]
      >If we leave char array pointer for a second, can we say that the program
      >will always work correctly?
      >
      >Would it be correct to say that a difference between str and &str is that
      >str is char* (points to one single character) and &str is a pointer to an
      >array(points to array of 10 characters)? If this is a true then in the
      >program should not be any difference if we use &str or str - both of them
      >will send to the function the address of the first element in the array.[/color]

      It is not necessary for a pointer to char and a pointer to array of
      char to have the same representation. They could even be of different
      sizes or have different alignment requirements.


      <<Remove the del for email>>

      Comment

      • Barry Schwarz

        #18
        Re: pointer and array

        On Sun, 19 Dec 2004 19:43:10 -0800, "E. Robert Tisdale"
        <E.Robert.Tisda le@jpl.nasa.gov > wrote:
        [color=blue]
        >Leon Brodskiy wrote:
        >[color=green]
        >> Could please anyone clarify about pointer and array in C?
        >>
        >> If I have:
        >>
        >> int arr[10];
        >>
        >> The following two commands will be the same: arr and &arr[0].
        >>
        >> What about &arr? Is it not the same thing?[/color]
        >[color=green]
        > > cat main.c[/color]
        > #include <stdio.h>
        >
        > int main(int argc, char* argv[]) {
        > int arr[10];
        > fprintf(stdout, "sizeof(arr ) = %u\n", sizeof(arr));[/color]

        Since size_t need not be an unsigned int, a cast would be appropriate
        here.
        [color=blue]
        > fprintf(stdout, "sizeof(&ar r[0]) = %u\n", sizeof(&arr[0]));
        > return 0;
        > }
        >[color=green]
        > > gcc -Wall -std=c99 -pedantic -o main main.c
        > > ./main[/color]
        > sizeof(arr) = 40
        > sizeof(&arr[0]) = 4
        >
        >arr is the name of an array of 10 objects of type int.
        >&arr[0] is a pointer to the first object in that array.[/color]



        <<Remove the del for email>>

        Comment

        • Emmanuel Delahaye

          #19
          Re: pointer and array

          Leon Brodskiy wrote on 19/12/04 :[color=blue]
          > Hi,
          >
          > Could please anyone clarify about pointer and array in C?
          >
          > If I have:
          >
          > int arr[10];
          >
          > The following two commands will be the same: arr and &arr[0].[/color]

          These are not 'commands' They are 'expressions'.

          'arr' is the name of the array. Because arr == arr + 0, it is also the
          address of its first element (&arr[0] is a complicated way of writing
          arr + 0).
          [color=blue]
          > What about &arr? Is it not the same thing?[/color]

          No. It's the address of the array (same value), *but the type is
          different*. Actually it has the 'int (*)[10]' type, and can be assigned
          to a pointer of the same type:

          int arr[10];

          int (*p)[10] = &arr;

          Is it useful or not is another question.

          --
          Emmanuel
          The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
          The C-library: http://www.dinkumware.com/refxc.html

          "Clearly your code does not meet the original spec."
          "You are sentenced to 30 lashes with a wet noodle."
          -- Jerry Coffin in a.l.c.c++

          Comment

          • Emmanuel Delahaye

            #20
            Re: pointer and array

            Leon Brodskiy wrote on 19/12/04 :[color=blue]
            > I'm asking because I have found in a program the following call:
            >
            > void f1(char inp[10])
            > {...}
            >
            > void main()
            > {...
            > char str[10];
            > ...
            > f1(&str);
            > ...
            > }
            >
            > I tought this is a bug and this is not supposed to work but it does work.[/color]

            It's a bug because the types are not the same. (A decent and well
            configured compiler should yell on it). It 'works' because the value is
            the same, but the '&' should be removed.
            [color=blue]
            > Function receives pointer to char. I send to the function a pointer to a
            > pointer to char<...>[/color]

            Wrong. You have sent the address of a pointer to an array of 10 char.
            Its type is 'char(*)[10]'.

            --
            Emmanuel
            The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
            The C-library: http://www.dinkumware.com/refxc.html

            "C is a sharp tool"

            Comment

            • Lawrence Kirby

              #21
              Re: pointer and array

              On Wed, 22 Dec 2004 07:43:21 +0100, Emmanuel Delahaye wrote:

              ....
              [color=blue]
              > No. It's the address of the array (same value), *but the type is
              > different*. Actually it has the 'int (*)[10]' type, and can be assigned
              > to a pointer of the same type:
              >
              > int arr[10];
              >
              > int (*p)[10] = &arr;
              >
              > Is it useful or not is another question.[/color]

              It is useful for arrays of arrays e.g.

              int arr2[3][10];
              int (*p)[10] = &arr2[0];

              arr2[0] is an array (with type array of 10 ints) and &arr2[0] is a pointer
              to that array (with type pointer to an array of 10 ints, the same as p).
              Because of p's type expressions like p[x][y] make sense and with p set up
              like this will access the same element as arr2[x][y].

              Yes, I could have written just

              int (*p)[10] = arr2;

              in this case but you can naturally express things like things like

              p = &arr2[1];

              Lawrence

              Comment

              • Lawrence Kirby

                #22
                Re: pointer and array

                On Wed, 22 Dec 2004 07:51:14 +0100, Emmanuel Delahaye wrote:
                [color=blue]
                > Leon Brodskiy wrote on 19/12/04 :[color=green]
                >> I'm asking because I have found in a program the following call:
                >>
                >> void f1(char inp[10])
                >> {...}
                >>
                >> void main()[/color][/color]

                You can never say too much that main returns int. :-)
                [color=blue][color=green]
                >> {...
                >> char str[10];
                >> ...
                >> f1(&str);
                >> ...
                >> }
                >>
                >> I tought this is a bug and this is not supposed to work but it does work.[/color]
                >
                > It's a bug because the types are not the same. (A decent and well
                > configured compiler should yell on it). It 'works' because the value is
                > the same, but the '&' should be removed.[/color]

                str and &str evaluate to pointers to different objects (an int vs. an
                array of 10 ints) so in a very real sense their values are different.
                Those two objects do happen to share the same starting byte in memory, so
                it is common for implementations to use the same representation for
                those two pointer values. But that isn't guaranteed.
                ..
                Type is important when considering whether two values are the same. Are 1
                and 1L the same value, do they have the same representation in an object?
                What about 1 and 1.0? Then -1 and ~0U (think of likely representations on
                a 2's complement system). Now consider different pointer types that you
                can't even compare directly.
                [color=blue][color=green]
                >> Function receives pointer to char. I send to the function a pointer to a
                >> pointer to char<...>[/color]
                >
                > Wrong. You have sent the address of a pointer to an array of 10 char.
                > Its type is 'char(*)[10]'.[/color]

                Your main point is of course correct. Passing a pointer to an array to a
                function that requires a pointer to char with a prototype in scope is a
                constraint violation. a conforming C compiler must produce a diagnostic.

                Lawrence

                Comment

                • Emmanuel Delahaye

                  #23
                  Re: pointer and array

                  Lawrence Kirby wrote on 22/12/04 :

                  <all good stuff snipped>
                  [color=blue]
                  > Your main point is of course correct. Passing a pointer to an array to a
                  > function that requires a pointer to char with a prototype in scope is a
                  > constraint violation. a conforming C compiler must produce a diagnostic.[/color]

                  Tanks for these details.

                  --
                  Emmanuel
                  The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
                  The C-library: http://www.dinkumware.com/refxc.html

                  "Clearly your code does not meet the original spec."
                  "You are sentenced to 30 lashes with a wet noodle."
                  -- Jerry Coffin in a.l.c.c++

                  Comment

                  • Joona I Palaste

                    #24
                    Re: pointer and array

                    Emmanuel Delahaye <emdel@yourbran oos.fr> scribbled the following:[color=blue]
                    > Lawrence Kirby wrote on 22/12/04 :[/color]
                    [color=blue]
                    > <all good stuff snipped>[/color]
                    [color=blue][color=green]
                    >> Your main point is of course correct. Passing a pointer to an array to a
                    >> function that requires a pointer to char with a prototype in scope is a
                    >> constraint violation. a conforming C compiler must produce a diagnostic.[/color][/color]
                    [color=blue]
                    > Tanks for these details.[/color]

                    Will they be capable of firing live ammunition?

                    --
                    /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
                    \-------------------------------------------------------- rules! --------/
                    "Products like that make me wish I could menstruate."
                    - Andy Richter

                    Comment

                    Working...