1st element of "an array of CHAR strings"

A

arnuld

int main()
{
const char* arr[] = {"bjarne", "stroustrup", "c++"};
char* parr = &arr;
}

this gives an error:
[arch@voodo tc++pl]$ g++ test.cpp
test.cpp: In function 'int main()':
test.cpp:4: error: cannot convert 'const char* (*)[3]' to 'char*' in
initialization
[arch@voodo tc++pl]$


what is wrong here ?
 
A

Alf P. Steinbach

* arnuld:
int main()
{
const char* arr[] = {"bjarne", "stroustrup", "c++"};
char* parr = &arr;
}

this gives an error:
[arch@voodo tc++pl]$ g++ test.cpp
test.cpp: In function 'int main()':
test.cpp:4: error: cannot convert 'const char* (*)[3]' to 'char*' in
initialization
[arch@voodo tc++pl]$


what is wrong here ?

What does the compiler say?
 
B

Bo Persson

arnuld wrote:
:: int main()
:: {
:: const char* arr[] = {"bjarne", "stroustrup", "c++"};
:: char* parr = &arr;
:: }
::
:: this gives an error:
:: [arch@voodo tc++pl]$ g++ test.cpp
:: test.cpp: In function 'int main()':
:: test.cpp:4: error: cannot convert 'const char* (*)[3]' to 'char*' in
:: initialization
:: [arch@voodo tc++pl]$
::
::
:: what is wrong here ?

That you try to assign the address of an array of pointers to const char to
a pointer to non-const char. :)

There are several levels of errors here.



Bo Persson
 
A

arnuld

That you try to assign the address of an array of pointers to const char to
a pointer to non-const char. :)

There are several levels of errors here.

ok here is my:

"try #1 to assign the address of an array of pointers to const char to
a pointer to const char"

const char* arr[] = {"bjarne", "stroustrup", "c++"};
const char* parr = &arr;



"try #2 to assign the address of an array of pointers to const char to
a pointer to const char"

const char* arr[] = {"bjarne", "stroustrup", "c++"};
char *const parr = &arr;



ERROR for try #1:

[arch@voodo tc++pl]$ g++ test.cpp
test.cpp: In function 'int main()':
test.cpp:4: error: cannot convert 'const char* (*)[3]' to 'const
char*' in initialization



ERROR for try #2:

[arch@voodo tc++pl]$ g++ test.cpp
test.cpp: In function 'int main()':
test.cpp:4: error: cannot convert 'const char* (*)[3]' to 'char*
const' in initialization
 
J

Jim Langston

arnuld said:
On Apr 1, 5:37 pm, "Bo Persson" <[email protected]> wrote:
That you try to assign the address of an array of pointers to const char
to
a pointer to non-const char. :)

There are several levels of errors here.

ok here is my:

"try #1 to assign the address of an array of pointers to const char to
a pointer to const char"

const char* arr[] = {"bjarne", "stroustrup", "c++"};
const char* parr = &arr;



"try #2 to assign the address of an array of pointers to const char to
a pointer to const char"

const char* arr[] = {"bjarne", "stroustrup", "c++"};
char *const parr = &arr;



ERROR for try #1:

[arch@voodo tc++pl]$ g++ test.cpp
test.cpp: In function 'int main()':
test.cpp:4: error: cannot convert 'const char* (*)[3]' to 'const
char*' in initialization



ERROR for try #2:

[arch@voodo tc++pl]$ g++ test.cpp
test.cpp: In function 'int main()':
test.cpp:4: error: cannot convert 'const char* (*)[3]' to 'char*
const' in initialization

What are you trying to accomplish? a char* will point to one character. A
char* [] points to pointers. They point to different things.

If you want your char* to point to the first element in the array, then do
that.
char* const parr = arr[0];

arr[0] also contains a char pointer, so this works.

Do you want parr to point to the array of char pointers? Then you need a
pointer to a char pointer.

const char** parr = arr;
 
A

arnuld

What are you trying to accomplish? a char* will point to one character. A
char* [] points to pointers. They point to different things.

If you want your char* to point to the first element in the array, then do
that.
char* const parr = arr[0];

arr[0] also contains a char pointer, so this works.

Do you want parr to point to the array of char pointers? Then you need a
pointer to a char pointer.

const char** parr = arr;

i don't understand one thing, if i do this:

const char** parr = &arr; // notice the "&"

"&arr" raises an error, where as "arr" does not. why ?
 
R

Rolf Magnus

arnuld said:
What are you trying to accomplish? a char* will point to one character.
A
char* [] points to pointers. They point to different things.

If you want your char* to point to the first element in the array, then
do that.
char* const parr = arr[0];

arr[0] also contains a char pointer, so this works.

Do you want parr to point to the array of char pointers? Then you need a
pointer to a char pointer.

const char** parr = arr;

i don't understand one thing, if i do this:

const char** parr = &arr; // notice the "&"

"&arr" raises an error, where as "arr" does not. why ?

