473,320 Members | 2,094 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.

how to get correct output for char*

int main()
{
char *test = "hello";
std::cout << *test << std::endl;
return 0;

}
the result of out put is h; only the first character. But what I want
is the whole string. How to do that?

Sep 19 '06 #1
4 4296
In article <11*********************@k70g2000cwa.googlegroups. com>,
hn*****@gmail.com says...
int main()
{
char *test = "hello";
std::cout << *test << std::endl;
return 0;

}
the result of out put is h; only the first character. But what I want
is the whole string. How to do that?
std::cout << test << std::endl;

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 19 '06 #2

<hn*****@gmail.comwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...

#include <iostream/* declares 'std::cout' */
#include <ostream /* declares 'std::endl' */
int main()
{
char *test = "hello";
Should be:

const char *test = "hello";

Without the 'const', the compiler would allow you
to modify the literal via the pointer. But modification
of a string literal produces undefined behavior, where
Bad Things can happen.
std::cout << *test << std::endl;
return 0;

}
the result of out put is h; only the first character.
That's exactly what your program told the computer to do.
Read about 'pointer dereference'.
>But what I want
is the whole string. How to do that?
std::cout << test << '\n';
An aside: in C++, instead of using 'C-style' strings
(zero terminated arrays of characters), I recommend
using the std::string type (declared by the header
<string>) instead.

-Mike
Sep 19 '06 #3

hn*****@gmail.com wrote:
int main()
{
char *test = "hello";
std::cout << *test << std::endl;
return 0;

}
the result of out put is h; only the first character. But what I want
is the whole string. How to do that?
You got the correct answer. You may be wondering why you should do
that and this is normal. I would suggest that you need to learn from a
different source that doesn't have you mucking with pointers before
explaining them. It used to be necissary to do this when teaching C
but it no longer is and some teachers and books haven't changed. You
will have to learn pointers eventually but you can put this off for
quite some time. You can wait until you have a grasp on programming in
general before you have to learn about addresses and dereferencing.

So learn from a source that teaches using std::string instead of char*.
Something that puts off pointer stuff for a more appropriate time.

Sep 19 '06 #4
hn*****@gmail.com wrote:
int main()
{
char *test = "hello";
std::cout << *test << std::endl;
return 0;

}

the result of out put is h; only the first character. But what I want
is the whole string. How to do that?
There is an overload for operator<< that takes a char*.

std::cout << hello << '\n';

When you dereference (*test), since test is a pointer to a (single)
char, you get only the first character as above. The overload of
operator<< for char* assumes that it is a C-style string and will print
out characters until it gets to a null terminator.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Sep 19 '06 #5

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

Similar topics

3
by: pv1 | last post by:
Hello, I am somewhat new to Access and vba, but am trying to complete a request that has been made of me. Access version 2002 WindowsXP. At this point these text boxes are bound to the table...
19
by: Dancefire | last post by:
Hi, everyone It might be a simple question, but I really don't know the answer. char c = '1'; cout << c; The above code will only output a '1' rather than 0x31; If I use int cast, it can...
13
by: Carramba | last post by:
Hi! I have written some peace of code, but I wonder if it's legal for ansi-c I have no problem to compiling it, but since I'm inexperience and the output is not correct I have doubts. Thank you...
20
by: Junmin H. | last post by:
Hello, I am trying to print the range of unsigned char and unsigned int. The one for char is working good, gives me a correct output, however the other one for int doesnt, why?? Thanks #include...
6
by: zhangsonglovexiaoniuniu | last post by:
Dear all, I need you help. here the program: char a = 0x91; printf("%x",a);
2
by: pks83 | last post by:
In MSSQL I am using datetime as the column type and trying to execute the below mentioned query select time from tickets where ticket_id = '1'; when i execute the query on sql manager window it...
13
by: Jim Langston | last post by:
I had asked this in comp.lang.c++ with out any answers that would actually work, so I'm hoping someone here may know a way. I am calling C library functions that want to output to stdout. I need...
0
by: Masterstill | last post by:
I've faced some problems while designing a page. the below codes look perfect on 800 * 600 browser but higher resolution is not supported. I've tried using % instead of px. but still i could not...
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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)...
1
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...
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
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.