473,320 Members | 1,921 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,320 software developers and data experts.

reading a char array

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....
Aug 5 '08 #1
8 2575
M.T
"om********@googlemail.com" <om********@googlemail.comwrites:

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.
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....
Aug 5 '08 #2
<om********@googlemail.comwrote in message news:72**********************************@a1g2000h sb.googlegroups.com...
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... why are you trying to *shift* cout left by (st shifted left by endl bits) bits and expect anything to display at all? I dont get it.
Aug 6 '08 #3
On 2008-08-06 09:40:21 -0400, "Chris Becke" <ch*********@gmail.comsaid:
<om********@googlemail.comwrote in message
news:72**********************************@a1g2000h sb.googlegroups.com...
>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... why are you trying to *shift* cout left by (st shifted left by
endl bits) bits and expect anything to display at all? I dont get it.
That's one of the first adjustments you have to make when you start
programming in C++. For cout, << is an inserter, not a shift operator.
So this line says to insert the contents of st into cout, then insert
endl into cout. When endl is inserted it starts a new line and flushes
the buffer.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 6 '08 #4
On Aug 6, 3:40 pm, "Chris Becke" <chris.be...@gmail.comwrote:
<omidsol...@googlemail.comwrote in messagenews:72**********************************@a 1g2000hsb.googlegroups.com...
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.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 6 '08 #5
In article <72**********************************@a1g2000hsb.g ooglegroups.com>,
om********@googlemail.com <om********@googlemail.comwrote:
>Why when reading a char array the whole array is displayed for
example
I was a bit confusing on your use of "reading" but I think you
meant more "looking through" or something like that, because,
below you are clearly writing the array.
>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.
Because it is a tossback to C, where "we" like to consider
char arrays as strings, as doing that is often handy, so there
is specicially an op<< for [const] char *'s as entities but not
for some other arrays. As you note, that's not a perfect situation,
and so can have some issues. That said, a suggestion is to consider
std::string as well as std::vector<>s if you're wanting or needing
some more consistency here (though I suspect you're mainly just trying
to figure out the inconsistency at the moment).
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 6 '08 #6
LR
Chris Becke wrote:
<om********@googlemail.comwrote in message news:72**********************************@a1g2000h sb.googlegroups.com...
>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/wg...s/papers/2008/ where you can
find a link to the latest draft standard, N2691, useful.

LR
Aug 7 '08 #7
LR
om********@googlemail.com wrote:
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
Aug 7 '08 #8
On Aug 7, 1:40 am, "Chris Becke" <chris.be...@gmail.comwrote:
>
Why... why are you trying to *shift* cout left
1994 called, they want their troll back.
Aug 8 '08 #9

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

Similar topics

8
by: dbuser | last post by:
Hi, I need help on a problem, as described below. I am reading a file "input.txt"which has data like this: abc def gh izk lmnopq rst uvwxyz I am using fstream object to read the file and...
6
by: Neil Patel | last post by:
I have a log file that puts the most recent record at the bottom of the file. Each line is delimited by a \r\n Does anyone know how to seek to the end of the file and start reading backwards?
21
by: EdUarDo | last post by:
Hi all, I'm not a newbie with C, but I don't use it since more than 5 years... I'm trying to read a text file which has doubles in it: 1.0 1.1 1.2 1.3 1.4 2.0 2.1 2.2 2.3 2.4 I'm doing...
18
by: John | last post by:
Hi, I'm a beginner is using C# and .net. I have big legacy files that stores various values (ints, bytes, strings) and want to read them into a C# programme so that I can store them in a...
4
by: n_jaksic | last post by:
I need to store heterogeneous data (for example, a string, a vector of floats and some ints) in an unsigned character array, for the purpose of storing information in some header. I then need to...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
7
by: Neo | last post by:
Hi, I have a programs that reads char ouput and should send a signal when it reads "login". I am currently reading into a char array but how do I check for the match? I don't think I can do- char...
1
by: freeurmind | last post by:
Hi, i have a text file input.txt that contain binary data i'm reading a stream of bytes and then converting them from unreadable symbols to a string of characters. My conversion is from...
21
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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

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.