By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,905 Members | 1,283 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,905 IT Pros & Developers. It's quick & easy.

POINTER EQUALS AN ARRAY

P: n/a
The program below creates a char pointer call charPtr...... i then
declare an char array string[10] "Good Luck"

When i assign charPtr = string, I expect an error. However, It
actually runs and outputs:

G
Good Luck
Can someone explain to me how charPtr = string actually works, but if I
create
char string = 'x';
wouldn't work.

#include<math.h>
#include<iostream>

using namespace std;
void main()
{
int value;
value = 500;
char* charPtr;
char string[10] = "Good Luck";
charPtr = string;
cout<< *charPtr<<endl;
cout<<charPtr<<endl;

}

Nov 3 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"chessc4c6" <pr***********@aol.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
The program below creates a char pointer call charPtr...... i then
declare an char array string[10] "Good Luck"

When i assign charPtr = string, I expect an error.
Why?
However, It
actually runs and outputs:

G
Good Luck
Can someone explain to me how charPtr = string actually works, but if I
create
char string = 'x';
wouldn't work.
In almost every context,, the name of an array is
automatically converted to a pointer to its first
element.

char string = 'x'; does not denote an array.

#include<math.h>
#include<iostream>

using namespace std;
void main()
int main()
{
int value;
value = 500;
IMO better is:

int value = 500;
char* charPtr;
char string[10] = "Good Luck";
charPtr = string;
assigns to 'charPtr' the address of 'string[0]'
cout<< *charPtr<<endl;
prints G (dereferences what 'charPtr' points to, a type 'char' object)
cout<<charPtr<<endl;
prints Good Luck because the << operator taking type 'char*'
argument is defined to print the 'C-style' string it points to.
Also note that because of the automatic conversion, the same
output results from:

cout << string << endl;

}


Do not be misled into believing that an array and a pointer
are the same thing. They are most certainly not.

What you are seeing is an automatic conversion.

Anyway, in C++ the preferred way of dealing with strings
is with the 'std::string' type, not arrays of characters.
Also, pointers are much less needed in C++ than in C.

-Mike
Nov 4 '05 #2

P: n/a
chessc4c6 wrote:
The program below creates a char pointer call charPtr...... i then
declare an char array string[10] "Good Luck"

When i assign charPtr = string, I expect an error. However, It
actually runs and outputs: #include<math.h>
You don't use this header.
#include<iostream>

using namespace std;
void main()
main() returns it.
{
int value;
value = 500;
char* charPtr;
char string[10] = "Good Luck";
charPtr = string;
cout<< *charPtr<<endl;
cout<<charPtr<<endl;

}


In most cases, the name of an array is converted to a pointer to the
first element. Assignment is one such case, the name "string" (don't
use that as it is the same as the standard string class) is converted
to a pointer to the first element, making it a char pointer. Naturally,
it's ok to then assign it to a char pointer.

What book are you using that doesn't explain this?

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Nov 4 '05 #3

P: n/a
chessc4c6 wrote:
The program below creates a char pointer call charPtr...... i then
declare an char array string[10] "Good Luck"

When i assign charPtr = string, I expect an error.
Why? Consider the following:

int a = 0;
double b;

b = a;

'b' and 'a' have different types. But do you expect an error when you assign 'a'
to 'b'? I hope you don't. You probably understand that when you assign an 'int'
value to a 'double' object the former gets implicitly converted to the type of
the latter.

In general case, when you assign a value of type 'T' to an object of different
type 'U', it is either 1) an error or 2) a perfectly correct piece of code that
uses an implicit conversion. The implicit conversion is automatically introduced
by the compiler. The specification of C++ language contains a list of all such
conversions, which formally referred to as 'standard' conversions.

One of the standard conversion is so called "array-to-pointer" conversion.
Applied to our example, it says that if you have a value of array type (say
'T[N]') as right-hand side of the assignment and an object of corresponding
pointer type (would be 'T*') as left-hand side of the assignment, an implicit
array-to-pointer conversion is performed and the pointer received the address of
the very first element of the array. That's exactly what happens in your case.
Can someone explain to me how charPtr = string actually works, but if I
create
char string = 'x';
wouldn't work.


Because the original array was... well, an array. And it was implicitly
converted to pointer type by the aforementioned standard array-to-pointer
conversion.

Now you have an object of type 'char' as right-hand side of the assignment.
There no implicit (or even explicit) conversion in C++ that can convert a value
of type 'char' to pointer type. Hence the error.

--
Best regards,
Andrey Tarasevich
Nov 4 '05 #4

P: n/a
Default User <de***********@yahoo.com> wrote:
void main()
main() returns it.


What is the program's termination status? I'll take "Typos" for 1000,
Alex. :-)

(For OP: main() returns int.)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 4 '05 #5

P: n/a
On 3 Nov 2005 15:45:17 -0800, "chessc4c6" <pr***********@aol.com> wrote:
Can someone explain to me how charPtr = string actually works
The C++ language defines an automatic array-to-pointer conversion (see section
4.2).

char string = 'x';
wouldn't work.


Actually, that should work just fine.

-dr
Nov 5 '05 #6

P: n/a
>
char string = 'x';
wouldn't work.

Actually, that should work just fine.


Have you tried it?

john
Nov 5 '05 #7

P: n/a
On Sat, 05 Nov 2005 07:20:44 GMT, John Harrison <jo*************@hotmail.com>
wrote:
char string = 'x';
wouldn't work.

Actually, that should work just fine.


Have you tried it?


Yes. Here's a complete program that compiles:

int main()
{
char string = 'x';
return 0;
}
Nov 6 '05 #8

P: n/a

"Dave Rahardja" <as*@me.com> wrote in message
news:d1********************************@4ax.com...
On Sat, 05 Nov 2005 07:20:44 GMT, John Harrison
<jo*************@hotmail.com>
wrote:

char string = 'x';
wouldn't work.
Actually, that should work just fine.


Have you tried it?


Yes. Here's a complete program that compiles:

int main()
{
char string = 'x';
return 0;
}


That's out of context of the original question.

-Mike
Nov 6 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.