Using argc and argv data

This is a discussion on Using argc and argv data within the C Programming forums, part of the General Programming Boards category; Hi fellow programmers I have been trying to figure out how to use the argc and argv data. I have ...

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

    Need help using argc and argv.

    Hi fellow programmers

    I have been trying to figure out how to use the argc and argv data. I have a main function that looks like

    Code:
    int main(int argc, char *argv[])
    {
       int num_roads = argc;
       char *road_data = &argv[1][0];
       int number_routes = num_routes(int num_roads, char *road_data);
       //other code goes here...
    But then i have a few local functions that require the integer array that is being passed in the main function, argv and argc. I can't work out what the header for the local function should be..

    Code:
    int get_num_routes(int num_roads, char *road_data)
    {
       int num_into = 0; /* Number of one way roads into the intersection */
       int num_routes = 0;
    
       for (i = 0; i < num_roads; i++)
       {
       if(road_data[i][0] == 0)
          num_into++;
       }
       printf("%3d\n", num_into);
       return 0;
    }
    and also can't work out how to call the data i need from argv and argc in this local function (that isn't close to being finished btw).

    I am having a horrible time learning about using arrays and pointers. Have spent heaps of time reading up on it but need to start learning by doing and that isn't going great. Also it is a bit after 4am and my brain has turned off.

    Hope this makes sense,

    Thanks -Nick
    Last edited by Rad_Turnip; 03-30-2006 at 09:29 AM.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    Making the assumption that your command syntax is somthing like
    program 20 33 64 23 40
    then
    argc = 6
    argv[0] = "program"
    argv[1] = "20"
    ...

    So:
    Code:
    int param;         // used to loop thru argv
    int values[20];    // to store the integers from argv
    ...
    for (param= 1;     // start after the program string
         param< argc;  // loop until end of parameters
         param++)      // next parameter
    {
        value[param-1] = atoi(argv[param]); // convert to int
    }
    Now you have an integer array created from the program's parameter list.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    26
    Thanks, that was just what i was looking for. However i have now run into an all new problem that I am having trouble dealing with.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* Prototypes of local functions */
    int get_num_routes(int num_roads, int *road_type);
    
    int main(int argc, char *argv[])
    {
    
       int num_roads = argc - 1;
       /* 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);
    
       return 0;
       }
    
    
       /* Calculate the number of routes in this particular intersection */
    int get_num_routes(int num_roads, int *road_type)
    {
       int i, j;
       int num_into = 0; /* Number of one way roads into the intersection */
       int num_outof = 0; /* Number of one way roads out of the intersection */
       int num_routes = 0;
       for (i = 0; i < num_roads; i++)
       {
          if(road_type[i] == 0)
             num_into++;
          else if (road_type[i] == 1)
             num_outof++;
       }
       /* code to calculate num_routes value goes here */
       return num_routes;
    }
    This gives the problem:

    Code:
    23: warning: passing arg 2 of `get_num_routes' makes pointer from integer without a cast
    If anyone has got any ideas about how to deal with this problem then i would be super grateful.

    Cheers people,
    -Nick

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int num_routes = get_num_routes(num_roads, *road_type);
    Remove the * from this function call. Passing the name of the array will in effect pass a pointer to the first element of the array.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    26
    Yay for Quzah

    Cheers man,
    -Nick

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help getting a grib on argc and argv
    By elwad in forum C Programming
    Replies: 10
    Last Post: 05-01-2009, 10:13 AM
  2. Converting a argc / argv construct into a va_list
    By chambece in forum C Programming
    Replies: 6
    Last Post: 07-03-2005, 04:47 PM
  3. command line arguments using argc and argv.
    By Cudarich in forum C Programming
    Replies: 2
    Last Post: 12-05-2004, 06:32 AM
  4. argc & argv
    By miryellis in forum C Programming
    Replies: 11
    Last Post: 09-19-2004, 11:59 PM
  5. how do i? re: argc - argv
    By luigi40 in forum C Programming
    Replies: 2
    Last Post: 06-11-2004, 10:17 AM

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