Thread: How to pass a FILE*-array as a void* to a function (and access the files)

  1. #1
    Registered User
    Join Date
    Jan 2010

    Question How to pass a FILE*-array as a void* to a function (and access the files)

    Hi folks,
    Some years now since I was in involved with C-programming and now I just hit a pointer problem which a cannot figure out how to solve....

    My problem is that I have to pass two file handles to a function which takes void* as parameter. (Nothing I can do about that.)

    I've written a small stand-alone program to show the issue. I just cannot get the file handles back and use them in the function. Any hints?

    #include <stdio.h>
    #include <stdlib.h>
    void my_func(void* par1)
    	//how to do this part?!?!?
    		FILE* f1_p=(FILE*) (par1);
    		FILE* f2_p=(FILE*) (par1+4);
    		printf( "f1_p=0x%x\n",(uint) f1_p);
    		printf( "f2_p=0x%x\n",(uint) f2_p);
    //this call generates a segmentation fault if the file handle isn't ok
    		long filepos=ftell(f1_p);  
    int main(void) {
    	FILE 								*f1;
    	FILE								*f2;
    	char* outputFileNameStr="testfile";
    	printf("!!!Hello World!!!\n");
    	//Open up the first file for writing
    	char file[256];
    	strcpy(file, outputFileNameStr);
    	file[strlen(outputFileNameStr)] = '\0';
    	f1 = fopen(file, "w");
    	printf( "%s\n",file);
    	//reuse the file-varaible again -- create a second file
    	file[strlen(outputFileNameStr)] ='_';
    	file[strlen(outputFileNameStr)+1] ='w';
    	file[strlen(outputFileNameStr)+2] ='s';
    	file[strlen(outputFileNameStr)+3] ='\0';
    	printf( "%s\n",file);
        f2 = fopen(file, "w");
        //both these open files should be passed to a function as a void*
        //HOW TO DO THAT
        //try to add them in an array so pointers to them are after each other in memory
    	FILE* fileHandle_arr[2];
    	printf( "f1=0x%x\n",f1);
    	printf( "f2=0x%x\n",f2);
    	printf( "&filehandle_arr[0]=0x%x\n",(uint) &fileHandle_arr[0]);
    	printf( "&filehandle_arr[1]=0x%x\n",(uint) &fileHandle_arr[1]);
    	//cast the array to a void_ptr since the function is defined that way
    	void* void_ptr= &fileHandle_arr[0];
    	printf("Closing files\n");
    	return EXIT_SUCCESS;

  2. #2
    Registered User
    Join Date
    Jun 2005
    When calling;
      void_ptr = fileHandle_arr;
    and in the called function;
    void my_func(void *parameter)
         FILE **files = parameter;
         FILE *f1_p = files[0], f2_p = files[1];     
    In C, there is no need for explicit conversion in the code above, as implicit conversions will work. If your compiler complains about an invalid conversion in my_func() above, it is actually a C++ compiler, and you will need to use an explicit conversion as follows.
    void my_func(void *parameter)
         FILE **files = (FILE **)parameter;    // explicit conversion needed by C++ compiler
         FILE *f1_p = files[0], f2_p = files[1];     
    Note that, in both C and C++, pointer arithmetic is forbidden on a void pointer. It is necessary to convert a void pointer into a pointer to the actual type of interest before doing pointer arithmetic (as pointer arithmetic requires the compiler to know the size of individual elements of whatever is pointed at, and that is indeterminate for a void pointer).

    As to the sanity of using void pointers in this case ..... I'll leave that question until another day.
    Last edited by grumpy; 01-22-2010 at 04:31 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Thanks man!

    The reason that it is a void* is that it is an external library (Acapela TTS engine) where you during initialization can pass anything as the void* and then when the lib fires events during the text-to-speech conversion you get your void* parameter back and can do whatever you want in your own code, for example saving the sounddata in a file and word-synch information in another...


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

Tags for this Thread