Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Newbie char* question

Reply
Thread Tools

Newbie char* question

 
 
Flzw
Guest
Posts: n/a
 
      11-13-2003
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


 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      11-13-2003

"Flzw" <(E-Mail Removed)> wrote in message
news:bp0f6v$u98$(E-Mail Removed)...
| 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
--
http://ivan.vecerina.com




 
Reply With Quote
 
 
 
 
Flzw
Guest
Posts: n/a
 
      11-13-2003
> 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


 
Reply With Quote
 
Flzw
Guest
Posts: n/a
 
      11-13-2003
Hmm, I'm stupid, I just added (LPCSTR) ...


 
Reply With Quote
 
=?iso-8859-1?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      11-13-2003
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.
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      11-13-2003
Flzw wrote:

> 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:\\";

 
Reply With Quote
 
Ivan Vecerina
Guest
Posts: n/a
 
      11-14-2003
Hi/Salut,
"Flzw" <(E-Mail Removed)> wrote in message
news:bp0fsr$r9n$(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
news:bp0g11$g35$(E-Mail Removed)...
> Hmm, I'm stupid, I just added (LPCSTR) ...


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


hth -Ivan
--
http://ivan.vecerina.com


 
Reply With Quote
 
Kelsey Bjarnason
Guest
Posts: n/a
 
      11-15-2003
On Thu, 13 Nov 2003 18:32:22 +0100, Flzw wrote:

> Okay I have something like this :
>
> --
> char* DriveLetter = "C:\\";


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').


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VONAGE Newbie w/newbie question New_kid@nowhere.new VOIP 0 08-11-2007 01:40 PM
another newbie question from another newbie.... Lee UK VOIP 4 05-17-2005 04:10 PM
newbie: cisco vlan newbie question No Spam Cisco 3 06-07-2004 10:02 AM
dumb newbie question (or newbie dumb question) Jerry C. Perl Misc 8 11-23-2003 04:11 AM
Newbie! I'm a newbie! What's wrong with this program? Id0x Python 4 07-20-2003 11:40 PM



Advertisments