pointer and string

X

Xiaoshen Li

Dear All,

My textbookd of C++ teaches string before teaching pointer. After
teaching pointer, it doesn't come back to string. I hope to learn how to
use a pointer to char to manipulate string?
For example, I saw code:

char *pString = new char[20];

char *pS = "Good morning";

What is the difference between the above two ways? How to set pString to
"Hello world"?

Thank you very much.
 
G

Gianni Mariani

Xiaoshen said:
Dear All,

My textbookd of C++ teaches string before teaching pointer. After
teaching pointer, it doesn't come back to string. I hope to learn how to
use a pointer to char to manipulate string?
For example, I saw code:

char *pString = new char[20];

char *pS = "Good morning";

What is the difference between the above two ways? How to set pString to
"Hello world"?

You really need to read the FAQ.

http://www.parashift.com/c++-faq-lite/
 
M

mlimber

Xiaoshen said:
Dear All,

My textbookd of C++ teaches string before teaching pointer. After
teaching pointer, it doesn't come back to string. I hope to learn how to
use a pointer to char to manipulate string?
For example, I saw code:

char *pString = new char[20];

char *pS = "Good morning";

What is the difference between the above two ways? How to set pString to
"Hello world"?

Thank you very much.

Get a better book. Our favorite around here is _Accelerated C++_ by
Koenig and Moo. Your library may have it.

M
 
G

Gavin Deane

Xiaoshen said:
Dear All,

My textbookd of C++ teaches string before teaching pointer. After
teaching pointer, it doesn't come back to string. I hope to learn how to
use a pointer to char to manipulate string?
For example, I saw code:

char *pString = new char[20];

char *pS = "Good morning";

What is the difference between the above two ways?

The first is a memory leak waiting to happen whereas the second is an
illegal attempt to modify a string literal waiting to happen.
How to set pString to
"Hello world"?

std::string pString = "Hello world";

Gavin Deane
 
P

Paul Henderson

char *pString = new char[20];
char *pS = "Good morning";
What is the difference between the above two ways? How to set pString to
"Hello world"?

The difference is that the first allocates a chunk of 20 bytes on the
heap, and puts a pointer to it in pString so you can use that memory to
store a string (or whatever). The second will mean the compiler writes
"Good morning" somewhere into the initialised data area of the program
image, and then the line sets pS to point to whereever that string ends
up. You shouldn't then modify it as pre-initialised data should be left
unchanged; however, you can copy the string to somewhere else using the
pointer pS quite safely. It's a better idea to declare pS as const, so
the compiler doesn't let you change it elsewhere.

To set pString to "Hello World", you could use strcpy(d, s), which
copies a string (s) into a buffer (d), like strcpy(pString, "Hello
World");
 
D

David Harmon

On Fri, 30 Dec 2005 12:26:28 +0000 in comp.lang.c++, Xiaoshen Li
Dear All,

My textbookd of C++ teaches string before teaching pointer.

Good good good good good!

You should be using std::string a lot. You should be using pointers
rarely. All books should teach std::string before pointers.
After teaching pointer, it doesn't come back to string.
I hope to learn how to
use a pointer to char to manipulate string?

Avoid using pointer to char to manipulate strings. Use string
member functions and associated library functions that work with
strings.

Pointer to char is a low-level concept suitable for _implementing_
classes like string. Application level code should use classes like
string, and eschew low-level hacking.

What is the good book you found? Probably Koenig & Moo "Accelerated
C++", one of the few good ones.
 
R

Ron Natalie

Xiaoshen said:
Dear All,

My textbookd of C++ teaches string before teaching pointer.

char* is not a string. char* points at a single char.
The C++ string type is called string.
What text book are you using?
char *pString = new char[20];

char *pS = "Good morning";

What is the difference between the above two ways?

One dynamically allocates an array of 20 chars and set pString to
point to the first char that allocation. You will need to remember
to delete this or you'll lose the allocation.

The second allocates a static array of (const) char initialized with
the letters "Good morning" followed by a null and initalizes pS to
point to the first char of that. Where this is allocated is up to
your implementation.
How to set pString to
"Hello world"?

You'll have to copy each char (the function strcpy will do this).

However, if you want to use strings, USE STRINGS.

You can then do assignment, copying, appending, insertion etc...
without having to worry about the memory allocation for them,
keeping track of the size, etc...
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top