Because you have a pointer to non-array, and you can't assign the address of
an array to it. You could do:

const char* (*parr)[3] = &arr;

now parr is a pointer to an array[3] of pointers to const char.
 
D

Daniel T.

arnuld said:
int main()
{
const char* arr[] = {"bjarne", "stroustrup", "c++"};
char* parr = &arr;
}

this gives an error:
[arch@voodo tc++pl]$ g++ test.cpp
test.cpp: In function 'int main()':
test.cpp:4: error: cannot convert 'const char* (*)[3]' to 'char*' in
initialization
[arch@voodo tc++pl]$


what is wrong here ?

Some examples:

void foo( const char* arr )
{
const char* a = arr; // works 'arr' and 'a' are both the same type.
char* b = arr; // fails, 'arr' is const and 'b' is not
const char* c = &arr; // fails, '&arr' (the address of 'arr') is a
// pointer to a 'const char*' (i.e., a 'const
// char**') whereas 'c' is a 'const char*'
}

As you can see, in order to do an assignment, the two types must be the
same. There are exceptions, but this is the general rule. One of the
exceptions is that you can convert an array into a pointer. So:

void foo( const char arr[] )
{
const char* a = arr; // works
}

void bar( const char* arr[] )
{
const char** b = arr; // works
}
 
O

Old Wolf

arnuld said:
const char* arr[] = {"bjarne", "stroustrup", "c++"};
const char* parr = &arr;

If you want your char* to point to the first element in the array,
then do that.
char* const parr = arr[0];

Firstly, this is an error because arr[0] is pointer to const char,
but 'parr' points to non-const char.

Supposing you fix that, then 'parr' does not point to the first
element in the array. In fact it points to the same string literal
as the first element in the array points to.
Do you want parr to point to the array of char pointers? Then you need a
pointer to a char pointer.

const char** parr = arr;

This code makes 'parr' point to the first element of 'arr'.
 
J

Jim Langston

Old Wolf said:
arnuld said:
const char* arr[] = {"bjarne", "stroustrup", "c++"};
const char* parr = &arr;

If you want your char* to point to the first element in the array,
then do that.
char* const parr = arr[0];

Firstly, this is an error because arr[0] is pointer to const char,
but 'parr' points to non-const char.

Supposing you fix that, then 'parr' does not point to the first
element in the array. In fact it points to the same string literal
as the first element in the array points to.

Arg. You're right. I actually put the code into my compiler and tested to
make sure that
const char** parr = arr;
would work, but didnt' think to check the other case. My bad.
This code makes 'parr' point to the first element of 'arr'.

Yes. And incremeting arr (or arr[1]) would make it point to the 2nd element
of arr.
 
A

arnuld

Some examples:

void foo( const char* arr )
{
const char* a = arr; // works 'arr' and 'a' are both the same type.
char* b = arr; // fails, 'arr' is const and 'b' is not
const char* c = &arr; // fails, '&arr' (the address of 'arr') is a
// pointer to a 'const char*' (i.e., a 'const
// char**') whereas 'c' is a 'const char*'

}


i understood the 1st 2 but i did not get the 3rd example. can you
explain a little bit more.

const char* c = &arr;

// fails, '&arr' (the address of 'arr') is a
// pointer to a 'const char*' (i.e., a 'const
// char**') whereas 'c' is a 'const char*'

it fails, i have tried it but what does this mean:

"&arr", the address of "arr" is a pointer to a "const char*". i think
the address of array is simply a the address of 1st element, which is
a memory location and i can point a pointer to it.


BTW, this must not fail then:

const char** = &arr;

As you can see, in order to do an assignment, the two types must be the
same. There are exceptions, but this is the general rule. One of the
exceptions is that you can convert an array into a pointer. So:
void foo( const char arr[] )
{
const char* a = arr; // works

}

void bar( const char* arr[] )
{
const char** b = arr; // works

}

OK, here the "pointer" must be one step ahead, .i.e if i have an
array, ic an use a single "*" if the array has a single "*" then i
need to use 2 "*".

right ?
 
D

Daniel T.

On Apr 1, 11:06 pm, "Daniel T." <[email protected]> wrote:
Some examples:

void foo( const char* arr )
{
const char* a = arr; // works 'arr' and 'a' are both the same type.
char* b = arr; // fails, 'arr' is const and 'b' is not
const char* c = &arr; // fails, '&arr' (the address of 'arr') is a
// pointer to a 'const char*' (i.e., a 'const
// char**') whereas 'c' is a 'const char*'

}


i understood the 1st 2 but i did not get the 3rd example. can you
explain a little bit more.

const char* c = &arr;

// fails, '&arr' (the address of 'arr') is a
// pointer to a 'const char*' (i.e., a 'const
// char**') whereas 'c' is a 'const char*'

it fails, i have tried it but what does this mean:

"&arr", the address of "arr" is a pointer to a "const char*". i think
the address of array is simply a the address of 1st element, which is
a memory location and i can point a pointer to it.[/QUOTE]

