assigning array addresses to integer variables and vice versa

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

    #1

    assigning array addresses to integer variables and vice versa

    I have couple of questions related to array addresses. As they belong
    to the same block, I
    am putting them here in one single post. I hope nobody minds:

    char array[35];

    int address;

    Questions 1:
    Why cannot I do the following:

    address = (int)array;

    whereas I found it perfectly alright to do the following:

    address = (int)&array;

    Question 2:
    What is the difference between array and &array. Are not they the same
    thing i.e. starting
    address of the array.

    Question 3:

    Why cannot I do array++. Is array a pointer constant?

    Question 4:
    How can I do the following:

    array = address;

    i.e. Give a new starting address to the array. Is it possible?

    Thanks in advance for all Your replies.

    A.

  • pemo

    #2
    Re: assigning array addresses to integer variables and vice versa


    "anonymous" <call_ret@yahoo .com> wrote in message
    news:1132833621 .437259.58740@z 14g2000cwz.goog legroups.com...[color=blue]
    >I have couple of questions related to array addresses. As they belong
    > to the same block, I
    > am putting them here in one single post. I hope nobody minds:
    >
    > char array[35];
    >
    > int address;
    >
    > Questions 1:
    > Why cannot I do the following:
    >
    > address = (int)array;
    >
    > whereas I found it perfectly alright to do the following:
    >
    > address = (int)&array;
    >
    > Question 2:
    > What is the difference between array and &array. Are not they the same
    > thing i.e. starting
    > address of the array.
    >
    > Question 3:
    >
    > Why cannot I do array++. Is array a pointer constant?
    >
    > Question 4:
    > How can I do the following:
    >
    > array = address;
    >
    > i.e. Give a new starting address to the array. Is it possible?
    >
    > Thanks in advance for all Your replies.[/color]
    [color=blue]
    >Questions 1:
    >Why cannot I do the following:[/color]
    [color=blue]
    >address = (int)array;[/color]

    What do you by 'can't do'?

    [color=blue]
    > Question 2:
    > What is the difference between array and &array. Are not they the same
    > thing i.e. starting[/color]

    an array name decomposes into the address of its first element, i.e., a
    constant. You obviously cannot take the address of a 'value', and so &array
    doesn't make sense, and I assume the compiler's saying to itself 'well, ok,
    I know what he means'.

    [color=blue]
    > Question 3:
    >
    > Why cannot I do array++. Is array a pointer constant?[/color]

    Because an array name decomposes into the address of its first element, so
    it's like saying 42++;

    [color=blue]
    > Question 4:
    > How can I do the following:
    >
    > array = address;[/color]


    You can't, it'd be like saying 42 = 24;


    Comment

    • Krishanu Debnath

      #3
      Re: assigning array addresses to integer variables and vice versa

      anonymous wrote:[color=blue]
      > I have couple of questions related to array addresses. As they belong
      > to the same block, I
      > am putting them here in one single post. I hope nobody minds:
      >
      > char array[35];
      >
      > int address;
      >
      > Questions 1:
      > Why cannot I do the following:
      >
      > address = (int)array;[/color]

      No, it is a legal syntax. However pointer to integer conversion may
      invoke undefined behavior if result cannot be represented in integer
      type.
      [color=blue]
      >
      > whereas I found it perfectly alright to do the following:
      >
      > address = (int)&array;
      >
      > Question 2:
      > What is the difference between array and &array. Are not they the same
      > thing i.e. starting
      > address of the array.[/color]

      array and &array are same in value context, both yield pointer to
      first element in value context.
      [color=blue]
      >
      > Question 3:
      >
      > Why cannot I do array++. Is array a pointer constant?[/color]

      postfix increment operator needs a modifiable l-value. array name is
      not a modifiable l-value.
      [color=blue]
      >
      > Question 4:
      > How can I do the following:
      >
      > array = address;
      >
      > i.e. Give a new starting address to the array. Is it possible?[/color]

      No, see above.

      Krishanu

      Comment

      • anonymous

        #4
        Re: assigning array addresses to integer variables and vice versa


        pemo wrote:[color=blue]
        > "anonymous" <call_ret@yahoo .com> wrote in message
        > news:1132833621 .437259.58740@z 14g2000cwz.goog legroups.com...[color=green]
        > >I have couple of questions related to array addresses. As they belong
        > > to the same block, I
        > > am putting them here in one single post. I hope nobody minds:
        > >
        > > char array[35];
        > >
        > > int address;
        > >
        > > Questions 1:
        > > Why cannot I do the following:
        > >
        > > address = (int)array;
        > >[/color][/color]
        [color=blue][color=green]
        > > whereas I found it perfectly alright to do the following:
        > >
        > > address = (int)&array;
        > >
        > > Question 2:
        > > What is the difference between array and &array. Are not they the same
        > > thing i.e. starting
        > > address of the array.
        > >
        > > Question 3:
        > >
        > > Why cannot I do array++. Is array a pointer constant?
        > >
        > > Question 4:
        > > How can I do the following:
        > >
        > > array = address;
        > >
        > > i.e. Give a new starting address to the array. Is it possible?
        > >
        > > Thanks in advance for all Your replies.[/color]
        >[color=green]
        > >Questions 1:
        > >Why cannot I do the following:[/color]
        >[color=green]
        > >address = (int)array;[/color]
        >
        > What do you by 'can't do'?[/color]

        Ooops, checking again, it is perfectly alright to do
        address = (int)array;
        My fault somewhere.[color=blue]
        >[color=green]
        > > Question 2:
        > > What is the difference between array and &array. Are not they the same
        > > thing i.e. starting[/color]
        >
        > an array name decomposes into the address of its first element, i.e., a
        > constant. You obviously cannot take the address of a 'value', and so &array
        > doesn't make sense, and I assume the compiler's saying to itself 'well, ok,
        > I know what he means'.[/color]

        Why so liberty here when it keeps complaining all other times. I mean
        the compiler :-)[color=blue]
        >
        >[color=green]
        > > Question 3:
        > >
        > > Why cannot I do array++. Is array a pointer constant?[/color]
        >
        > Because an array name decomposes into the address of its first element, so
        > it's like saying 42++;
        >
        >[color=green]
        > > Question 4:
        > > How can I do the following:
        > >
        > > array = address;[/color]
        >
        >
        > You can't, it'd be like saying 42 = 24;[/color]

        Thanks for your help.

        Comment

        • Anand

          #5
          Re: assigning array addresses to integer variables and vice versa

          anonymous wrote:[color=blue]
          > I have couple of questions related to array addresses. As they belong
          > to the same block, I
          > am putting them here in one single post. I hope nobody minds:
          >
          > char array[35];
          >
          > int address;
          >
          > Questions 1:
          > Why cannot I do the following:
          >
          > address = (int)array;
          >
          > whereas I found it perfectly alright to do the following:
          >
          > address = (int)&array;
          >
          > Question 2:
          > What is the difference between array and &array. Are not they the same
          > thing i.e. starting
          > address of the array.
          >
          > Question 3:
          >
          > Why cannot I do array++. Is array a pointer constant?
          >
          > Question 4:
          > How can I do the following:
          >
          > array = address;
          >
          > i.e. Give a new starting address to the array. Is it possible?
          >
          > Thanks in advance for all Your replies.
          >
          > A.
          >[/color]
          Search this group for numerous discussion about hazels of converting int
          to pointer.

          Standard has added special type intptr_t and uintptr_t to enable integer
          types to hold object pointers.
          Discussed in section:
          "7.18.1.4 Integer types capable of holding object pointers".

          But this is a *nice to have*, your compiler can decide not implement
          this data type.

          Again, this is only for the conversion from/to void.


          To answer your specific questions.
          Q1: Both are undefined behavior

          Q2: Both means the same i.e. address of the first element.
          [ However the moment you pass an array to a function, the meaning
          changes, and hence meaning of array and &array differs there ]

          Q3&Q4: array is not an lvalue, see section:
          6.3.2.1 Lvalues, arrays, and function designators
          So you cannot modify it.
          ( Again the meaning of array changes the moment you pass it to
          function)

          --
          (Welcome) http://www.ungerhu.com/jxh/clc.welcome.txt
          (clc FAQ) http://www.eskimo.com/~scs/C-faq/top.html

          Comment

          • Suman

            #6
            Re: assigning array addresses to integer variables and vice versa


            pemo wrote:[color=blue]
            > "anonymous" <call_ret@yahoo .com> wrote in message
            > news:1132833621 .437259.58740@z 14g2000cwz.goog legroups.com...[/color]
            [snip][color=blue][color=green]
            > > Question 2:
            > > What is the difference between array and &array. Are not they the same
            > > thing i.e. starting[/color]
            >
            > an array name decomposes into the address of its first element, i.e., a
            > constant.
            >[/color]
            [snip]

            Well, almost.
            [color=blue]
            > You obviously cannot take the address of a 'value', and so &array
            > doesn't make sense, and I assume the compiler's saying to itself 'well, ok,
            > I know what he means'.[/color]

            In a value context, an object of type "array N of T" becomes
            a value of type "pointer to T", pointing to the first element
            of that array, i.e., the one with subscript 0.

            And do search the archives.

            Comment

            • anonymous

              #7
              Re: assigning array addresses to integer variables and vice versa


              Anand wrote:[color=blue]
              > anonymous wrote:[color=green]
              > > I have couple of questions related to array addresses. As they belong
              > > to the same block, I
              > > am putting them here in one single post. I hope nobody minds:
              > >
              > > char array[35];
              > >
              > > int address;
              > >
              > > Questions 1:
              > > Why cannot I do the following:
              > >
              > > address = (int)array;
              > >
              > > whereas I found it perfectly alright to do the following:
              > >
              > > address = (int)&array;
              > >
              > > Question 2:
              > > What is the difference between array and &array. Are not they the same
              > > thing i.e. starting
              > > address of the array.
              > >
              > > Question 3:
              > >
              > > Why cannot I do array++. Is array a pointer constant?
              > >
              > > Question 4:
              > > How can I do the following:
              > >
              > > array = address;
              > >
              > > i.e. Give a new starting address to the array. Is it possible?
              > >
              > > Thanks in advance for all Your replies.
              > >
              > > A.
              > >[/color]
              > Search this group for numerous discussion about hazels of converting int
              > to pointer.
              >
              > Standard has added special type intptr_t and uintptr_t to enable integer
              > types to hold object pointers.
              > Discussed in section:
              > "7.18.1.4 Integer types capable of holding object pointers".
              >
              > But this is a *nice to have*, your compiler can decide not implement
              > this data type.
              >
              > Again, this is only for the conversion from/to void.[/color]

              I think no compiler has it implemented as yet ? Is this so?[color=blue]
              >
              >
              > To answer your specific questions.
              > Q1: Both are undefined behavior
              >
              > Q2: Both means the same i.e. address of the first element.
              > [ However the moment you pass an array to a function, the meaning
              > changes, and hence meaning of array and &array differs there ][/color]

              Can you be more elaborate on this please?
              [color=blue]
              >
              > Q3&Q4: array is not an lvalue, see section:
              > 6.3.2.1 Lvalues, arrays, and function designators
              > So you cannot modify it.
              > ( Again the meaning of array changes the moment you pass it to
              > function)
              >
              > --
              > (Welcome) http://www.ungerhu.com/jxh/clc.welcome.txt
              > (clc FAQ) http://www.eskimo.com/~scs/C-faq/top.html[/color]

              Can I get a copy of the standard for download from somewhere?

              Thanks in advance!

              Comment

              • Eric Sosman

                #8
                Re: assigning array addresses to integer variables and vice versa

                anonymous wrote:
                [color=blue]
                > I have couple of questions related to array addresses. As they belong
                > to the same block, I
                > am putting them here in one single post. I hope nobody minds:
                >
                > char array[35];
                >
                > int address;
                >
                > Questions 1:
                > Why cannot I do the following:
                >
                > address = (int)array;[/color]

                You can, if you like. The result will be useful on
                some machines, useless on others. I wouldn't recommend it.
                [color=blue]
                > whereas I found it perfectly alright to do the following:
                >
                > address = (int)&array;[/color]

                That's equally legitimate, equally useful or useless.
                Again, I wouldn't recommend it.
                [color=blue]
                > Question 2:
                > What is the difference between array and &array. Are not they the same
                > thing i.e. starting
                > address of the array.[/color]

                This is Question 6.12 in the comp.lang.c Frequently Asked
                Questions (FAQ) list


                [color=blue]
                > Question 3:
                >
                > Why cannot I do array++. Is array a pointer constant?[/color]

                FAQ Question 6.7.
                [color=blue]
                > Question 4:
                > How can I do the following:
                >
                > array = address;
                >
                > i.e. Give a new starting address to the array. Is it possible?[/color]

                No.
                [color=blue]
                > Thanks in advance for all Your replies.[/color]

                You're welcome. Read the FAQ; it will do you good.

                --
                Eric Sosman
                esosman@acm-dot-org.invalid

                Comment

                • Eric Sosman

                  #9
                  Re: assigning array addresses to integer variables and vice versa

                  Krishanu Debnath wrote:
                  [color=blue]
                  > array and &array are same in value context, both yield pointer to
                  > first element in value context.[/color]

                  They are not the same. Both produce pointers, but
                  point to different objects. Please read the FAQ.

                  --
                  Eric Sosman
                  esosman@acm-dot-org.invalid

                  Comment

                  • Jordan Abel

                    #10
                    Re: assigning array addresses to integer variables and vice versa

                    On 2005-11-24, Eric Sosman <esosman@acm-dot-org.invalid> wrote:[color=blue]
                    > Krishanu Debnath wrote:
                    >[color=green]
                    >> array and &array are same in value context, both yield pointer to
                    >> first element in value context.[/color]
                    >
                    > They are not the same. Both produce pointers, but
                    > point to different objects. Please read the FAQ.[/color]

                    They will compare equal, though, if converted to a void *

                    [because they point to the same memory location]

                    Comment

                    • Flash Gordon

                      #11
                      Re: assigning array addresses to integer variables and vice versa

                      Suman wrote:[color=blue]
                      > pemo wrote:[color=green]
                      >> "anonymous" <call_ret@yahoo .com> wrote in message
                      >> news:1132833621 .437259.58740@z 14g2000cwz.goog legroups.com...[/color]
                      > [snip][color=green][color=darkred]
                      >>> Question 2:
                      >>> What is the difference between array and &array. Are not they the same
                      >>> thing i.e. starting[/color]
                      >> an array name decomposes into the address of its first element, i.e., a
                      >> constant.
                      >>[/color]
                      > [snip]
                      >
                      > Well, almost.
                      >[color=green]
                      >> You obviously cannot take the address of a 'value', and so &array
                      >> doesn't make sense, and I assume the compiler's saying to itself 'well, ok,
                      >> I know what he means'.[/color]
                      >
                      > In a value context, an object of type "array N of T" becomes
                      > a value of type "pointer to T", pointing to the first element
                      > of that array, i.e., the one with subscript 0.[/color]

                      Not when it is the operand of either sizeof or the unary &, then it does
                      *not* degenerate to a pointer. So the type of &array is pointer to
                      array, where as the type of pointer array degenerates to is pointer to
                      T. So they are different types.
                      [color=blue]
                      > And do search the archives.[/color]

                      Also check the FAQ where it will be far easier to find the correct
                      information.
                      --
                      Flash Gordon
                      Living in interesting times.
                      Although my email address says spam, it is real and I read it.

                      Comment

                      • Flash Gordon

                        #12
                        Re: assigning array addresses to integer variables and vice versa

                        Krishanu Debnath wrote:[color=blue]
                        > anonymous wrote:[color=green]
                        >> I have couple of questions related to array addresses. As they belong
                        >> to the same block, I
                        >> am putting them here in one single post. I hope nobody minds:
                        >>
                        >> char array[35];
                        >>
                        >> int address;[/color][/color]

                        <snip>
                        [color=blue][color=green]
                        >> Question 2:
                        >> What is the difference between array and &array. Are not they the same
                        >> thing i.e. starting
                        >> address of the array.[/color]
                        >
                        > array and &array are same in value context, both yield pointer to
                        > first element in value context.[/color]

                        No, they have different types. &array has the type pointer to array of
                        35 char, where as array degenerates to a pointer to char.

                        Check the comp.lang.c FAQ

                        <snip>
                        --
                        Flash Gordon
                        Living in interesting times.
                        Although my email address says spam, it is real and I read it.

                        Comment

                        • Flash Gordon

                          #13
                          Re: assigning array addresses to integer variables and vice versa

                          anonymous wrote:[color=blue]
                          > Anand wrote:[color=green]
                          >> anonymous wrote:[color=darkred]
                          >>> I have couple of questions related to array addresses. As they belong
                          >>> to the same block, I
                          >>> am putting them here in one single post. I hope nobody minds:
                          >>>
                          >>> char array[35];
                          >>>
                          >>> int address;
                          >>>
                          >>> Questions 1:
                          >>> Why cannot I do the following:
                          >>>
                          >>> address = (int)array;
                          >>>
                          >>> whereas I found it perfectly alright to do the following:
                          >>>
                          >>> address = (int)&array;
                          >>>
                          >>> Question 2:
                          >>> What is the difference between array and &array. Are not they the same
                          >>> thing i.e. starting
                          >>> address of the array.[/color][/color][/color]

                          <snip>
                          [color=blue][color=green]
                          >> Q2: Both means the same i.e. address of the first element.
                          >> [ However the moment you pass an array to a function, the meaning
                          >> changes, and hence meaning of array and &array differs there ][/color]
                          >
                          > Can you be more elaborate on this please?[/color]

                          It's not quite right. &array is always different from array, they differ
                          in type. So, given your definition above, the compiler *must* complain
                          if you pass &array to a function with a prototype in scope specifying a
                          pointer to char. E.g.

                          #include <string.h>
                          int main(void)
                          {
                          char array[35] = "hello";
                          size_t len1 = strlen(array); /* this is allowed */
                          size_t len2 = strlen(&array); /* The compiler is required to complain */
                          }

                          &array has type pointer to array, where as array otherwise degenerates
                          to a pointer to char.
                          [color=blue][color=green]
                          >> Q3&Q4: array is not an lvalue, see section:
                          >> 6.3.2.1 Lvalues, arrays, and function designators
                          >> So you cannot modify it.
                          >> ( Again the meaning of array changes the moment you pass it to
                          >> function)
                          >>
                          >> --
                          >> (Welcome) http://www.ungerhu.com/jxh/clc.welcome.txt
                          >> (clc FAQ) http://www.eskimo.com/~scs/C-faq/top.html[/color]
                          >
                          > Can I get a copy of the standard for download from somewhere?[/color]

                          Google for n1124 and you will find the draft for C99 + all the
                          subsequent corrections. That and all the other publicly available copies
                          are at http://www.open-std.org/jtc1/sc22/wg14/www/docs/ or you can buy a
                          copy of the actual release version.

                          I think though that you really need to work through a good text book
                          though, such as K&R2, and also read the FAQ (the URL is above) which
                          will tell you what K&R2 is (in the Bibliography, I think).
                          --
                          Flash Gordon
                          Living in interesting times.
                          Although my email address says spam, it is real and I read it.

                          Comment

                          • Keith Thompson

                            #14
                            Re: assigning array addresses to integer variables and vice versa

                            Anand <Anand@no-replies.com> writes:[color=blue]
                            > anonymous wrote:[color=green]
                            >> I have couple of questions related to array addresses. As they belong
                            >> to the same block, I
                            >> am putting them here in one single post. I hope nobody minds:
                            >> char array[35];
                            >> int address;
                            >> Questions 1:
                            >> Why cannot I do the following:
                            >> address = (int)array;
                            >> whereas I found it perfectly alright to do the following:
                            >> address = (int)&array;
                            >> Question 2:
                            >> What is the difference between array and &array. Are not they the same
                            >> thing i.e. starting
                            >> address of the array.
                            >> Question 3:
                            >> Why cannot I do array++. Is array a pointer constant?
                            >> Question 4:
                            >> How can I do the following:
                            >> array = address;
                            >> i.e. Give a new starting address to the array. Is it possible?
                            >> Thanks in advance for all Your replies.
                            >> A.
                            >>[/color]
                            > Search this group for numerous discussion about hazels of converting
                            > int to pointer.
                            >
                            > Standard has added special type intptr_t and uintptr_t to enable
                            > integer types to hold object pointers.
                            > Discussed in section:
                            > "7.18.1.4 Integer types capable of holding object pointers".
                            >
                            > But this is a *nice to have*, your compiler can decide not implement
                            > this data type.
                            >
                            > Again, this is only for the conversion from/to void.
                            >
                            >
                            > To answer your specific questions.
                            > Q1: Both are undefined behavior[/color]

                            Converting a pointer to an integer *can* invoke undefined behavior.
                            Specifically:

                            Any pointer type may be converted to an integer type. Except as
                            previously specified, the result is implementation-defined. If the
                            result cannot be represented in the integer type, the behavior is
                            undefined. The result need not be in the range of values of any
                            integer type.
                            [color=blue]
                            > Q2: Both means the same i.e. address of the first element.
                            > [ However the moment you pass an array to a function, the meaning
                            > changes, and hence meaning of array and &array differs there ][/color]

                            As others have pointed out, they're not the same; they have different
                            types.

                            There's nothing special about function calls. An array expression is
                            implicitly converted to a pointer to its first element in *any*
                            context other than as the operand of a unary "&" or "sizeof", or when
                            it's a string literal in an initializer.

                            --
                            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                            We must do something. This is something. Therefore, we must do this.

                            Comment

                            • Jordan Abel

                              #15
                              Re: assigning array addresses to integer variables and vice versa

                              On 2005-11-24, Keith Thompson <kst-u@mib.org> wrote:[color=blue][color=green]
                              >> Q2: Both means the same i.e. address of the first element.
                              >> [ However the moment you pass an array to a function, the meaning
                              >> changes, and hence meaning of array and &array differs there ][/color]
                              >
                              > As others have pointed out, they're not the same; they have different
                              > types.
                              >
                              > There's nothing special about function calls. An array expression is
                              > implicitly converted to a pointer to its first element in *any*
                              > context other than as the operand of a unary "&" or "sizeof", or when
                              > it's a string literal in an initializer.[/color]

                              well, there _is_ one thing special about functions, but it pertains to
                              function prototypes, not funtion calls: the fact that declaring what
                              appears to be an array formal parameter is in fact a pointer formal
                              parameter [and thus, int main(int argc, char *argv[5]); is compatible
                              with int main(int argc, char **argv);, and sizeof argv == sizeof(char
                              **) in both cases, and not 5*sizeof(char *).]

                              Comment

                              Working...