Complete the allocate_int function on the next slide. It accepts a pointer to a pointer to an integer called pointer_pointer, and a raw value. Change the value of pointer_pointer's pointer's address to point to new memory that has the value of the int.

  • Allocate memory for a single integer on the heap, and save it's address into a new pointer.
  • Update the memory address that pointer_pointer is pointing to, to be the address of the new pointer.
  • Set the raw value of the integer that pointer_pointer now points to the value passed in.
  • 
    void allocate_int(int **pointer_pointer, int value)
    {
    
    }
    
    	

This function correctly allocates an array of token pointers on the heap, but notice that the addresses it's adding to each index are the addresses of the stack-allocated inputs.

  • Update the create_token_pointer_array's loop to allocate new space for each token on the heap, and store the address of the new space in the array.
  • Copy the members of the input struct into the newly allocated one.

token_t** create_token_pointer_array(token_t* tokens, size_t count)
{
  token_t** token_pointers = malloc(count * sizeof(token_t*));
  if (token_pointers == NULL)
  {
    exit(1);
  }
  for (size_t i = 0; i < count; ++i) {
    token_pointers[i] = &tokens[i];
  }
  return token_pointers;
}