Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > assigning array addresses to integer variables and vice versa

Reply
Thread Tools

assigning array addresses to integer variables and vice versa

 
 
anonymous
Guest
Posts: n/a
 
      11-24-2005
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.

 
Reply With Quote
 
 
 
 
pemo
Guest
Posts: n/a
 
      11-24-2005

"anonymous" <> wrote in message
news: ups.com...
>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.


>Questions 1:
>Why cannot I do the following:


>address = (int)array;


What do you by 'can't do'?


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


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'.


> Question 3:
>
> Why cannot I do array++. Is array a pointer constant?


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


> Question 4:
> How can I do the following:
>
> array = address;



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


 
Reply With Quote
 
 
 
 
Krishanu Debnath
Guest
Posts: n/a
 
      11-24-2005
anonymous wrote:
> 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;


No, it is a legal syntax. However pointer to integer conversion may
invoke undefined behavior if result cannot be represented in integer
type.

>
> 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.


array and &array are same in value context, both yield pointer to
first element in value context.

>
> Question 3:
>
> Why cannot I do array++. Is array a pointer constant?


postfix increment operator needs a modifiable l-value. array name is
not a modifiable l-value.

>
> Question 4:
> How can I do the following:
>
> array = address;
>
> i.e. Give a new starting address to the array. Is it possible?


No, see above.

Krishanu
 
Reply With Quote
 
anonymous
Guest
Posts: n/a
 
      11-24-2005

pemo wrote:
> "anonymous" <> wrote in message
> news: ups.com...
> >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.

>
> >Questions 1:
> >Why cannot I do the following:

>
> >address = (int)array;

>
> What do you by 'can't do'?


Ooops, checking again, it is perfectly alright to do
address = (int)array;
My fault somewhere.
>
> > Question 2:
> > What is the difference between array and &array. Are not they the same
> > thing i.e. starting

>
> 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'.


Why so liberty here when it keeps complaining all other times. I mean
the compiler
>
>
> > Question 3:
> >
> > Why cannot I do array++. Is array a pointer constant?

>
> Because an array name decomposes into the address of its first element, so
> it's like saying 42++;
>
>
> > Question 4:
> > How can I do the following:
> >
> > array = address;

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


Thanks for your help.

 
Reply With Quote
 
Anand
Guest
Posts: n/a
 
      11-24-2005
anonymous wrote:
> 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.
>

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
 
Reply With Quote
 
Suman
Guest
Posts: n/a
 
      11-24-2005

pemo wrote:
> "anonymous" <> wrote in message
> news: ups.com...

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

>
> an array name decomposes into the address of its first element, i.e., a
> constant.
>

[snip]

Well, almost.

> 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'.


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.

 
Reply With Quote
 
anonymous
Guest
Posts: n/a
 
      11-24-2005

Anand wrote:
> anonymous wrote:
> > 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.
> >

> 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.


I think no compiler has it implemented as yet ? Is this so?
>
>
> 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 ]


Can you be more elaborate on this please?

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


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

Thanks in advance!

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      11-24-2005
Suman wrote:
> pemo wrote:
>> "anonymous" <> wrote in message
>> news: ups.com...

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

>> an array name decomposes into the address of its first element, i.e., a
>> constant.
>>

> [snip]
>
> Well, almost.
>
>> 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'.

>
> 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.


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.

> And do search the archives.


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.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      11-24-2005
Krishanu Debnath wrote:
> anonymous wrote:
>> 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;


<snip>

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

>
> array and &array are same in value context, both yield pointer to
> first element in value context.


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.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      11-24-2005
anonymous wrote:
> Anand wrote:
>> anonymous wrote:
>>> 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.


<snip>

>> 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 ]

>
> Can you be more elaborate on this please?


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.

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

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


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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting int and short to byte array and vice versa carmen Java 4 01-12-2010 05:00 PM
converting floating point number to integer and vice versa FPGA VHDL 5 01-08-2008 06:29 AM
Safe conversion from floating real to integer (and vice versa) rz0 C Programming 4 02-01-2006 05:27 PM
Howto display an ASP.net datagrid rows as columns and vice versa? Jimmy ASP .Net 1 06-14-2005 03:30 PM
CString to char array convertion (vice versa) Tim Wong C++ 5 01-21-2005 05:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57