More trouble passing arrays

This is a discussion on More trouble passing arrays within the C Programming forums, part of the General Programming Boards category; Hi, I have been trying to get my head around arrays, though i understood it but have tripped over again ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    26

    More trouble passing arrays

    Hi,

    I have been trying to get my head around arrays, though i understood it but have tripped over again with multidimensional arrays.

    Code:
    int main(int argc, char *argv[])
    {
       
       int num_roads = argc - 1;        /* to change by 1 later !!!!!! */
       /* To create an integer array from the input char array */
       int i;
       int road_type[num_roads];
       for(i = 0; i < num_roads; i++)
       {
          road_type[i] = atoi(argv[i+1]);
       }
       /* -----------------end road type thing-------------------*/
       
       int num_routes = get_num_routes(num_roads, road_type);
       printf("Number of possible routes is %d\n", num_routes);
       
       /* Create an integer array showing possible routes i.e. road 1 to road 4 would be 14 */
       int j;
       int route_list[num_routes][2];
       for(j = 0; j < num_routes; j++)
       {
          route_list[j][0] = get_route_list(num_roads, num_routes, road_type, j, 0);
          printf("%d", route_list[j][0]);
          route_list[j][1] = get_route_list(num_roads, num_routes, road_type, j, 1);
          printf("%d\n", route_list[j][1]);
       }
       /* -----------------end route list thing-------------------*/
       
       route_check(num_roads, num_routes, road_type, route_list);
       return 0;
    }
    
    
    
    
    /* Create an array showing 1 for routes that can not occur simultaneously */
    int route_check(int num_roads, int num_routes, int *road_type, int route_list[][1])
    {
       int i, j, k;
       int from = 9;
       int to = 9;
       int route_holder[] = {0,0};
       int route_clear1;
       int route_clear2;
       int side1[num_roads];
       int side2[num_roads];
       int invalid_routes[num_routes][num_routes]; /* write function to clear this. Stores 1 for invalid route */
       for(i = 0; i < num_routes; i++)
       {
          from = route_list[i][0];
          to = route_list[i][1];
          for(j = 0; j < num_routes; j++)
          {
             for (k = 0; k < num_routes; k++)
             {
                get_side1(side1, num_roads, road_type, from, to);
                get_side2(side2, num_roads, road_type, from, to);
                if(route_list[j][0] == side1[k])
                {
                   route_holder[0] = j;
                   route_clear1++;
                }
                if(route_list[j][1] == side1[k])
                {
                   route_holder[1] = j;
                   route_clear1++;
                }
                if(route_list[j][1] == side2[k])
                {
                   route_holder[1] = j;
                   route_clear2++;
                }
                if(route_list[j][1] == side2[k])
                {
                   route_holder[1] = j;
                   route_clear2++;
                }
             }
          }   
       }
       return 0;//*invalid_routes;
    }
    
    
    /* Function to get a list of of the roads on either side of a particular route. */
    void get_side1(int *side1, int num_roads, int *road_type, int from, int to)
    {
       int i = from;
       int place_holder = 0;
       while(i != to)
       {
          if(i != num_roads - 1)
          {
             side1[place_holder] = i;
             place_holder++;
          }
          else if(i == num_roads - 1)
          {
             side1[place_holder] = i;
             place_holder++;
             i = 0;
          }
          i++;
       }
       if (road_type[from] == 2)
       {
          side1[place_holder] = from;
          place_holder++;
       }
       side1[place_holder] = 9;
    }
          
    
      
    /* Function to get a list of of the roads on either side of a particular route. */
    void get_side2(int *side2, int num_roads, int *road_type, int from, int to)
    {
       int i = from;
       int place_holder = 0;
       while(i != to)
       {
          if(i != num_roads - 1)
          {
             side2[place_holder] = i;
             place_holder++;
          }
          else if(i == 0)
          {
             side2[place_holder] = i;
             place_holder++;
             i = num_roads - 1;
          }
          i--;
       }
       if (road_type[to] == 2)
       {
          side2[place_holder] = to;
          place_holder++;
       }
       side2[place_holder] = 9;
    }


    I have cut out most of the code from the program, sorry there is still so much to read i am not sure what parts are involved in the error.

    The error is...
    in main: passing arg 4 of 'route_check' from incompatible pointer type.

    and before i managed to create that error i got....
    error subscripted value is neither array nor pointer
    for the parts in the route_check fuction where i used route_list[j][1] or route_list[j][0]

    I haven't finished writing route_check yet, that why it don't make much/any sence. Was just trying to iron out any errors at this stage.


    Any help on these errors would be great.

    Thanks again,
    -Nick

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    You are passing route_list to route_check() and route_list is declared as:
    Code:
    int route_list[num_routes][2];
    However, route_check() takes the type:
    Code:
    int route_list[][1]
    These are not interchangeable.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    26
    *sigh* it is always a stupid mistake. Thanks a lot for your help.

    -Nick

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble passing args to functions in other files
    By Midnight Coder in forum C Programming
    Replies: 6
    Last Post: 01-03-2009, 05:13 PM
  2. Passing Multidementional arrays
    By kingneb in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2008, 04:24 PM
  3. Replies: 7
    Last Post: 06-04-2008, 11:39 PM
  4. Having Trouble Passing typedef Arrays to a Function
    By jlharrison in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 12:06 PM
  5. passing arrays to functions
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 03-01-2002, 03:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21