Thread: Creating an array of pointers to int[]

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    3

    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!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Last edited by quzah; 09-29-2006 at 06:31 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    Thanks!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do i declare array of pointers to base clase reobject
    By icantprogram in forum C++ Programming
    Replies: 3
    Last Post: 12-20-2002, 02:30 AM
  2. Array of pointers
    By falconetti in forum C Programming
    Replies: 5
    Last Post: 01-11-2002, 07:26 PM
  3. Loading an array using pointers
    By cheesehead in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2001, 05:23 AM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM