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

A

Alfonso Morra

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
 
K

Krishanu Debnath

Alfonso said:
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)
 
S

Suman

Krishanu said:
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.
 
C

Christopher Benson-Manica

Krishanu Debnath said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top