Topic: char* and stream
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/02/15 Raw View
Ahn Ki-yung wrote:
>
> char* str = new char[20]; // allocate memory for 20 characters
> cout<<"Input sting less then 20 letters including white space" <<endl;
> cout<<"More than that can occure an error." <<endl;
> cin>>str;
>
> Let's say that Input was "Haapy New Year !"
> Is it proper to modify the string through the pointer str ?
> like this.
>
> *(str+2) = 'p';
Of course. You got the character array with new, and you
obviously can change that (BTW, you can also just write
str[2]='p'; it's the same). This has nothing to do with
streams; cin >> str just filled your provided array, it
did not allocate a new one.
BTW, it's not a good idea to rely on users not typing more.
Hackers like to mis-use such code ;-)
Generally, the best idea is to use std::string instead.
BTW, if the input was "Haapy New Year !" (should that have been
"Happy"? ;-)), then str just contains "Happy".
The stream extraction operator stops extracting at the next
whitespace. If you want to get full lines, use getline instead.
>
> In C++ string literals are const char[] type so that you cannot
> modify the string literals through the char* type of variables,
> which means result is not defined. (This is beacause literals
> do not have lvalue.) But in the case of copying the string
> literals into array, you can modify the copied string.
>
> char arr[] = "Haapy New Year";
> char* ptr = "Haapy New Year";
Note that the second one is deprecated; it will probably be
disallowed in the next version of the standard. A good compiler
should warn you. Since string literals have type char const[],
according to general type rules, they can only be assigned to
char const*, not to char*. A special rule was made for
string literal assignment, since much old code will contain
just this line. But it's deprecated anyway; new code shouldn't
contain it, and old code should be gradually changed not to
use it.
>
> arr[2] = 'p'; // ok
> ptr[2] = 'p'; // result is not defined (can be error)
Indeed, it can be a call to Universe::~Universe() ;-)
If ptr were declared char const*, this would not compile.
>
> But I think that string copied from streams are not
> literals so that we can properly modify it with the pionter
> as i wrote the code above. Am I right ?
The allocation was with new[], so you can change it.
Who writes into that memoy is irrelevant, only the origin of
the memory is important.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ahn Ki-yung <kyagrd@daidun.kaist.ac.kr>
Date: 1999/02/12 Raw View
char* str = new char[20]; // allocate memory for 20 characters
cout<<"Input sting less then 20 letters including white space" <<endl;
cout<<"More than that can occure an error." <<endl;
cin>>str;
Let's say that Input was "Haapy New Year !"
Is it proper to modify the string through the pointer str ?
like this.
*(str+2) = 'p';
In C++ string literals are const char[] type so that you cannot
modify the string literals through the char* type of variables,
which means result is not defined. (This is beacause literals
do not have lvalue.) But in the case of copying the string
literals into array, you can modify the copied string.
char arr[] = "Haapy New Year";
char* ptr = "Haapy New Year";
arr[2] = 'p'; // ok
ptr[2] = 'p'; // result is not defined (can be error)
But I think that string copied from streams are not
literals so that we can properly modify it with the pionter
as i wrote the code above. Am I right ?
Anxious for your Assurance,
Precious be your Addition.
KYAGRD
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: 1999/02/12 Raw View
Ahn Ki-yung wrote:
> In C++ string literals are const char[] type so that you cannot
> modify the string literals through the char* type of variables,
> which means result is not defined. (This is beacause literals
> do not have lvalue.) But in the case of copying the string
> literals into array, you can modify the copied string.
>
> char arr[] = "Haapy New Year";
> char* ptr = "Haapy New Year";
This is all correct except that the case of the identifier
arr above, you are not "copying a string literal into an
array." You are initializing an appropriate sized array
with those characters. Would be exactly the same as doing:
char arr[15] = { 'H', 'a', 'a', 'p', 'y', ' ', ...
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ahn Ki-yung <kyagrd@daidun.kaist.ac.kr>
Date: 1999/02/14 Raw View
> > char arr[] = "Haapy New Year";
> > char* ptr = "Haapy New Year";
>
> This is all correct except that the case of the identifier
> arr above, you are not "copying a string literal into an
> array."
Ok, not copying. Sorry I was to say it looks like.
ptr[2] = 'p'; // undefined
They are all correct but as I said first this is not correct.
Not defined ! ( See The C++ Programming Language 3rd Edition )
this is beacause "Haapy New Year" is a string literal and it's
type is const char[15]. It is not guaranteed to modify string
literals with char*. And my question is that if you get the string
through cin then if I can modify it with char*.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]