1// Use malloc to allocate memory
2ptr = (castType*) malloc(size);
3int *exampl = (int*) malloc(sizeof(int));
4// Use calloc to allocate and inizialize n contiguous blocks of memory
5ptr = (castType*) calloc(n, size);
6char *exampl = (char*) calloc(20, sizeof(char));
1
2// declare a pointer variable to point to allocated heap space
3int *p_array;
4double *d_array;
5
6// call malloc to allocate that appropriate number of bytes for the array
7
8p_array = (int *)malloc(sizeof(int)*50); // allocate 50 ints
9d_array = (int *)malloc(sizeof(double)*100); // allocate 100 doubles
10
11
12// use [] notation to access array buckets
13// (THIS IS THE PREFERED WAY TO DO IT)
14for(i=0; i < 50; i++) {
15 p_array[i] = 0;
16}
17
18// you can use pointer arithmetic (but in general don't)
19double *dptr = d_array; // the value of d_array is equivalent to &(d_array[0])
20for(i=0; i < 50; i++) {
21 *dptr = 0;
22 dptr++;
23}
24