Esempio n. 1
0
int main() {
	// we will create three integer arrays of array_len
	int array_len = 10;
	// create the three arrays using the function
	// above, using different multipliers so we can
	// tell them apart when we print them
	int *a = create_int_array(array_len,10);
	int *b = create_int_array(array_len,100);
	int *c = create_int_array(array_len,1000);

	// create space to store pointers to these
	// arrays (pointers to integer pointers)
	int** array_pointer = malloc(3*sizeof(int*));
	// and store the addresses of the three arrays
	// in this array
	array_pointer[0] = a;
	array_pointer[1] = b;
	array_pointer[2] = c;

	// we can now print out the elements of this
	// "matrix" by iterating indexing into array_pointer
	for(int i=0; i<3; i++) {
		for(int j=0; j<array_len; j++) {
			printf("array_pointer[%d][%d]: %d\n",i,j,
				array_pointer[i][j]);
		}
	}

	// to free the memory we first need to free
	// array_pointer and then we need to free
	// the three individual arrays
	free(array_pointer);
	free(a);
	free(b);
	free(c);

	return 0;
}
Esempio n. 2
0
int
main() {
    printf("auto-array test program\n");

    struct Integer_array my_array;

    create_int_array(&my_array, 4);

    for (int i=0; i<10; i++) {
        append_int(&my_array, 2*i);
    }

    print_array(&my_array);

    return 0;
}