
December 30th, 2005, 04:15 PM
| | | pointer and string
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. | 
December 30th, 2005, 04:25 PM
| | | Re: pointer and string
Xiaoshen Li wrote:[color=blue]
> 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"?[/color]
You really need to read the FAQ. http://www.parashift.com/c++-faq-lite/ | 
December 30th, 2005, 04:25 PM
| | | Re: pointer and string
Xiaoshen Li wrote:[color=blue]
> 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.[/color]
Get a better book. Our favorite around here is _Accelerated C++_ by
Koenig and Moo. Your library may have it.
M | 
December 30th, 2005, 04:35 PM
| | | Re: pointer and string
Xiaoshen Li wrote:[color=blue]
> 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?[/color]
The first is a memory leak waiting to happen whereas the second is an
illegal attempt to modify a string literal waiting to happen.
[color=blue]
> How to set pString to
> "Hello world"?[/color]
std::string pString = "Hello world";
Gavin Deane | 
December 30th, 2005, 06:05 PM
| | | Re: pointer and string
> char *pString = new char[20];[color=blue]
> char *pS = "Good morning";
> What is the difference between the above two ways? How to set pString to
> "Hello world"?[/color]
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"); | 
December 30th, 2005, 08:45 PM
| | | Re: pointer and string
On Fri, 30 Dec 2005 12:26:28 +0000 in comp.lang.c++, Xiaoshen Li
<xli6@gmu.edu> wrote,[color=blue]
>Dear All,
>
>My textbookd of C++ teaches string before teaching pointer.[/color]
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.
[color=blue]
>After teaching pointer, it doesn't come back to string.
>I hope to learn how to
>use a pointer to char to manipulate string?[/color]
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. | 
December 30th, 2005, 11:25 PM
| | | Re: pointer and string
Xiaoshen Li wrote:[color=blue]
> Dear All,
>
> My textbookd of C++ teaches string before teaching pointer.[/color]
char* is not a string. char* points at a single char.
The C++ string type is called string.
What text book are you using?
[color=blue]
>
> char *pString = new char[20];
>
> char *pS = "Good morning";
>
> What is the difference between the above two ways?[/color]
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.
[color=blue]
> How to set pString to
> "Hello world"?[/color]
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... |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|