how to get correct output for char*

H

hnzhang

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?
 
J

Jerry Coffin

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;
 
M

Mike Wahler

#include <iostream> /* declares 'std::cout' */
#include said:
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
 
N

Noah Roberts

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.
 
M

Marcus Kwok

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.
 

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
473,776
Messages
2,569,602
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top