473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

1st element of "an array of CHAR strings"

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 ?

Apr 1 '07 #1
21 2708
* 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?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 1 '07 #2
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
Apr 1 '07 #3
On Apr 1, 5:34 pm, "Alf P. Steinbach" <a...@start.nowrote:
* arnuld:
[SNIP]
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?
well, it says:

can not convert a "pointer to a pointer to const char" to "pointer to
char"

Apr 1 '07 #4
On Apr 1, 5:37 pm, "Bo Persson" <b...@gmb.dkwrote:
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


Apr 1 '07 #5
"arnuld" <ge*********@gmail.comwrote in message
news:11*********************@o5g2000hsb.googlegrou ps.com...
>On Apr 1, 5:37 pm, "Bo Persson" <b...@gmb.dkwrote:
>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;
Apr 1 '07 #6
On Apr 1, 6:30 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:

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 ?

Apr 1 '07 #7
arnuld wrote:
>On Apr 1, 6:30 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:

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

Apr 1 '07 #8
"arnuld" <ge*********@gmail.comwrote:
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
}
Apr 1 '07 #9
On Apr 2, 1:30 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"arnuld" <geek.arn...@gmail.comwrote in message
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'.

Apr 2 '07 #10
"Old Wolf" <ol*****@inspire.net.nzwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
On Apr 2, 1:30 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"arnuld" <geek.arn...@gmail.comwrote in message
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.
>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'.
Yes. And incremeting arr (or arr[1]) would make it point to the 2nd element
of arr.
Apr 2 '07 #11
On Apr 1, 11:06 pm, "Daniel T." <danie...@earthlink.netwrote:
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 ?

Apr 2 '07 #12
In article <11**********************@n76g2000hsh.googlegroups .com>,
"arnuld" <ge*********@gmail.comwrote:
On Apr 1, 11:06 pm, "Daniel T." <danie...@earthlink.netwrote:
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.
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.
Apr 2 '07 #13
On Apr 1, 8:25 am, "arnuld" <geek.arn...@gmail.comwrote:
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.

Apr 2 '07 #14
On Apr 2, 5:40 pm, "Daniel T." <danie...@earthlink.netwrote:
"arnuld" <geek.arn...@gmail.comwrote:
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)
BTW, this must not fail then:
const char** = &arr;
Right.

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 ?

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 ?

Apr 2 '07 #15
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
Apr 2 '07 #16
arnuld wrote:
>On Apr 1, 11:06 pm, "Daniel T." <danie...@earthlink.netwrote:
>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.
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 )

Apr 2 '07 #17
"arnuld" <ge*********@gmail.comwrote:
BTW, this must not fail then:
const char** = &arr;
Right.


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

:-)
Apr 2 '07 #18
On Apr 2, 11:55 pm, "Daniel T." <danie...@earthlink.netwrote:
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 ?

Apr 3 '07 #19
"arnuld" <ge*********@gmail.comwrote:
On Apr 2, 11:55 pm, "Daniel T." <danie...@earthlink.netwrote:
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.
Apr 3 '07 #20
arnuld wrote:
>On Apr 2, 11:55 pm, "Daniel T." <danie...@earthlink.netwrote:
>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'.
Apr 3 '07 #21
On Apr 3, 6:55 am, "Daniel T." <danie...@earthlink.netwrote:
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';

Apr 3 '07 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Martin Robins | last post by:
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being...
4
by: Rob Smeets | last post by:
Hi all, I have the following problem: I have to revise a c++ dll. And i'm new to c++. I have to change a function, but i cannot change it's structure. I want to check the parameters and act...
26
by: Chris Potter | last post by:
Hello everyone. I am taking my first course in C and in one of my assignments i need to print out an array that could have anywhere from 0 to 100 positive integers in it (a negative integer is...
13
by: baumann.Pan | last post by:
when define char *p = " can not modify"; p ='b' ;is not allowed, but if you declare p as char p = "can modify"; p = 'b'; is ok? why?
4
by: John Devereux | last post by:
Hi, I would like some advice on whether I should be using plain "chars" for strings. I have instead been using "unsigned char" in my code (for embedded systems). In general the strings contain...
33
by: Frederick Gotham | last post by:
(My dialect of English seems to puzzle people at times, so I'll first clarify a few terms which I use in the following post:) (1) By "domestic", I mean "ordinary, run-of-the-mill, not...
9
by: Frederick Gotham | last post by:
What are your thoughts on the following code? typedef int Coords; int main() { Coords *const p = new Coords; delete p; }
4
moishy
by: moishy | last post by:
I have a function which adds strings to an array, but outside the function it didn't add the string to the array. Example: function AddStringToArray($name,$string) { $theArray =...
34
by: arnuld | last post by:
what is the difference between these 2: char name = "hackers"; char* name = "hackers";
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.