user2256825 user2256825 - 3 months ago 14
C++ Question

warning: deprecated conversion from string constant to ‘char*’ when i add a formal parameter in the function def but with no actual parameter

The code below doesnt throw any warnings while i compile

#include <iostream>

void GetHMACCode(unsigned char* buffer,long bufferLength,unsigned char** pResult,unsigned int &nResultLen){}

unsigned char *pCRCBufferptr ;

main() {
unsigned char *pHMAC = NULL;
int ncrcDataLength;
unsigned int nHMACLen = 0;
GetHMACCode(pCRCBufferptr,ncrcDataLength,&pHMAC,nHMACLen);

}


But When i add an extra argument in the function (not in the function call), I get the below warning

myFile.cpp: In function ‘int main()’:
myFile.cpp:11: warning: deprecated conversion from string constant to ‘char*’


Added extra arguement in the function

#include <iostream>

void GetHMACCode(unsigned char* buffer,long bufferLength,unsigned char** pResult,unsigned int &nResultLen,char *extra_arg = "11111111111111111111"){}

unsigned char *pCRCBufferptr ;

main() {
unsigned char *pHMAC = NULL;
int ncrcDataLength;
unsigned int nHMACLen = 0;
GetHMACCode(pCRCBufferptr,ncrcDataLength,&pHMAC,nHMACLen);

}


I am confused why this warnign is being thrown

Thanks
Tejas

Answer

The warning is about converting the string literal to a non-const char* (char* extra_arg = "...").

Changing the definition of your function to the following should make the warning go away:

void GetHMACCode(unsigned char* buffer,long bufferLength,unsigned char** pResult,unsigned int &nResultLen, const char *extra_arg = "11111111111111111111"){}
Comments