Why can't I sprintf into an LPCSTR? - Programmer

This is a discussion on Why can't I sprintf into an LPCSTR? - Programmer ; I just want the following text in szMsg passed to a function of mine called EventLog(szMsg) which takes a LPCSTR. I can't sprintf directly into an LPCSTR so how can I convert a char * or a char[] into an ...

+ Reply to Thread
Results 1 to 11 of 11

Thread: Why can't I sprintf into an LPCSTR?

  1. Why can't I sprintf into an LPCSTR?

    I just want the following text in szMsg passed to a function of mine called
    EventLog(szMsg) which takes a LPCSTR. I can't sprintf directly into an
    LPCSTR so how can I convert a char * or a char[] into an LPCSTR

    char * szMsg;
    char szMsg[255];
    sprintf(szMsg, "Number %d (Total: %d)\n", iCount, iTotal);

    EventLog(szMsg) fails.

    Is there an easy way to convert a char* or a char[] to an LPCSTR?

    RD



  2. Re: Why can't I sprintf into an LPCSTR?

    "Max Adams" wrote:

    >I just want the following text in szMsg passed to a function of mine called
    >EventLog(szMsg) which takes a LPCSTR. I can't sprintf directly into an
    >LPCSTR so how can I convert a char * or a char[] into an LPCSTR
    >
    >char * szMsg;
    >char szMsg[255];


    This will stop the compiler right here. You've just defined szMsg to
    be two different things. Either use the second one (the array), or add
    a malloc statement to allocate memory for the pointer in the first
    statement. (I'd just use the array.)

    >sprintf(szMsg, "Number %d (Total: %d)\n", iCount, iTotal);
    >
    >EventLog(szMsg) fails.
    >
    >Is there an easy way to convert a char* or a char[] to an LPCSTR?


    A cast should do the trick:

    EventLog((LPCSTR)szMsg);

    --
    Tim Slattery
    Slattery_T@bls.gov

  3. Re: Why can't I sprintf into an LPCSTR?

    "Max Adams" wrote in message
    news:bfh38h$edm1j$1@ID-116287.news.uni-berlin.de...
    > I just want the following text in szMsg passed to a function of mine

    called
    > EventLog(szMsg) which takes a LPCSTR. I can't sprintf directly into an
    > LPCSTR


    LPCSTR is defined to be char const*. 'const' means 'do not modify'.
    Maybe you should have a look at your design and its const-correctness.

    > so how can I convert a char * or a char[] into an LPCSTR


    Conversion from non-const to const are implicit and should not make your
    compiler complain at all.

    > char * szMsg;
    > char szMsg[255];


    Only one of those can be there, as Tim pointed out already.

    > sprintf(szMsg, "Number %d (Total: %d)\n", iCount, iTotal);


    This should compile and run without any errors.

    > EventLog(szMsg) fails.


    If EventLog takes an LPCSTR, like you said it would, the conversion is
    perfomed implicitly and should neither cause warnings nor errors. Please
    show us a compile code snippet that exhibits the behaviour.

    hth
    --
    jb

    (replace y with x if you want to reply by e-mail)



  4. Re: Why can't I sprintf into an LPCSTR?

    "Tim Slattery" wrote in message
    news:ue5ohvcppmggakasicj7n08cesg4eg23cm@4ax.com...
    > "Max Adams" wrote:


    > >sprintf(szMsg, "Number %d (Total: %d)\n", iCount, iTotal);
    > >
    > >EventLog(szMsg) fails.
    > >
    > >Is there an easy way to convert a char* or a char[] to an LPCSTR?

    >
    > A cast should do the trick:
    >
    > EventLog((LPCSTR)szMsg);



    Apart from the fact that a cast in this case is totally unnecessary due
    to an already perfomed implicit conversion, a cast should never be used to
    'shut the compiler up'. My apologies if you did not imply this usage for
    casts.

    regards
    --
    jb

    (replace y with x if you want to reply by e-mail)



  5. Re: Why can't I sprintf into an LPCSTR?


    "Max Adams" wrote in message
    news:bfh38h$edm1j$1@ID-116287.news.uni-berlin.de...
    > I just want the following text in szMsg passed to a function of mine

    called
    > EventLog(szMsg) which takes a LPCSTR. I can't sprintf directly into an
    > LPCSTR so how can I convert a char * or a char[] into an LPCSTR
    >
    > char * szMsg;
    > char szMsg[255];
    > sprintf(szMsg, "Number %d (Total: %d)\n", iCount, iTotal);
    >
    > EventLog(szMsg) fails.
    >
    > Is there an easy way to convert a char* or a char[] to an LPCSTR?
    >
    > RD


    Yes its very easy, converting a char* or a char[] to a LPCSTR happens
    automatically. Why not post the error message, or describe what you mean by
    'fails' or post the code for EventLog.

    john



  6. Re: Why can't I - shut up the compiler?

    "RickMerrill" wrote in message
    newsYWTa.137078$ye4.95787@sccrnsc01...
    > Jakob Bieling wrote:
    > ...
    > > Apart from the fact that a cast in this case is totally unnecessary

    due
    > > to an already perfomed implicit conversion, a cast should never be used

    to
    > > 'shut the compiler up'.

    >
    > Why not? I do that all the time, At Warning level 4, to indicate to
    > myself (and future programmers) that I "knew" what I was doing. I know
    > it clutters up the code, and it might obscure problems for future
    > porting to other systems, but I assert that it is good practice. - RM



    What does the warning level have to do with it? You will not be warned
    when an implicit conversion is performed, whether you cast, or not. But I
    think you are not referring to implicit casts here, correct?

    If you know what you are doing, then a cast is fine. Though, you
    probably do not know what you are doing, if you are using so many casts that
    it clutters up your code -- or you are using a badly designed library, which
    forces many casts in your own code.

    regards
    --
    jb

    (replace y with x if you want to reply by e-mail)



  7. Re: Why can't I - shut up the compiler?

    > > > ... a cast should never be used to 'shut the compiler up'.

    Suppose you like your code to compile without warnings, so that when
    you *do* see a warning, you know that you need to pay attention.

    What is the recommended way to shut the compiler up?

  8. Re: Why can't I - shut up the compiler?

    In article ,
    johnbrown105@hotmail.com (John Brown) wrote:

    > > > > ... a cast should never be used to 'shut the compiler up'.

    >
    > Suppose you like your code to compile without warnings, so that when
    > you *do* see a warning, you know that you need to pay attention.
    >
    > What is the recommended way to shut the compiler up?


    The recommended way is to write correct code. If the compiler is
    complaining, the code is probably incorrect (This is, of course,
    assuming that the compiler has no bugs in it. But, it is more likely
    to be right than you are.)

    In C++, you should never use C casts. Use C++ casts:

    reinterpret_cast< myType >( myExpression )

    static_cast< myType >( myExpression )

    dynamic_cast< myType >( myExpression )

    const_cast< myType >( myExpression )

    Learn the differences, and use the correct one. Know that
    if you need to use const_cast or reinterpret_cast then
    there is probably a bug in the design of the code.

    Read Scott Meyers' books:
    "Effective C++",
    "More Effective C++", and
    "Effective STL",

    Read Herb Sutter's books:
    "Exceptional C++" and
    "More Exceptional C++"

  9. Re: Why can't I - shut up the compiler?

    "John Brown" wrote in message
    news:ee1f9e62.0307251524.388487be@posting.google.c om...

    > > > > ... a cast should never be used to 'shut the compiler up'.


    > Suppose you like your code to compile without warnings, so that when
    > you *do* see a warning, you know that you need to pay attention.


    Sometimes I do, yes.

    > What is the recommended way to shut the compiler up?


    There is none. You are not supposed to shut the compiler up in the first
    place, but design your program to not produce any warnings. As I said in an
    earlier post, this might be impossible due to a badly designed third-party
    library, so you need a cast. But in that case you have a good reason to
    cast, as opposed to "now I do not get the warning anymore".

    regards
    --
    jb

    (replace y with x if you want to reply by e-mail)



  10. Re: Why can't I - shut up the compiler?

    > The recommended way is to write correct code. If the compiler is
    > complaining, the code is probably incorrect


    I am not talking about warnings caused by if (c = EOF).

    First of all, let me say that I am using C, not C++. In C, it is
    impossible to write a Win32 program without casts if STRICT is
    #defined, and even if it is not. In fact, I think you will usually get
    errors with STRICT instead of warnings because HBITMAP, HICON, HFONT,
    HPEN etc. can't be "derived" from HGDIOBJ in C. They cannot be used
    where an HGDIOBJ is expected without a cast. Plus you have the packing
    of parameters of any type that you can imagine into wParam and lParam.
    I suppose that you could use the message cracker macros, but you would
    simply be using evil typecasts typed in by someone else.

    Of course, if your government can say "*We* didn't say that Iraq tried
    to buy uranium from Niger. We said that the *British* said so, and
    they *did* say so. So we didn't lie.", then I can certainly use sinful
    macros declared by Microsoft and still claim to be pure of heart.

  11. Re: Why can't I - shut up the compiler?

    I said:
    > Of course, if your government can say ...


    Of course, I should have said "if the United States government can say .."

    I have no idea where you are from.

+ Reply to Thread