Skip over navigation

Why Use Pointers?

Problems

Dynamic Memory Allocation

How to Cite This SparkNote

Problem : Write a function which takes an integer n, allocates space for a string of n characters, and returns a pointer to that space.

char *create_string(int n)

Problem : Write a function which allocates an array of n integers, where n is a parameter to the function.

int *create_array(int n)

Problem : Write a function int **create_2d_array(int x, int y) which allocates a two-dimensional array of size x by y and returns a pointer to it.

int **create_2d_array(int x, int y) { int **xdir; int *ydir; int i; ydir = malloc(x * y * sizeof(int)); if (ydir == NULL) return NULL; xdir = malloc(x * sizeof(int*)); if (xdir == NULL) return NULL; for(i=0; i

Follow Us