assigning array addresses to integer variables and vice versa

A

anonymous

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

pemo

anonymous said:
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;
 
K

Krishanu Debnath

anonymous said:
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
 
A

anonymous

pemo said:
anonymous said:
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.
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 :)
Because an array name decomposes into the address of its first element, so
it's like saying 42++;




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

Thanks for your help.
 
A

Anand

anonymous said:
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)
 
S

Suman

pemo said:
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.
 
A

anonymous

Anand said:
anonymous said:
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)

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

Thanks in advance!
 
F

Flash Gordon

Suman said:
pemo said:
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.
 
F

Flash Gordon

Krishanu said:
anonymous said:
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;
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>
 
F

Flash Gordon

anonymous said:
Anand said:
anonymous said:
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.
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.
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).
 
E

Eric Sosman

anonymous said:
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;

You can, if you like. The result will be useful on
some machines, useless on others. I wouldn't recommend it.
whereas I found it perfectly alright to do the following:

address = (int)&array;

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

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

http://www.eskimo.com/~scs/C-faq/top.html
Question 3:

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

FAQ Question 6.7.
Question 4:
How can I do the following:

array = address;

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

No.
Thanks in advance for all Your replies.

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

Eric Sosman

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

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

Jordan Abel

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

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

[because they point to the same memory location]
 
K

Keith Thompson

Anand said:
anonymous said:
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

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

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

Jordan Abel

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 ]

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.

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 *).]
 
R

Randy Howard

anonymous wrote
(in article
I have couple of questions related to array addresses.

[snip]

What you are trying to do is going to cause you a lot of grief
in the long run. Do not make assumptions about the size of a
pointer being equivalent to the size of some other data type,
unless you are 100% sure that your code does not need to be
portable off-platform, including moving from 32-bit to 64-bit
environments. If you wish to pursue this, it's not appropriate
for comp.lang.c, which discusses standard C and portable
programming within that scope.
 
B

Barry Schwarz

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:

Others have answered
Question 2:
Ditto

Question 3:

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

array is not a pointer at all. To prove this to yourself, print the
sizeof array as well as the sizeof several different types of
pointers. sizeof array must be 35 while sizeof(T*) for various types T
will usually be 4 or 8, depending on your system.

It is true that in certain circumstances an unsubscripted array name
in an expression will **evaluate to** the address of the first element
of the array with type pointer to element type but:

It does not create a pointer object in the same sense that a
definition such as "char *ptr;" does.

The resulting value is not a modifiable lvalue and cannot be the
operand of prefix or postfix ++.
Question 4:
How can I do the following:

array = address;

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

Not at all. And why would you ever want to? If you want to change
the contents of an entire array, you can use memcpy. If you want the
same code to process different arrays, you can use pointers.


<<Remove the del for email>>
 
E

Eric Sosman

Jordan said:
Krishanu Debnath wrote:



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


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

[because they point to the same memory location]

True, but not particularly relevant. 0 == 0LL, but
that doesn't mean that `int' and `long long' are the same.
 
B

Barry Schwarz

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:

Others have answered
Question 2:
Ditto

Question 3:

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

array is not a pointer at all. To prove this to yourself, print the
sizeof array as well as the sizeof several different types of
pointers. sizeof array must be 35 while sizeof(T*) for various types T
will usually be 4 or 8, depending on your system.

It is true that in certain circumstances an unsubscripted array name
in an expression will **evaluate to** the address of the first element
of the array with type pointer to element type but:

It does not create a pointer object in the same sense that a
definition such as "char *ptr;" does.

The resulting value is not a modifiable lvalue and cannot be the
operand of prefix or postfix ++.
Question 4:
How can I do the following:

array = address;

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

Not at all. And why would you ever want to? If you want to change
the contents of an entire array, you can use memcpy. If you want the
same code to process different arrays, you can use pointers.


<<Remove the del for email>>
 
M

Mark McIntyre

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;

Because its nonsensical. How can you convert an array to an integer?
whereas I found it perfectly alright to do the following:

address = (int)&array;

This is equally nonsensical. Your compiler won't complain though,
because this time the conversion is allowed - you're converting a
pointer to an array into an int. Its just a nonsensical conversion.
Question 2:
What is the difference between array and &array.

Array is the array. &array is the address of the start of array.
Are not they the same
thing i.e. starting address of the array.

They have different types, even though they may refer to the same
memory location.
Question 3:

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

An array is an array. Its NOT a pointer.
Question 4:
How can I do the following:

array = address;

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

No. An array is an array. if you want a pointer, use a pointer.

And please read the FAQ, there's a big section about pointers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top