assign string to chr*

C

cerr

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
 
I

Ian Collins

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[].
 
J

Juha Nieminen

cerr said:
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.)
 
M

MJ_India

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
 
J

James Kanze

On 07/31/11 10:11 AM, Sasha Daje wrote:
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).
 
I

Ian Collins

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

SG

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
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top