0

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

user2256825
  • 594
  • 6
  • 22
  • @Holt: Instead of ignoring the warning, the OP would be better understanding that in C++ a string literal can only be assigned to `const char*` and `char*` is only for backwards compatibility with C. – Jesse Good Aug 17 '16 at 13:16

2 Answers2

2

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"){}
Peter K
  • 1,372
  • 8
  • 24
  • so after this change my code is throwing run time error – user2256825 Aug 17 '16 at 13:31
  • undefined reference to `GetHMACCode(unsigned char*, long, unsigned char**, unsigned int&, char const*)' when i call this function without last argument GetHMACCode(pCRCBufferptr, ncrcDataLength, &pHMAC, nHMACLen); – user2256825 Aug 17 '16 at 13:32
  • @user2256825 Have you changed both the declaration in your header and the definition in you implementation file? – Peter K Aug 17 '16 at 13:33
  • The definition and declaration are both actually in the header file , Changed both now, and is working fine – user2256825 Aug 17 '16 at 13:37
  • Dont forgot to accept an answer if it solved your problem. – Peter K Aug 17 '16 at 13:40
1

Adding const solves your problem:

void GetHMACCode(unsigned char* buffer,
                 long bufferLength,
                 unsigned char** pResult,
                 unsigned int &nResultLen,
                 const char *extra_arg = "11111111111111111111") // const added here
{}
alexeykuzmin0
  • 6,344
  • 2
  • 28
  • 51