reading a char array

O

omidsoltan

Hello, I am new to c++ and have a very simple question.

Why when reading a char array the whole array is displayed for
example

char st[] = " hello " ;
cout << st << endl ;

will then display the whole array.

but if you use an int array e.g

int num [] = {1,2,3};
cout << num ;

will display the address of the first number;

the second example makes sense since num is really &num[0], I dont
understand why the same isn't the case for the first example.

Thanks everybody....
 
M

M.T

the "<<"method of char* are different from that of int*
u can also write a class and overload its "<<"method and it
will perform what u want it to do.
 
J

James Kanze

Hello, I am new to c++ and have a very simple question.
Why when reading a char array the whole array is displayed for
example
char st[] = " hello " ;
cout << st << endl ;
Good grief I hate this syntax.

Propose something better:).
Why... why are you trying to *shift* cout left by (st shifted
left by endl bits) bits and expect anything to display at all?

Where do you see any shifting? In C++, the << operator is
insertion, the >> operator extraction. (It's true that they are
abusively overloaded for shifting integral types, but that's
just for reasons of C compatibility.)
 
L

LR

Chris said:
Hello, I am new to c++ and have a very simple question.

Why when reading a char array the whole array is displayed for
example

char st[] = " hello " ;
cout << st << endl ;


Good grief I hate this syntax.

Why? What would you suggest as an alternative?
Why... why are you trying to *shift* cout left by (st shifted left by endl bits) bits

The << operator has been overloaded in the standard library for output,
so shift is not occurring here.

It wouldn't seem reasonable to me to shift a stream.

and expect anything to display at all?

Because these overloads are part of the standard and intended for
output, so that's a reasonable expectation.
I dont get it.


You may find this link
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/ where you can
find a link to the latest draft standard, N2691, useful.

LR
 
L

LR

Hello, I am new to c++ and have a very simple question.

Why when reading a char array the whole array is displayed for
example

char st[] = " hello " ;
cout << st << endl ;

will then display the whole array.

but if you use an int array e.g

int num [] = {1,2,3};
cout << num ;

will display the address of the first number;

the second example makes sense since num is really &num[0], I dont
understand why the same isn't the case for the first example.

I don't know the reason why this choice was made, but I'll guess anyway.

It has to do with capability and expectation.

st is zero terminated, so it's easy to write a function to write out the
characters and stop writing when the zero character is reached.

OTOH num is not zero terminated.

I suspect that this is the behavior that most programmers would expect
or at least desire.

Also, consider, if
std::cout << st << std::endl
were to print the address of st, what syntax would you use to print the
contents of the string stored at st? Would you allow,
std::cout << "hello" << std::endl;

LR
 

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
474,170
Messages
2,570,927
Members
47,469
Latest member
benny001

Latest Threads

Top