MinGW and WinPcap, can't fix warning

I've recently moved from MSVC 2010 compiler to MinGW 4.9.1. I've changed everything I needed in my app, fixed all warnings except this one:

 
deprecated conversion from string constant to 'char*'


When I was using MSVC I had no warnings of this kind.

This is the part of code that gives the warning:

 
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, alldevs, errbuf) == -1)


errbuf is declared this way:
 
char errbuf[PCAP_ERRBUF_SIZE];


I've already tried a few solutions like the ones from here - http://en.wikibooks.org/wiki/GCC_Debugging/g%2B%2B/Warnings/deprecated_conversion_from_string_constant but none of them worked.
Last edited on
You should allocate errbuf on the heap to fix the warning, like this:

1
2
3
4
5
6
7
char* errbuf = new char [PCAP_ERRBUF_SIZE];
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, alldevs, errbuf) == -1)

// do your thing

delete [] errbuf;


Unfortunately that didn't work either, still same warning.
Then it is PCAP_SRC_IF_STRING that makes you trouble. Looking at documentation, it is just a define:
https://www.winpcap.org/docs/docs_40_2/html/group__remote__source__string.html

It is some mistake in a library itself that the first argument does not take const char* as it should.

To "fix it" use a cast or just ignore the warning. Alternatively, you can do this:
1
2
3
4
5
6
7
char* ifString  = new char[sizeof PCAP_SRC_IF_STRING + 1];
strcpy ( ifString, PCAP_SRC_IF_STRING );

// then call the function like this:
if (pcap_findalldevs_ex(ifString, NULL, alldevs, errbuf) == -1)

delete [] ifString;

Thanks, that fixed the warning.
Last edited on
Topic archived. No new replies allowed.