0

In C this part of code works fine but in C++ i get an error:

argument of type "void *" is incompatible with parameter of type "FILE *"

How can i fix it? This is the relevant part of code:

static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
    {

        curl_off_t nread;

        size_t retcode = fread(ptr, size, nmemb, stream); /*i get an error on this line on stream */

        nread = (curl_off_t)retcode;

        fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
            " bytes from file\n", nread);
        return retcode;

    }

The solution was simple actually in the end.So if it helps anyone i simply changed void* with FILE *ptr and FILE *stream and it worked

Raven
  • 1
  • 3
  • 1
    When speaking French to an English guy you get the same problem. C and C++ are different languages – Ed Heal May 01 '16 at 02:59
  • 2
    Possible duplicate of [void pointers: difference between C and C++](http://stackoverflow.com/questions/1736833/void-pointers-difference-between-c-and-c) – Jfevold May 01 '16 at 03:02
  • 1
    In general it is a bad idea to compile C code with a C++ compiler. That's what C compilers are for. – M.M May 01 '16 at 03:10

2 Answers2

3
   size_t retcode = fread((FILE*)ptr, size, nmemb, stream); /*i get an error on this line on stream */

That's the quick and dirty answer. My question would be, if ptr is always a pointer to a FILE, then why declare the parameter as void*?

void pointers: difference between C and C++ has all the explanation you might ever desire. I've flagged this question as a duplicate, but wanted to get you over this hump in the short term.

Community
  • 1
  • 1
Jfevold
  • 422
  • 2
  • 11
  • 2
    Can you explain your answer a bit more rather than just posting a code snippet? This helps ensure high quality answers across SO. Thanks! – winhowes May 01 '16 at 03:00
3

C allows automatic conversions between void* and pointers of other types:

6.3.2.3 Pointers

1 A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

Hence, it is OK to use ptr in a call to a function that expects a FILE*.

C++ allows automatic conversion from pointers of other types to void* but it does not allow automatic conversion from void* to pointers of other types.

Hence you get the compiler error.

R Sahu
  • 204,454
  • 14
  • 159
  • 270