1st element of "an array of CHAR strings"

R

Rolf Magnus

arnuld said:
On Apr 2, 11:55 pm, "Daniel T." <[email protected]> wrote:
Given 'arr' is declared as "const char* arr[]" then:

arr and &arr[0] are effectively the same thing.

&arr is the address of arr.

So. arr[0] is a const char*, arr and &arr[0] is the address of the const
char*, and &arr is the address of the address of a const char*.

Now check this out:

arr[0][0] is a const char...

:)

i got it, say:

const char* arr[] = { "Daniel", "Tartaglia" }

it is an array of ararys,

No, it's not. It's an array of pointers.
because elements, e.g. /Daniel/ itself is a character array.

Yes, that's right. Still, arr is an array of _pointers_. The first element
(arr[0]) points to the first character of the character array "Daniel", the
second element of arr points to the first character of "Tartaglia".
so

1.) /arr/ and /&arr[0]/ give the address of 1st element of arr[]
Yes.

2.) &arr, will give the address of "D" of /Daniel/ (array inside an
array)

No, it won't. &arr gives the address of the array. *arr would give you the
address of the 'D'.
 
O

Old Wolf

Given 'arr' is declared as "const char* arr[]" then:

arr and &arr[0] are effectively the same thing.

Only if 'arr' is used in certain contexts...
&arr is the address of arr.

....not including this one :) &arr is different to &(&arr[0])
In fact the latter is an error because &arr[0] is not an lvalue.
So. arr[0] is a const char*, arr and &arr[0] is the address of the const
char*, and &arr is the address of the address of a const char*.

No, &arr is the address of an array of const char* .
Have a read of http://c-faq.com/aryptr/ .
arr[0][0] is a const char...

To clarify, the char need not be const; it is only the lvalue
expression arr[0][0] that designates it const. For example, this
is legal code:

char ch;
const char *arr[] = { &ch };

const_cast<char &>(arr[0][0]) = 'x';
 

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

No members online now.

Forum statistics

Threads
474,301
Messages
2,571,549
Members
48,295
Latest member
JayKillian
Top