How to call a function several times, but with a different structure as argument?

This is a discussion on How to call a function several times, but with a different structure as argument? within the C Programming forums, part of the General Programming Boards category; Hi I have written a function where I can save a structure in a file. So the prototype looks like ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    26

    How to call a function several times, but with a different structure as argument?

    Hi

    I have written a function where I can save a structure in a file. So the prototype looks like this:
    Code:
    void SaveStructure(FILE *f, STRUCTURE *structure);
    The structure looks for example like this:
    Code:
    typedef struct
    	{
    		int dummy;
    		char text[30];
    
    	} STRUCTURE ;
    Now I want to use the function SaveStructure(...) again, but with this structure for example:
    Code:
    typedef struct
    	{
    		int nr;
    		char name[30];
    		char street[30];
    
    	} NEW_STRUCTURE ;
    How can I make it possible that the function SaveStructure(...) doesn't care how the structure looks like?
    Does the format of the pointer struct always looks the same? So could I make a dummy-structure:
    Code:
    typedef struct
    	{
    		int dummy
    
    	} SAVESTRUCT;
    and use the function:
    Code:
    void SaveStructure(FILE *f, SAVESTRUCT *structure);
    this way?
    Code:
    void main (void)
    {
    	FILE *f1 = NULL;
            FILE *f2 = NULL;
    	STRUCTURE structure;
    	NEW_STRUCTURE new_structure;
    
    	SaveStructure(f1, &structure);
    	SaveStructure(f2, &new_structure);
    
    	return;
    }
    thx for any help....
    Last edited by mabuhay; 02-13-2006 at 10:02 AM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Key to this would be what SaveStructure does. Does it store the structure in binary? Or does it write each member to a text file? If it were a binary, I might write it something like this.
    Code:
    void SaveStructure(FILE *f, void *structure, size_t size);
    Code:
    	SaveStructure(f1, &structure, sizeof structure);
    	SaveStructure(f2, &new_structure, sizeof new_structure);
    If it saves to a text file, perhaps have a third parameter that specifies the structure type. Then you can select a proper pointer type.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    It will be a binary file. Thx for your answer, I will try it out...

    mfg

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    Jup, that works.

    Now I have another problem. In the function, I have to copy the data from one file to another (for example to add a block of data in the middle of the file). For that, I need a temporary structure to carry the block from one file to another. Can I now use a void-pointer too, and for the size of the block, I can use size?

    Generally question about the void-pointer:
    The void-pointer would be the pointer to the beginning of my block of data or whatever I have. And where's the end of the block? Does the system know, that from where the void-pointer points to till the end of the block, data is stored? So the void-pointer acts like a normal variable and will be freed after the function has been finished?

    thx for help again

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by mabuhay
    Now I have another problem. In the function, I have to copy the data from one file to another (for example to add a block of data in the middle of the file). For that, I need a temporary structure to carry the block from one file to another. Can I now use a void-pointer too, and for the size of the block, I can use size?
    Yes. I was imagining something like this.
    Code:
    void SaveStructure(FILE *f, void *structure, size_t size)
    {
        fwrite(structure, size, 1, f);
    }
    Quote Originally Posted by mabuhay
    Generally question about the void-pointer:
    The void-pointer would be the pointer to the beginning of my block of data or whatever I have. And where's the end of the block?
    I was assuming a wrapper for fread/fwrite, which will copy n items of s bytes. What I did was pass the size of the structure in bytes from the calling function and used this for s. One of these blobs of bytes is the n.
    Quote Originally Posted by mabuhay
    Does the system know, that from where the void-pointer points to till the end of the block, data is stored?
    Not exactly.
    Quote Originally Posted by mabuhay
    So the void-pointer acts like a normal variable and will be freed after the function has been finished?
    It acts like a normal variable because it is one. I don't think I'd use the term "freed", I'd say it goes out of scope just the same as if you'd passed a copy of where a structure starts as a structure pointer.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    Thanks for all your answers. It's running

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C interview questions
    By natrajdreams in forum C Programming
    Replies: 7
    Last Post: 12-12-2010, 11:40 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. how to get function call stack
    By George2 in forum C Programming
    Replies: 18
    Last Post: 11-11-2006, 06:51 AM
  4. passing counters between function
    By BungleSpice in forum C Programming
    Replies: 18
    Last Post: 02-21-2004, 05:16 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21