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.... 8 2575
"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....
<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.
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)
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
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?
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 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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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?
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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....
|
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
| |