Right. So if you have "const char* arr[]" then "arr" is the address of
the array and "&arr" is the address of the address of the array.
BTW, this must not fail then:

const char** = &arr;
Right.
As you can see, in order to do an assignment, the two types must be the
same. There are exceptions, but this is the general rule. One of the
exceptions is that you can convert an array into a pointer. So:
void foo( const char arr[] )
{
const char* a = arr; // works

}

void bar( const char* arr[] )
{
const char** b = arr; // works

}

OK, here the "pointer" must be one step ahead, .i.e if i have an
array, ic an use a single "*" if the array has a single "*" then i
need to use 2 "*".

right ?

Right.
 
T

The Prog

int main()
{
const char* arr[] = {"bjarne", "stroustrup", "c++"};
char* parr = &arr;

}

this gives an error:
[arch@voodo tc++pl]$ g++ test.cpp
test.cpp: In function 'int main()':
test.cpp:4: error: cannot convert 'const char* (*)[3]' to 'char*' in
initialization
[arch@voodo tc++pl]$

what is wrong here ?

hey man,

first of all u r useing a single char pointer to point to an array of
char arrays.
u shd use a double pointer of chars.
 
A

arnuld

const char* c = &arr;
// fails, '&arr' (the address of 'arr') is a
// pointer to a 'const char*' (i.e., a 'const
// char**') whereas 'c' is a 'const char*'
it fails, i have tried it but what does this mean:
"&arr", the address of "arr" is a pointer to a "const char*". i think
the address of array is simply a the address of 1st element, which is
a memory location and i can point a pointer to it.

Right. So if you have "const char* arr[]" then "arr" is the address of
the array and "&arr" is the address of the address of the array.

NO, i am not thinking this way i am saying this:

arr -----> &arr[0]
(inplicit conversion)


i did not understand the expression i wrote. i just deduced that from
your 3 examples. i dont understand the differences between these:

arr &arr &arr[0]

As you can see, in order to do an assignment, the two types must be the
same. There are exceptions, but this is the general rule. One of the
exceptions is that you can convert an array into a pointer. So:
void foo( const char arr[] )
{
const char* a = arr; // works
}
void bar( const char* arr[] )
{
const char** b = arr; // works
}
OK, here the "pointer" must be one step ahead, .i.e if i have an
array, ic an use a single "*" if the array has a single "*" then i
need to use 2 "*".

Right.

i get that, no problem. i always think that when one deals with an
"array", one can not avoid "pointer to first element". "array" can be
accessed ONLY and ONLY by "pointer" to 1st element, implicitly (a.k.a
indexing) or explicitly (a.k.a "arr" or "&arr[0]', no 3rd expression
and this is where i get confused when i see "&arr". what is that ? )

RIGHT ?
 
D

Default User

The Prog wrote:

u shd use a double pointer of chars.


And you should use standard English. "u" and "shd" are not words. It
makes your post very difficult to read.




Brian
 
R

Rolf Magnus

arnuld said:
i understood the 1st 2 but i did not get the 3rd example. can you
explain a little bit more.

const char* c = &arr;

// fails, '&arr' (the address of 'arr') is a
// pointer to a 'const char*' (i.e., a 'const
// char**') whereas 'c' is a 'const char*'

it fails, i have tried it but what does this mean:

"&arr", the address of "arr" is a pointer to a "const char*". i think
the address of array is simply a the address of 1st element, which is
a memory location and i can point a pointer to it.

Well, they point to the same memory location, but they are of different
type.
BTW, this must not fail then:

const char** = &arr;

arr is not a pointer to const char. It's an array of pointers to const char.
That's a different type. The following would work:

const char** p = &arr[0]; // address of the first element of your array
As you can see, in order to do an assignment, the two types must be the
same. There are exceptions, but this is the general rule. One of the
exceptions is that you can convert an array into a pointer. So:
void foo( const char arr[] )
{
const char* a = arr; // works

}

void bar( const char* arr[] )
{
const char** b = arr; // works

}

OK, here the "pointer" must be one step ahead, .i.e if i have an
array, ic an use a single "*" if the array has a single "*" then i
need to use 2 "*".

right ?

Actually, the above example isn't a very good one, because no arrays are
involved. In a function argument, the [] doesn't mean 'array', but
rather 'pointer' (a strange 'feature' inherited from C, which was supposed
to make things easier, but in fact just makes them more complicated), so in
fact

void foo( const char arr[] )

is 100% equivalent to

void foo( const char* arr )
 
D

Daniel T.

arnuld said:


i did not understand the expression i wrote. i just deduced that from
your 3 examples. i dont understand the differences between these:

arr &arr &arr[0]

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

:)
 
A

arnuld

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, because elements, e.g. /Daniel/ itself is a
character array. so

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

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

right ?
 
D

Daniel T.

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, because elements, e.g. /Daniel/ itself is a
character array. so

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

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

Just the opposite. Putting the '&' increases the pointer level, the '*'
decreases it.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top