Newbie char* question

F

Flzw

Okay I have something like this :

--
char* DriveLetter = "C:\\";
.....

// Now I want it to be "D:\\" So I want to do :
DriveLetter[0] += 1; // This crashes the app
--

Well, I'm quite sure I've already done things that looked like that and
worked but oh well, this could just be a dream, anyway, what's wrong ? how
can I have it to work ?
thanks
 
I

Ivan Vecerina

| Okay I have something like this :
|
| --
| char* DriveLetter = "C:\\";
This defines a pointer to a *constant*, static string.
For some ugly backwards-compatibility reason, it is
not a compilation error, but what actualy should be
written is:
char const* DriveLetter = "C:\\"; ...

| // Now I want it to be "D:\\" So I want to do :
| DriveLetter[0] += 1; // This crashes the app
.... which would make this a compile error.

| Well, I'm quite sure I've already done things that looked like that and
| worked but oh well, this could just be a dream, anyway, what's wrong ? how
| can I have it to work ?

This way:
char DriveLetter[] = "C:\\";
This will define a local array of chars, initialized
with "C:\\".
This array can then be modified as you expected:
DriveLetter[0] += 1; // all ok now


hth-Ivan
 
F

Flzw

This way:
char DriveLetter[] = "C:\\";
This will define a local array of chars, initialized
with "C:\\".
This array can then be modified as you expected:
DriveLetter[0] += 1; // all ok now

Yes, this is what I thought, I've tried this but then I have another
problem, I need to use this pointer as an argument to a Win API function and
when I use the above code I get a compilation error :

Impossible to conver Parameter 3 from 'char (*__w64 )[4]' to 'LPCSTR'

any idea ? :/

Thanks for your answer btw
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Flzw escribió:
This way:
char DriveLetter[] = "C:\\";
This will define a local array of chars, initialized
with "C:\\".
This array can then be modified as you expected:
DriveLetter[0] += 1; // all ok now

Yes, this is what I thought, I've tried this but then I have another
problem, I need to use this pointer as an argument to a Win API function and
when I use the above code I get a compilation error :

Impossible to conver Parameter 3 from 'char (*__w64 )[4]' to 'LPCSTR'

any idea ? :/

Perhaps you have used char * DriveLetter [] instead of char DriveLetter
[] ?

Regards.
 
R

Rolf Magnus

Flzw said:
Okay I have something like this :

--
char* DriveLetter = "C:\\";
....

// Now I want it to be "D:\\" So I want to do :
DriveLetter[0] += 1; // This crashes the app
--

Well, I'm quite sure I've already done things that looked like that
and worked but oh well, this could just be a dream, anyway, what's
wrong ? how can I have it to work ?
thanks

You're trying to modify a string literal, which is not modifyable. Your
program invokes undefined behaviour by doing that. The reason that you
can assing a string literal to a pointer to non-const char at all is
historical (legacy from C). Try:

char DriveLetter[] = "C:\\";

This creates a modifyable array of char that contains a copy of the
string literal. Or even better, use std::string:

std::string DriveLetter = "C:\\";
 
I

Ivan Vecerina

Hi/Salut,
Flzw said:
This way:
char DriveLetter[] = "C:\\";
....
Yes, this is what I thought, I've tried this but then I have another
problem, I need to use this pointer as an argument to a Win API function and
when I use the above code I get a compilation error :

Impossible to conver Parameter 3 from 'char (*__w64 )[4]' to 'LPCSTR'

Could you post the line that triggered the error?

My guess is that you might have written:
WinApiCall( & DriveLetter );
instead of:
WinApiCall( DriveLetter );

Flzw said:
Hmm, I'm stupid, I just added (LPCSTR) ...

Casting is dangerous most of the times, and is best avoided...


hth -Ivan
 
K

Kelsey Bjarnason

Okay I have something like this :

Note that "C:\\" is a _literal_. It should really spew an "assignment
loses const" warning or some such, since you're assigning from a
non-modifiable to a modifiable - from a const char * to a char * - but C
and C++ allow this.
// Now I want it to be "D:\\" So I want to do : DriveLetter[0] += 1; //
This crashes the app --

Here, however, you're actually trying to modify that constant value, which
won't work.

Try this:

char DriveLetter[] = "C:\\";
DriveLetter[0] = 'D';

Also note that there's no guarantee, as far as I'm aware, that 'C' + 1 ==
'D'; IIRC, the only things guaranteed to be sequential like that are the
numerics ('0' .. '9').
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top