Thread: Print argv array

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    86

    Print argv array

    Trying to write a program that sorts names using an algorithm. I can not get the initial array to print properly, I feel like it's something simple that I'm missing. Sorry if I butchered it worse messing with the syntax.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    void print_array(char [], int);
    int main(int argc, char * argv[]){
        
        print_array(* argv, (argc-1));
        
        getchar();
        return 0;
        
    }
    void print_array( char word[], int size){
         int i = 0;
         for( i = 0; i < size; i++){
              if(i < (size - 1)){
                   printf("%c", word[i]);
         }else{
               printf("%c\n", word[i]);
               }
         }
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    argv is an array of pointers to char*, thus, *argv is just a pointer to char, it's equivalent to *(argv + 0) which is equivalent to argv[0]. So you are calling print_array with just the first element of argv (usually the name of the program).

    We actually have a FAQ article that covers working with argv, and has an example that very nearly does what you want. Check out this link: FAQ > Accessing command line parameters/arguments - Cprogramming.com.

    EDIT:
    * Really, it's just a pointer to pointer to char, since arrays decay to pointers when passed to a function, and argv is a parameter passed to main, which is a function.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    Thanks for FAQ, I understand more clearly how they work but I didnt' see much how to pass the elements to another function. I tried setting an initialized array to char * argv[] to no avail. Or must I print the array in main for it to execute properly?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Ahh, I get what you're after. Just make the argc and argv parameters in the other function look like the ones in main, then pass argc and argv from main to that function, no need for any extra *, [], etc. Like this:
    Code:
    #include <stdio.h>
    
    void foo(int argc, char *argv[])  // looks just like the parameter to main
    {
        int i;
        for (i = 0; i < argc; i++) {  // now use them just like you would use them in main
            puts(argv[i]);
        }
    }
    
    
    int main(int argc, char *argv[])
    {
        foo(argc, argv);  // pass the variables as-is, no need for extra "adornments"
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    Resolved
    Last edited by carpeltunnel; 12-05-2012 at 05:44 PM. Reason: mini epiphany

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    So, I am having a little trouble of writing my sorting algorithm. My primary questions is to access the specific element instead of the string i.e in the word john acess the j. Do I need to pass the argv[][] or will argv[] work, and if I need to two brackets, do I need to include that in my main function paraments?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Are you trying to sort each word in argv?

    If so, then you need to pass the entire word. For example:
    Code:
    void sort(char *word, int len)
    {
        char tmp;
        // sorting algorithm loops and such
                // swap code
                tmp = word[i];
                word[i] = word[j];
                word[j] = tmp;
    }
    
    int main(int argc, char *argv[])
    {
        sort(argv[3], strlen(argv[3]));  // sort the 4th word in argv
    }
    If that's not what you want, you need to be more specific in what exactly you are trying to sort.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    I apologize for the lack of clarity. I'm attemption to sort names alphabeticalls in order and reverse order. i.e. david, john, bob would have the output

    In order:
    Bob
    David
    John

    Reverse order:
    John
    David
    Bob

    I have looked up some sorting algorithm code but am having trouble translating it to the name sort. My biggest issue is where or not I have to pass argv[][] to sort names that start with the same letter(s). For example, Andrew and Another would have to go to the [i][3] element before the sorting could be successfully completed.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Okay, so you need to sort the elements of argv. argv is essentially an array of char pointers, so you are just sorting an array of char pointers. You need something like this:
    Code:
    void sort(char *argv[], int argc)
    {
        char *tmp;
    
        // sort algorithm loops
                 tmp = argv[i];
                 argv[i] = argv[j];
                 argv[j] = tmp;
    }
    
    int main(int argc, char *argv[])
    {
        ...
        sort(argv + 1, argc - 1);  // this omits the first argument, which is the program name, and adjusts the length accordingly
        ...
    }

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    Thanks, I've been tinkering with my algorithm for some time. I can't get any desired output. I'll paste my code for some tips on obvious flaw and if anyone has any links to some help on the topic I have google quite a bit wihtout much success.
    Code:
    void sort_in_order(int argc, char * argv[]){
         int i, j;
         char * temp;
         
         for(i = 0; i < argc; i++  ){
               for(j = i+1; j < argc; j++){
                     if(strcmp(argv[j],argv[i]<0))
                     {
                      temp = argv[i];             
                      argv[i] = argv[j];             
                      argv[j] = temp;
                     }
         }
    }
    }

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    if(strcmp(argv[j],argv[i]<0))
    Double check the placement of your parenthesis on this line.

  12. #12
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    Ah, thanks but my output remains the same as the original input.

  13. #13
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
     if(strcmp(argv[j],argv[i]<0))

    To move strings, strncpy/strcpy

    Your program can not write to argv[n]

    Remember that pointers are not arrays

    Code:
    temp = argv[i];             
    argv[i] = argv[j];             
    argv[j] = temp;
    Fact - Beethoven wrote his first symphony in C

  14. #14
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    Ok, so my program is starting to move in the right direction but I've hit a stumper.
    The display is

    In order:
    andrebob
    bob
    w
    w

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    void print_array(int, char * []);
    void sort_in_order(int, char * []);
    int main(int argc, char * argv[]){ 
        
        printf("Before sorting:\n");
        print_array(argc-1, argv+1);
        
        sort_in_order (argc, argv);
        printf("In order:\n");
        print_array(argc, argv);
        
        getchar();
        return 0;
        
    }
    void print_array(int argc, char * argv[]){
         int i = 0;
         for( i = 1; i < argc; i++){
              if(i < (argc-1)){
                   printf("%s\n", argv[i]);
         }else{
               printf("%s\n", argv[i]);
               }
         }
    }
    void sort_in_order(int argc, char * argv[]){
         int i, j;
         char * temp;
         
         for(i = 0; i < argc; i++  ){
               for(j = i+1; j < argc-1; j++){
                     if(strcmp (argv[j],argv[i])<0){
                      strcpy(temp,argv[i]);             
                      strcpy(argv[i],argv[j]);             
                      strcpy(argv[j],temp);
                     }
                     }
               
         }
    }

  15. #15
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    With the input of bob, david, john, andrew if that helps with advice.

    If not, if anyone knows of some sources that have info on this topic it would be appreciated. I'm struggling to find quality source on the particular issues

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of pointers and pointer for arrays with argv
    By thames in forum C Programming
    Replies: 4
    Last Post: 12-02-2012, 10:24 AM
  2. Print a 2D Array
    By xxshankar in forum C Programming
    Replies: 3
    Last Post: 03-11-2010, 08:29 PM
  3. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  4. main() ; argv[1] = string at argv[0]???
    By UCnLA in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 12:16 AM
  5. Replies: 2
    Last Post: 03-26-2008, 04:44 PM