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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alfonso Morra

    #1

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

    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

  • Krishanu Debnath

    #2
    Re: cannot convert parameter 2 from 'const char *' to 'char *'


    Alfonso Morra wrote:[color=blue]
    > 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[/color]

    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@india. ti.com

    Comment

    • Suman

      #3
      Re: cannot convert parameter 2 from 'const char *' to 'char *'


      Krishanu Debnath wrote:[color=blue]
      > Alfonso Morra wrote:[color=green]
      > > 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[/color]
      >
      > 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@india. ti.com[/color]

      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.

      Comment

      • Christopher Benson-Manica

        #4
        Re: cannot convert parameter 2 from 'const char *' to 'char *'

        Krishanu Debnath <krishanu.debna th@gmail.com> wrote:
        [color=blue]
        > 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)[/color]

        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)cybers pace.org | don't, I need to know. Flames welcome.

        Comment

        Working...