Skip to main content
Logo image

Section 21.3 Allocate Memory for Arrays

We can extend the use of malloc() beyond single variables and use it to reserve memory for an entire array!

If you cannot see this codecast, please click here.

Check Your Understanding Check Your Understanding

1.

    Suppose you would like to reserve space in memory for an array of N integers (where N is a variable of type int that holds the number of ints to be stored) and store the resulting location in an integer pointer (an int *)
    array
    
    that has already been declared. What is the correct way to do so?
  • array = (int *) malloc(N*sizeof(int));
    
  • Correct
  • array = (int *) malloc(N*int);
    
  • Not quite. Try again!
  • N = (int *) malloc(array*sizeof(int));
    
  • Not quite. Try again!
  • (int *) array = N*malloc(sizeof(int));
    
  • Not quite. Try again!