By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 865 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

how to get correct output for char*

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a

<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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.