Allocating a double pointer as an array

This is a discussion on Allocating a double pointer as an array within the C Programming forums, part of the General Programming Boards category; I'm trying to allocate a double pointer within a structure as an array. This is the function so far: Code: ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    31

    Allocating a double pointer as an array

    I'm trying to allocate a double pointer within a structure as an array. This is the function so far:

    Code:
    struct set
    	{
    	int filled;
    	int size;
    	char **data;
    	};
    
    struct set *createSet(int maxElts)
    	{
    	struct set *sp;
    	if ((sp=malloc(sizeof(struct set)))==NULL)
    		{
    		printf("Failed to allocate memory!\n");
    		abort();
    		}
    	sp->filled=0;
    	sp->size=maxElts;
    	if ((sp->data=malloc(sizeof(char *)*maxElts))==NULL)
    		{
    		printf("Failed to allocate memory!\n");
    		abort();
    		}
    		if (sp->data[0]==NULL) //I wanted this to be allocated in such a way that I could use it as an array at this point. However, this is simply coming up as NULL.
    			printf("it isnt working\n");
    	return sp;
    	}
    Anyone know how to do this? I know it is possible, since my instructor gave me the line that mallocs the double pointer with the intent to use it in this fashion.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,246
    You've dynamically allocated an array of pointers (maxElts of them) using malloc(). If you want to have a 2D array, each of those pointers also needs to point at a valid array of char - one way of obtaining those valid arrays is to also dynamically allocate them using malloc().
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Something like this?

    Code:
    /* Assuning we are allocating a[12][12] */
    
    int i;
    
    if( ( sp->data = malloc( 12 * sizeof( char * ) ) ) != NULL )
        for( i = 0; i < 12; i++ )
             if( ( sp->data[i] = malloc( 12 * sizeof( char ) ) ) != NULL )
             { }
    And of-course the free of allocation should be done as follow:

    Code:
    for( i=0; i < 12; i++)
         free( sp->data[i] );
    
    free( sp->data );
    Do some proper error checking and your code indendation is poor!

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    31
    Thanks guys, that was my issue.

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. Passing a pointer to two-dimension array in a function
    By E_I_S in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2008, 09:57 AM
  3. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. "subscript requires array or pointer type"?
    By Nutka in forum C Programming
    Replies: 12
    Last Post: 12-06-2002, 04:51 PM

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