Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > POINTER EQUALS AN ARRAY

Reply
Thread Tools

POINTER EQUALS AN ARRAY

 
 
chessc4c6
Guest
Posts: n/a
 
      11-03-2005
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;

}

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      11-04-2005
"chessc4c6" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      11-04-2005
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.
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-04-2005
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
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      11-04-2005
Default User <(E-Mail Removed)> 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.
 
Reply With Quote
 
Dave Rahardja
Guest
Posts: n/a
 
      11-05-2005
On 3 Nov 2005 15:45:17 -0800, "chessc4c6" <(E-Mail Removed)> 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
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      11-05-2005
>
>>char string = 'x';
>>wouldn't work.

>
>
> Actually, that should work just fine.
>


Have you tried it?

john
 
Reply With Quote
 
Dave Rahardja
Guest
Posts: n/a
 
      11-06-2005
On Sat, 05 Nov 2005 07:20:44 GMT, John Harrison <(E-Mail Removed)>
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;
}
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      11-06-2005

"Dave Rahardja" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Sat, 05 Nov 2005 07:20:44 GMT, John Harrison
> <(E-Mail Removed)>
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Why the address of array equals array. =?big5?B?r0W84Q==?= C Programming 8 03-15-2007 04:03 AM
Pointer to class equals pointer to base class Alberto Luaces C++ 6 06-19-2006 06:16 PM
Array of pointer Vs Pointer to Array sangeetha C Programming 9 10-09-2004 07:01 PM
does String.equals breaks the equals contract? Edward A Thompson Java 4 02-11-2004 11:33 PM



Advertisments