Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #8305 > unrolled thread
| Started by | cerr <ron.eggler@gmail.com> |
|---|---|
| First post | 2011-07-28 19:50 -0700 |
| Last post | 2011-08-09 11:36 +0200 |
| Articles | 10 — 8 participants |
Back to article view | Back to comp.lang.c++
assign string to chr* cerr <ron.eggler@gmail.com> - 2011-07-28 19:50 -0700
Re: assign string to chr* Ian Collins <ian-news@hotmail.com> - 2011-07-29 15:12 +1200
Re: assign string to chr* Juha Nieminen <nospam@thanks.invalid> - 2011-07-29 06:09 +0000
Re: assign string to chr* MJ_India <mail.mohitjain@gmail.com> - 2011-07-29 23:30 -0700
Re: assign string to chr* Sasha Daje <sasha.daje@gmail.com> - 2011-07-30 15:11 -0700
Re: assign string to chr* Ian Collins <ian-news@hotmail.com> - 2011-07-31 10:35 +1200
Re: assign string to chr* James Kanze <james.kanze@gmail.com> - 2011-08-07 10:30 -0700
Re: assign string to chr* Ian Collins <ian-news@hotmail.com> - 2011-08-08 07:39 +1200
Re: assign string to chr* "xieyuntian" <xieyuntian1981@sohu.com> - 2011-08-02 17:37 +0800
Re: assign string to chr* SG <s.gesemann@gmail.invalid> - 2011-08-09 11:36 +0200
| From | cerr <ron.eggler@gmail.com> |
|---|---|
| Date | 2011-07-28 19:50 -0700 |
| Subject | assign string to chr* |
| Message-ID | <f1a1a754-4b33-4c69-9ed4-73ab46c07bb7@d8g2000prf.googlegroups.com> |
hi, how do i correctly assign a string to a char*? If i do it char *test; test= "BLABLA"; I get "warning: deprecated conversion from string constant to ‘char*’" But strcpy() ot sprintf() doesn't work because there's no room allocated yet. So do I actually need to allocate space with calloc()/ malloc()? Thanks, Ron
[toc] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-07-29 15:12 +1200 |
| Message-ID | <99emo8F7uhU1@mid.individual.net> |
| In reply to | #8305 |
On 07/29/11 02:50 PM, cerr wrote: > hi, > > how do i correctly assign a string to a char*? > If i do it > char *test; > test= "BLABLA"; > I get "warning: deprecated conversion from string constant to ‘char*’" > But strcpy() ot sprintf() doesn't work because there's no room > allocated yet. So do I actually need to allocate space with calloc()/ > malloc()? Either use std::string, const char* or char[]. -- Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2011-07-29 06:09 +0000 |
| Message-ID | <4e324e97$0$3926$7b1e8fa0@news.nbl.fi> |
| In reply to | #8305 |
cerr <ron.eggler@gmail.com> wrote: > how do i correctly assign a string to a char*? > If i do it > char *test; > test= "BLABLA"; > I get "warning: deprecated conversion from string constant to ???char*???" Try to read the message. The crucial keyword is "constant". (Hint: your 'test' variable isn't.)
[toc] | [prev] | [next] | [standalone]
| From | MJ_India <mail.mohitjain@gmail.com> |
|---|---|
| Date | 2011-07-29 23:30 -0700 |
| Message-ID | <c96d34b5-6243-4870-9abc-a19e7053a253@g3g2000prf.googlegroups.com> |
| In reply to | #8305 |
On Jul 29, 11:50 am, cerr <ron.egg...@gmail.com> wrote: > hi, > > how do i correctly assign a string to a char*? > If i do it > char *test; > test= "BLABLA"; > I get "warning: deprecated conversion from string constant to ‘char*’" > But strcpy() ot sprintf() doesn't work because there's no room > allocated yet. So do I actually need to allocate space with calloc()/ > malloc()? > Thanks, > Ron Are you sure you want _editable_ pointer to char? 1. If yes, you can avoid malloc/new etc and allocate space on stack or use std::string for automatic allocation and free. char *test; char blabla[] = "BLABLA"; test= blabla; 2. If no, replace char *test; with const char *test; or char const *test; as per your preferred coding style. -Mohit
[toc] | [prev] | [next] | [standalone]
| From | Sasha Daje <sasha.daje@gmail.com> |
|---|---|
| Date | 2011-07-30 15:11 -0700 |
| Message-ID | <234c1699-d7b4-4f35-930b-ae6a1790d6ec@glegroupsg2000goo.googlegroups.com> |
| In reply to | #8305 |
test = (char*)"BLABLA";
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-07-31 10:35 +1200 |
| Message-ID | <99jf95F194U1@mid.individual.net> |
| In reply to | #8344 |
On 07/31/11 10:11 AM, Sasha Daje wrote: Context? > test = (char*)"BLABLA"; No you don't want to do that if test is a char*. -- Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | James Kanze <james.kanze@gmail.com> |
|---|---|
| Date | 2011-08-07 10:30 -0700 |
| Message-ID | <19cd6be0-459a-4c4e-bdc6-dd13751ec722@fq4g2000vbb.googlegroups.com> |
| In reply to | #8345 |
On Jul 30, 11:35 pm, Ian Collins <ian-n...@hotmail.com> wrote: > On 07/31/11 10:11 AM, Sasha Daje wrote: > Context? > > test = (char*)"BLABLA"; > No you don't want to do that if test is a char*. It depends on where test is defined. I've recently written a lot of code full of const_cast<char*>. I had to interface with a third party library written in C, which didn't use const (or only rarely used const). -- James Kanze
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-08-08 07:39 +1200 |
| Message-ID | <9a87ulFef9U1@mid.individual.net> |
| In reply to | #8544 |
On 08/ 8/11 05:30 AM, James Kanze wrote: > On Jul 30, 11:35 pm, Ian Collins<ian-n...@hotmail.com> wrote: >> On 07/31/11 10:11 AM, Sasha Daje wrote: > >> Context? > >>> test = (char*)"BLABLA"; > >> No you don't want to do that if test is a char*. > > It depends on where test is defined. I've recently written a > lot of code full of const_cast<char*>. I had to interface with > a third party library written in C, which didn't use const (or > only rarely used const). But you didn't do that with string literals, did you? -- Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | "xieyuntian" <xieyuntian1981@sohu.com> |
|---|---|
| Date | 2011-08-02 17:37 +0800 |
| Message-ID | <j18ggh$5qj$1@adenine.netfront.net> |
| In reply to | #8305 |
"cerr" <ron.eggler@gmail.com> ??????:f1a1a754-4b33-4c69-9ed4-73ab46c07bb7@d8g2000prf.googlegroups.com... hi, how do i correctly assign a string to a char*? If i do it char *test; test= "BLABLA"; I get "warning: deprecated conversion from string constant to ‘char*’" But strcpy() ot sprintf() doesn't work because there's no room allocated yet. So do I actually need to allocate space with calloc()/ malloc()? Thanks, Ron VS2008 VS2005, no warning --- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
[toc] | [prev] | [next] | [standalone]
| From | SG <s.gesemann@gmail.invalid> |
|---|---|
| Date | 2011-08-09 11:36 +0200 |
| Message-ID | <j1qv28$i9l$1@speranza.aioe.org> |
| In reply to | #8305 |
Am 29.07.2011 04:50, schrieb cerr: > how do i correctly assign a string to a char*? > If i do it > char *test; > test= "BLABLA"; > I get "warning: deprecated conversion from string constant to ‘char*’" > But strcpy() ot sprintf() doesn't work because there's no room > allocated yet. So do I actually need to allocate space with calloc()/ > malloc()? > Thanks, > Ron Hello Ron, I don't feel like you understand what `test` -- the way you declared it -- actually is. This is because you're speaking of assigning a string to `char*`. But a variable with a type `char*` does not store strings. It stores addresses. So, you can only assign some address to it. While it is possible to use a string literal on the right hand side of such an assignment, what you really assign to test is an address, in this case, the address of the first character. I suggest you try to explain your higher level goal instead. Why do you want to use a pointer? What for? If this is really what you want: Where do you want this pointer to point to? If you want to declare a variable that is able to STORE string VALUES much like an int variable is able to STORE some integer VALUE or a double variable is able to STORE an approximation of a real VALUE, `char*` is not the type you want but `std::string` is. Nobody likes dealing with dynamic memory for string handling manually. Some may say they do but I wouldn't believe them. ;-) Luckily, in C++ there are nicer alternatives. Consider getting a decent C++ book (like Accelerated C++) so you can take advantage of the things that C++ has to offer. SG
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web