Generic function for initialisation

This is a discussion on Generic function for initialisation within the C Programming forums, part of the General Programming Boards category; Hello, me again! I am trying to write a function that I will be able to use to initialise the ...

  1. #1
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343

    Question Generic function for initialisation

    Hello, me again!

    I am trying to write a function that I will be able to use to initialise the values of any type of data. Any opinions on this function.

    Cheers.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <memory.h>
    
    typedef struct Node NODE;
    
    struct Node
    {
    	int  *pData;
    	NODE *pNext;
    	NODE *pPrev;
    };
    
    void init_list (void *, size_t);
    
    int main(void)
    {
    	NODE *pMylist;
    
    	if ((pMylist = (NODE *) malloc(sizeof(NODE))) == NULL)
    	{
    		printf("ERROR: Unable to allocate memory\n");
    		exit(1);
    	}
    
    	init_list(pMylist, sizeof(NODE));
    
    	free(pMylist);
    
    	return 0;
    }
    
    void init_list (void *pData, size_t size)
    {
    	memset(pData, NULL, size);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    Can't be done (easily)
    http://www.eskimo.com/~scs/C-faq/q7.31.html

    Setting all bits zero, which is what memset(pData, 0, size); does, doesn't guarantee that NULL pointers are really NULL pointers, or that floats are really 0.0

  3. #3
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Ah... OK - so it is probably best to initialise each member individualy to be sure.

    Thanks again Salem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM

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