Newbie char* question

Discussion in 'C++' started by Flzw, Nov 13, 2003.

  1. Flzw

    Flzw Guest

    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
    Flzw, Nov 13, 2003
    #1
    1. Advertising

  2. "Flzw" <> wrote in message
    news:bp0f6v$u98$...
    | 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
    Ivan Vecerina, Nov 13, 2003
    #2
    1. Advertising

  3. Flzw

    Flzw Guest

    > 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
    Flzw, Nov 13, 2003
    #3
  4. Flzw

    Flzw Guest

    Hmm, I'm stupid, I just added (LPCSTR) ...
    Flzw, Nov 13, 2003
    #4
  5. 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.
    =?iso-8859-1?Q?Juli=E1n?= Albo, Nov 13, 2003
    #5
  6. Flzw

    Rolf Magnus Guest

    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:\\";
    Rolf Magnus, Nov 13, 2003
    #6
  7. Hi/Salut,
    "Flzw" <> wrote in message
    news:bp0fsr$r9n$...
    > > 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" <> wrote in message
    news:bp0g11$g35$...
    > 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
    Ivan Vecerina, Nov 14, 2003
    #7
  8. 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').
    Kelsey Bjarnason, Nov 15, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    549
  2. wwj
    Replies:
    24
    Views:
    2,507
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    473
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    579
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,043
    Ian Collins
    May 9, 2006
Loading...

Share This Page