cannot convert parameter 2 from 'const char *' to 'char *'

Discussion in 'C Programming' started by Alfonso Morra, Aug 12, 2005.

  1. Hi,

    I'm using a third party library which has a function that expects an arg
    of type const char*. I have a char* which is assigned a value at run
    time - how can I pass it to my function?

    Thanks
    Alfonso Morra, Aug 12, 2005
    #1
    1. Advertising

  2. Alfonso Morra wrote:
    > Hi,
    >
    > I'm using a third party library which has a function that expects an arg
    > of type const char*. I have a char* which is assigned a value at run
    > time - how can I pass it to my function?
    >
    > Thanks


    Probably you told us the opposite. Your function expects a char*
    argument and you
    are passing const char* actual. Use a explicit char* cast before
    passing it to the function, thats the only option you have.(Assuming
    you cannot change third party library function)

    --
    Krishanu Debnath, Aug 12, 2005
    #2
    1. Advertising

  3. Alfonso Morra

    Suman Guest

    Krishanu Debnath wrote:
    > Alfonso Morra wrote:
    > > Hi,
    > >
    > > I'm using a third party library which has a function that expects an arg
    > > of type const char*. I have a char* which is assigned a value at run
    > > time - how can I pass it to my function?
    > >
    > > Thanks

    >
    > Probably you told us the opposite. Your function expects a char*
    > argument and you
    > are passing const char* actual. Use a explicit char* cast before
    > passing it to the function, thats the only option you have.(Assuming
    > you cannot change third party library function)
    >
    > --
    >


    Any decently written library should have things like when to
    use const and when not to, sorted out.
    What if the third party modifies the input?
    Think of ...
    strcpy("Hello", "World");
    and you'll get my point.
    Better still, copy it to some temporary buffer and pass the buffer.
    Surely, some code would have been most welcome here.
    Suman, Aug 12, 2005
    #3
  4. Krishanu Debnath <> wrote:

    > Probably you told us the opposite. Your function expects a char*
    > argument and you
    > are passing const char* actual. Use a explicit char* cast before
    > passing it to the function, thats the only option you have.(Assuming
    > you cannot change third party library function)


    That's only advisable if the const char * actually points to
    modifiable memory; if it does, it might be better off not being
    declared const. If it does not (for example, if it points to a string
    literal), and the third party function attempts to modify the string
    it is passed, tragedy is the likely result.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Aug 12, 2005
    #4
    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.

Share This Page