Invalid conversion from void * to char *

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lewis Mbuthia
    New Member
    • Mar 2011
    • 3

    Invalid conversion from void * to char *

    I tried to write a program to write a wav file.Could you please debug it for me?

    Code:
    #include <math.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    #define SECONDS 10 /* produce 10 second(s) of noise */
    #define PI 3.14159265358979
    
    int put_little_short(char *t, unsigned int value)
    {
        *(unsigned char *)(t++)=value&255;
        *(unsigned char *)(t)=(value/256)&255;
        return 2;
    }
    int put_little_long(char *t, unsigned int value)
    {
        *(unsigned char *)(t++)=value&255;
        *(unsigned char *)(t++)=(value/256)&255;
        *(unsigned char *)(t++)=(value/(256*256))&255;
        *(unsigned char *)(t)=(value/(256*256*256))&255;
        return 4;
    }
    
    /* returns the number of bytes written. skips two bytes after each write */
    int fill_data(char *start, int frequency, int seconds)
    {
        int i, len=0;
        double value;
        for(i=0; i<seconds*44100; i++) 
        {
            value=32767.0 * sin(2.0*PI*((double)(i))*(double)(frequency)/44100.0);
            put_little_short(start,int(value));
            start += 4;
            len+=2;
        }
        return len;
    }
    
    int main()
    {
        char *buffer=malloc(SECONDS*44100*4+1000);
        char *t=buffer;
        int len;
        int fd;
    
        *t++='R'; *t++='I'; *t++='F'; *t++='F';
        t+=4; /* total length will be put in later */
        *t++='W'; *t++='A'; *t++='V'; *t++='E';
    
        /* format chunk, 8 bytes header and 16 bytes payload */
        *t++='f'; *t++='m'; *t++='t'; *t++=' ';
        t+=put_little_long(t,16); /* I know the length  of the fmt_ chunk*/
        t+=put_little_short(t,1); /* chunk type is always one */
        t+=put_little_short(t,2); /* two channels */
        t+=put_little_long(t,44100); /* samples per second */
        t+=put_little_long(t,44100*2*2); /* bytes per second */
        t+=put_little_short(t,4); /* bytes pro sample (all channels) */
        t+=put_little_short(t,16); /* bits per sample */
    
        /* data chunk, 8 bytes header and XXX bytes payload */
        *t++='d'; *t++='a'; *t++='t'; *t++='a';
    
        len=fill_data(t+4,450,SECONDS); /* left channel, 450Hz sine */
        len+=fill_data(t+6,452,SECONDS); /* right channel, 452Hz sine */
        put_little_long(t,len);
        put_little_long(buffer+4,len+8+16+8);
        fd=open("test.wav", O_RDWR|O_CREAT, 0644);
        write(fd,buffer,len+8+16+8+8);
        close(fd);
        return 0;
    }
    The problem is in the first line of the main program which my Dev-Cpp compiler says is an invalid conversion from void to char.
    P.S. If you have a quick fix (like in type casting the buffer or the malloc) please text message the statement to me .My number is. I need the code urgently.
    You may also send to my email another source code to carry out the same task. My only interest is that i be able to specify the frequency of the output wav file as per my requirements in the source code.
    Last edited by Meetee; Apr 14 '11, 08:28 AM. Reason: Phone number is deleted
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You are compiling this code as C++ although it looks like C code.

    C will implicitly cast void* to char*, or indeed any other pointer type; C++ wont.

    Either compile the code as C or cast the output of malloc to char*.

    Oh and next time copy and paste the actual compiler errors because the error you are getting is not "invalid conversion from void to char" but rather "invalid conversion from void * to char *"

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      By the way, you should always confirm malloc succeeded before you dereference the returned pointer.

      Comment

      Working...