# Creating an array of pointers to int[]

• 09-29-2006
OkashiiKen
Creating an array of pointers to int[]
Hello all,

I am just beginning to learn C, and have been reading K&R. Ive been doing some exercises from the book, and those ive found online. Ive written a binary search algorithm which operates on rotated sorted lists ({0,1,2,3,4,5} -> {3,4,5,1,2,0}), and would like to test my arrays using nested for loops.

For this reason, I am trying to create one array containing pointers to other int arrays. However, I cant seem to do this properly. I am unsure how to declare an array of pointers to int[]. It seeems incorrect to declare it as an 'int *arr' or 'int[] *arr'. Originally, I had tried passing a pointer to the first element of each sub-array (*a instead of (*a)[size]), but it gave me strange results when i printed out the contents of the sub-arrays.

I am trying to understand the language mechanics and the usage of pointers better, so please comment on the code if there is clearly a better way to do something.

Please ignore the function call to rotated_bin_search().

Code:

``` /* This short function is used to print out the contents of a  * specified array.  */ void printArr(int *arr, int length){   for(int i = 0; i < length; i++){     printf("%d ", arr[i]);   }   printf("\n"); } int main(void) {     int *arr,(*a)[ARR_LENGTH], (*b)[ARR_LENGTH], (*c)[ARR_LENGTH], (*d)[ARR_LENGTH], (*e)[ARR_LENGTH], *t;    int a_arr[ARR_LENGTH] = {1,2,3,4,5,6,7,8,9,10};   int b_arr[ARR_LENGTH] = {5,6,7,8,9,10,1,2,3,4};   int c_arr[ARR_LENGTH] = {6,7,8,9,10,1,2,3,4,5};   int d_arr[ARR_LENGTH] = {3,4,5,6,7,8,9,10,1,2};   int e_arr[ARR_LENGTH] = {9,10,1,2,3,4,5,6,7,8};   a = &a_arr;   b = &b_arr;   c = &c_arr;   d = &d_arr;   e = &e_arr;   int *t_arr[5] = {*a, *b, *c, *d, *e};   //t = &t_arr[0];   for(int k = 0; k < 5; k++){     //arr = t + k;     arr = &t_arr[k];     printf("\nthe array is:\n");     printArr(arr, ARR_LENGTH);     /*for(int j = 0; j < 10; j++){       int target = j + 1;       int count = 0;       int found = rotated_bin_search(arr, ARR_LENGTH, target, &arr[0], &arr[9], count);       if(found!=0)         printf ("\n%d was found in the array\n", found);     }*/   }   return 1; }```
Thanks!
• 09-29-2006
quzah
Code:

`int *array[ SIZE ];`
This is an array of pointers to integers.
Code:

`int (*array)[SIZE];`
This is a pointer to an array of integers.

You probably want the former, not the latter. Then you do something like:
Code:

```int array1[ SIZE ] = { ...stuff... }; ... int arrayN[ SIZE ] = { ...stuff... }; int *array[ SIZE ] = { array1, array2, ... arrayN };```

Quzah.
• 09-29-2006
OkashiiKen
Thanks!
• 09-29-2006
OkashiiKen
If any other newbies ever have this problem.....

Code:

```int main(void) {     int *arr;   int a_arr[ARR_LENGTH] = {1,2,3,4,5,6,7,8,9,10};   int b_arr[ARR_LENGTH] = {5,6,7,8,9,10,1,2,3,4};   int c_arr[ARR_LENGTH] = {6,7,8,9,10,1,2,3,4,5};   int d_arr[ARR_LENGTH] = {3,4,5,6,7,8,9,10,1,2};   int e_arr[ARR_LENGTH] = {9,10,1,2,3,4,5,6,7,8};   int *t_arr[5] = {a_arr, b_arr, c_arr, d_arr, e_arr};   for(int k = 0; k < 5; k++){     arr = t_arr[k];     printf("\nthe array is:\n");     printArr(arr, ARR_LENGTH);     for(int j = 0; j < 10; j++){       /*int target = j + 1;       int count = 0;       int found = rotated_bin_search(arr, ARR_LENGTH, target, &arr[0], &arr[9], count);       if(found!=0) {         printf ("%d was found in the array\n", found);       }*/     }   }   return 1; }```