array question

Hi All
if i have the folowing code:
char array[7];
what contains (&array) ?
Thanks
[118 byte] By [Briana] at [2007-11-18 19:27:17]
# 1 Re: array question
I believe it's the address of the first element in the array.
Paul Rice at 2007-11-9 0:32:37 >
# 2 Re: array question
the name of the array is the first item address
Briana at 2007-11-9 0:33:34 >
# 3 Re: array question
Have you actually tried the code?

char array[] = {'a','b','c','d','e','f','g','h','i','\0'};
cout << "string:\t" << array << endl;
cout << "address:" << (&array) << endl;

They don't display the same thing.
Paul Rice at 2007-11-9 0:34:32 >
# 4 Re: array question
Paul Rice,
your check is incorrect. using a char* type with cout displays
the string itself. check the following and see the same addresses:

char array[] = {'a','b','c','d','e','f','g','h','i','\0'};
cout << "string:\t" << (void*) array << endl;
cout << "address:" << (&array) << endl;
Guysl at 2007-11-9 0:35:34 >
# 5 Re: array question
I thought I was clear about what the values would be when I labeled them "string:" and "address:". Sorry about the confusion.
Paul Rice at 2007-11-9 0:36:43 >
# 6 Re: array question
Paul Rice,
Sorry too about the confusion.
bottom line is array and &array are the address of the first element.

Regards,
Guy
Guysl at 2007-11-9 0:37:42 >
# 7 Re: array question
Hi

What type is &arr ? char**?!
assigning such thing result with a compiler eror.
Briana at 2007-11-9 0:38:36 >
# 8 Re: array question
'array' is really a constant (address to first element). You can't "take" an address of a constant, thus &array is wrong!
j0nas at 2007-11-9 0:39:39 >
# 9 Re: array question
j0nas,
I agree. though both array and &array are the same address,
they are different types, and as you said &array is wrong.

Regards,
Guy
Guysl at 2007-11-9 0:40:40 >
# 10 Re: array question
Originally posted by j0nas
'array' is really a constant (address to first element). You can't "take" an address of a constant, thus &array is wrong!

No, array is really the whole array. If 'array' were just an address, then 'sizeof(array)' would evaluate to the size of an address (or the size of a pointer). Instead, 'sizeof(array)' evaluates to the size of the entire memory of the array.

And why is '&array' wrong? I believe this is acceptable C (and C++) syntax to take the address of an array (I wish I had a copy of K&R around). It just returns the address of the first element (which makes sense if you think about memory layout). The fact that you can take the address of an array makes writing generic macros and templates much easier.

- Kevin
KevinHall at 2007-11-9 0:41:41 >
# 11 Re: array question
this thread shows why the following is good practice.....

&array[0]

there's no confusion about what you want here. but say you have something like

char *array = "1234";

and you now did &array. you wont get the address of the first element but rather you'll get the location of 'array' itself in memory. a pointer still has to exist somewhere.

so if you want the address of the actual text it's always best to just use &array[0] as an act of habit. pointer or not.

kind of an aside but something i felt like posting.
filthy_mcnasty at 2007-11-9 0:42:48 >
# 12 Re: array question
I do not have a copy of K&R around, but I'm holding
"A Book On C(Al Kelley,Ira Pohl) / Addison Wesley",
page 254-5:

say we have -
int a[N],*p;

"...Note that because a is a constant pointer, expressions such as
a=p
++a
a+=2
&a
are illegal. We cannot change the value of a."

illegal or not, I checked with .c extention and it is compiled.
back to array and &array, they are different types. the following
overload demonstrate it:

#include <iostream>

void test1(char (*)[10]);
void test2(char *);

int main()
{
char str[10]="12345";
test1(&str);
test2(str);

return 0;
}

void test1(char (*p)[10])
{
cout << "in test1" << endl;
cout << "*p=" << *p << " sizeof(*p)=" << sizeof(*p) << endl;

}

void test2(char* p)
{
cout << "in test2" << endl;
cout << "*p=" << *p << " sizeof(*p)=" << sizeof(*p) << endl;
}
Guysl at 2007-11-9 0:43:43 >
# 13 Re: array question
I agree that they are different types. I agree that '&array' is constant, and thus certain operations cannot be used on it. I agree that '&array' should not normally be used -- and when it is used, it should be used with care. I still believe however that '&array' is legal. The type of '&array' would be 'type (*)[array-size]'.

Check out this page (http://publications.gbdirect.co.uk/c_book/chapter5/arrays_and_address_of.html) and this thread (http://www.dev-archive.com/forum/showthread.php?s=&threadid=292722). Thus the code below should be valid C:

int main()
{
int array[10] = {0,1,2,3,4,5,6,7,8,9};
int (*array_ptr)[10] = &array;

return 0;
}

- Kevin
KevinHall at 2007-11-9 0:44:48 >
# 14 Re: array question
Isn't it amazing how the simplest things can generate such interest.
:)

Anyways, I would see int array[10]; as a C++ object.
The type of this object is int[10], and int[10] has an operator int* built into it.
That is to say, it will implicitly convert from an int[10] to an int*.
Also, &array is a pointer to an int[10] object, and this pointer will also implicitly cast to an int*.
Zaccheus at 2007-11-9 0:45:51 >
# 15 Re: array question
The rules are not really different for C and C++ concerning this issue including implicit casting rules.

Originally posted by Zaccheus
Also, &array is a pointer to an int[10] object, and this pointer will also implicitly cast to an int*.

This actually is incorrect. This code will fail to compile (try it):

int main()
{
int array[10] = {0,1,2,3,4,5,6,7,8,9};
int (*array_ptr)[10] = &array; /* no error here */

int *faulty_ptr = &array; /* compile time error */

return 0;
}

- Kevin
KevinHall at 2007-11-9 0:46:51 >
# 16 Re: array question
Ooops.

You are absolutely correct:
VC2003:
error C2440: 'initializing' :
cannot convert from 'int (*)[10]' to 'int *'
Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast

Curious.
Zaccheus at 2007-11-9 0:47:44 >