POINTER EQUALS AN ARRAY

C

chessc4c6

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;

}
 
M

Mike Wahler

chessc4c6 said:
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
 
D

Default User

chessc4c6 said:
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
 
A

Andrey Tarasevich

chessc4c6 said:
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.
 
D

Dave Rahardja

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
 
M

Mike Wahler

Dave Rahardja said:
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
 

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top