473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Invalid conversion from void * to char *

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

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <math.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8.  
  9. #define SECONDS 10 /* produce 10 second(s) of noise */
  10. #define PI 3.14159265358979
  11.  
  12. int put_little_short(char *t, unsigned int value)
  13. {
  14.     *(unsigned char *)(t++)=value&255;
  15.     *(unsigned char *)(t)=(value/256)&255;
  16.     return 2;
  17. }
  18. int put_little_long(char *t, unsigned int value)
  19. {
  20.     *(unsigned char *)(t++)=value&255;
  21.     *(unsigned char *)(t++)=(value/256)&255;
  22.     *(unsigned char *)(t++)=(value/(256*256))&255;
  23.     *(unsigned char *)(t)=(value/(256*256*256))&255;
  24.     return 4;
  25. }
  26.  
  27. /* returns the number of bytes written. skips two bytes after each write */
  28. int fill_data(char *start, int frequency, int seconds)
  29. {
  30.     int i, len=0;
  31.     double value;
  32.     for(i=0; i<seconds*44100; i++) 
  33.     {
  34.         value=32767.0 * sin(2.0*PI*((double)(i))*(double)(frequency)/44100.0);
  35.         put_little_short(start,int(value));
  36.         start += 4;
  37.         len+=2;
  38.     }
  39.     return len;
  40. }
  41.  
  42. int main()
  43. {
  44.     char *buffer=malloc(SECONDS*44100*4+1000);
  45.     char *t=buffer;
  46.     int len;
  47.     int fd;
  48.  
  49.     *t++='R'; *t++='I'; *t++='F'; *t++='F';
  50.     t+=4; /* total length will be put in later */
  51.     *t++='W'; *t++='A'; *t++='V'; *t++='E';
  52.  
  53.     /* format chunk, 8 bytes header and 16 bytes payload */
  54.     *t++='f'; *t++='m'; *t++='t'; *t++=' ';
  55.     t+=put_little_long(t,16); /* I know the length  of the fmt_ chunk*/
  56.     t+=put_little_short(t,1); /* chunk type is always one */
  57.     t+=put_little_short(t,2); /* two channels */
  58.     t+=put_little_long(t,44100); /* samples per second */
  59.     t+=put_little_long(t,44100*2*2); /* bytes per second */
  60.     t+=put_little_short(t,4); /* bytes pro sample (all channels) */
  61.     t+=put_little_short(t,16); /* bits per sample */
  62.  
  63.     /* data chunk, 8 bytes header and XXX bytes payload */
  64.     *t++='d'; *t++='a'; *t++='t'; *t++='a';
  65.  
  66.     len=fill_data(t+4,450,SECONDS); /* left channel, 450Hz sine */
  67.     len+=fill_data(t+6,452,SECONDS); /* right channel, 452Hz sine */
  68.     put_little_long(t,len);
  69.     put_little_long(buffer+4,len+8+16+8);
  70.     fd=open("test.wav", O_RDWR|O_CREAT, 0644);
  71.     write(fd,buffer,len+8+16+8+8);
  72.     close(fd);
  73.     return 0;
  74. }
  75.  
  76.  
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.
Apr 14 '11 #1

✓ answered by Banfa

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 *"

2 8396
Banfa
9,065 Expert Mod 8TB
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 *"
Apr 14 '11 #2
donbock
2,426 Expert 2GB
By the way, you should always confirm malloc succeeded before you dereference the returned pointer.
Apr 15 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Gary James | last post by:
I've inherited a COM dll project that was switched from the VS6 environment to VS.Net. I thought I could debug the COM dll code by adding a VB.Net project to the solution, but when I set...
1
by: VM | last post by:
How can I debug an application during execution? I'm trying to find a way to look at the contents of a variable through the Debug window (like in VB6). Thanks.
0
by: Jeff | last post by:
I am writing a program that must be run on a specific server due to its dependancy on the application we are writing for. VS .NET is installed on my laptop and I installed the Remote Debugging...
2
by: Gary James | last post by:
I've inherited a COM dll project that was switched from the VS6 environment to VS.Net. I thought I could debug the COM dll code by adding a VB.Net project to the solution, but when I set...
2
by: FBM | last post by:
Hi, I am preparing project for submission and i'd like to allow the print out of debug messages. I know that with something like #define DEBUG #ifdef DEBUG print(X); #endif
1
by: apacheutara | last post by:
hai.. i juz started learning C++ language & started to try creating a program. its a simple contacts list. here's the code: ========================================================= ...
3
by: BobAchgill | last post by:
I downloaded and tried to install my program on a Hebrew Windows Professional computer and got the error at start up of the program: Application has generated an exception that could not be...
3
momotaro
by: momotaro | last post by:
am supposed to have the same data that i have entred but its not the case!!! can you help me???? #include<stdio.h> typedef struct { char name; double salary; int rank;
1
by: George2 | last post by:
Hello everyone, I am using Visual Studio 2005 and I am wondering whether we could debug into the global delete operator? I have tried to debug by setting a break point to delete statement,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.