user1010101
user1010101

Reputation: 1658

Pointer to Pointer to a structure in c

I have a simple structure.

struct grades
{
 int lowest;
 int highest;
};

Then I am to create a function that returns a pointer which initializes the structure.

struct *grades setGrades(int low, int high)
{
  struct *ptr = malloc(sizeof(struct grades));
  ptr->lowest = low;
  ptr->highest = high;
  return ptr;
}

Now I am supposed to make a function with definition struct **grades random(int size); I am supposed to allocate space for an array of pointers to grade structures with the number of elements equal to size. When the array of pointers is created, I want to set each pointer in the array to a newly created data structure that has the low variable equal to 10 and high variable equal to 100 and then return pointer.

I am really lost at this point as I looked up double pointers to structures online but did not find any examples that can help me clear my understanding. I was wondering if someone can perhaps explain me how double pointers to structures work it would give me a Great start in the right directions.

Upvotes: 1

Views: 341

Answers (3)

wolfPack88
wolfPack88

Reputation: 4203

A "double pointer" as you called it is simply a pointer to a pointer. That is, struct grade example is a variable that contains a lowest and a highest as you've defined. A pointer to that, struct grade *example is a variable that stores the memory address of the same structure. A pointer to a pointer, struct grade **example, is a variable that stores the memory address of a variable that stores the memory address of your structure. A more detailed explanation can be found here. Anyways, to answer your specific question, a function would be:

struct grades** random(int size) {
    struct grades** result = malloc(sizeof(struct grades*) * size); //here you are
                                                    //allocating enough space for an
                                                    //array of pointers
    int i;
    for(i = 0; i < size; i++) {
        result[i] = setGrades(10, 100); //here you are setting each pointer to one
                                        //grade through the function you've already
                                        //defined
    }

    return result;
}

Upvotes: 2

log0
log0

Reputation: 10917

struct grades { int lowest; int highest; };

struct grades * createGradeSet(int low, int high) //careful: return type is struct grades *
{
  // variable name: ptr, type: struct grades *
  struct grades * ptr = malloc(sizeof(struct grades)); 
  ptr->lowest = low;
  ptr->highest = high;
  return ptr;
}

struct grades ** random(int size)
{
  // Create a pointer to an array of struct grades pointers
  // the size of the array is `size` x the size of a struct grades pointer
  struct grades ** ptr_arr = malloc(sizeof(struct grades *) * size); 
  for (unsigned int i = 0; i < size; i++)
     ptr_arr[i] = createGradeSet(10, 100);  // assign a newly created Gradeset to every cell
  return ptr_arr;
}

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 310990

Try the following

struct grades ** random( size_t size )
{
    if ( size == 0 ) return NULL:

    struct grades **p = malloc( size * sizeof( struct grades * ) );

    if ( p != NULL )
    {
        size_t i = 0;
        do
        {
            p[i] = malloc( sizeof( struct grades ) );
            if ( p[i] != NULL )
            {
                p[i]->lowest = 10;
                p[i]->highest = 100;
            }
        } while ( p[i] != NULL && ++i < size );

        if ( i != size )
        {
            for ( size_t j = 0; j < i; j++ ) free( p[i] );
        }
        free( p );
        p = NULL;    
    }

    return p;
}      

Function setGrades should be written as

struct *grades setGrades( int low, int high )
{
    struct *p = malloc( sizeof( struct grades ) );

    if ( p != NULL )
    {
        p->lowest = low;
        p->highest = high;
    }

    return p;
}

In this case the do while loop in the above function can be written as

        do
        {
            p[i] = setGrades( 10, 100 );
        } while ( p[i] != NULL && ++i < size );

Upvotes: 1

Related Questions