Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   warning: assignment from incompatible pointer type warning in a function poiter (http://www.programmingforums.org/showthread.php?t=13778)

jakh Aug 16th, 2007 3:14 PM

warning: assignment from incompatible pointer type warning in a function poiter
 
hello there,
I am writing a C program in which I need a function pointer to be assigned. The code is as follows

:

typedef struct nsmail_inputstream {

  void *rock;
  int (*read) (void *rock,
            char *buf, unsigned size);
  void (*rewind) (void *rock);
  void (*close) (void *rock);
} nsmail_inputstream_t;

int Test_read(void *rock,char *buf, unsigned size)
{
    if(fgets(buf,size,(FILE *)rock) == NULL)
        return -1;
    return strlen(buf);
}

nsmail_inputstream_t l_inputStream;

l_inputStream.read = Test_read;


This end up with the following warnings and I can't figure out why please help me,

warning: assignment from incompatible pointer type

Thanks in advace.
--jakh

Jessehk Aug 16th, 2007 3:54 PM

Seems to work for me :confused:

:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct nsmail_inputstream {
    void *rock;
    int (*read)( void *rock, char *buf, unsigned size );
    void (*rewind)( void *rock );
    void (*close)( void *rock );
} nsmail_inputstream_t;

int Test_read( void *rock, char *buf, unsigned size ) {
    if ( fgets( buf, size, (FILE *)rock ) == NULL )
        return -1;
   
    return strlen( buf );
}

int main( void ) {
    nsmail_inputstream_t inputStream;
    inputStream.read = Test_read;
   
    return 0;
}


:

$ gcc -std=c99 -Wall -Wextra -pedantic example.c -o example
$ ./example


peace_of_mind Aug 16th, 2007 4:16 PM

jakh, what compiler are you using?

jakh Aug 16th, 2007 11:32 PM

hi all,
Thanks everybody for quick reply, My compiler is gcc and I used the following options.(I have included the required header files etc..)
gcc -g -Wall test.c

yeah it should work , but I don't know why it gives such warnings.

hey Jessehk,
I copy and paste this code and tried , again it worked fine,

Thank your for your help

niteice Aug 16th, 2007 11:58 PM

Does Test_read have the same declaration as the function pointer?

jakh Aug 17th, 2007 12:05 AM

yeah it is ...
please see the above code fragment.
Thank you

--jakh


All times are GMT -5. The time now is 2:58 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC