Thread: copying argv

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    59

    copying argv

    Hello ,
    I want to copy argv as strings , I don,t know the size of the arguments .
    example .
    these are command line arguments
    Code:
    mr james and john .
    mr james , isaac .
    I don,t know if it is possible copying the address of the names( james ,john and isaac )

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Of course it is... the argv array is just an array of strings... strlen() will tell you how long they are, strcpy() will make copies... or (although non-standard) _strdup() will allocate memory and hand you a pointer to a copy.

    However; there is no need to copy these strings unless you are planning to modify them. They are variables, passed into a function, just like any other variable, passed into a function...

    Try this to learn more...
    Code:
    #include <stdio.h>
    
    int main (int argc, char* argv[])
      { int i;
    
        for (i = 0; i < argc; i++)
           printf("%s (%d)\n",argv[i],strlen(argv[i]));
    
        return 0; }
    This will let you see how your parameters transfer from the command line to your argv string array.
    Last edited by CommonTater; 10-05-2011 at 11:01 PM. Reason: added #include ....

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int main( int argv, char **argc )
    {
        char *dest = NULL;
        int x;
        for( x = 0; x < argv; x++ )
        {
            dest = malloc( strlen( argc[ x ] ) + 1 );
            if( dest )
            {
                strcpy( dest, argc[ x ] );
                printf( "argc[ %d ] is %s\n", x, dest );
                free( dest );
            }
        }
        return 0;
    }
    Looks about right.


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

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    Hi, Thanks for your reply.
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int main( int argv, char **argc )
    {
        char *dest = NULL;
        int x;
        for( x = 0; x < argv; x++ )
        {
            dest = malloc( strlen( argc[ x ] ) + 1 );
            if( dest )
            {
                strcpy( dest, argc[ x ] );
                printf( "argc[ %d ] is %s\n", x, dest );
                free( dest );
            }
        }
        return 0;
    }
    Can you please explain this a little bit to me .
    Code:
    dest = malloc( strlen( argc[ x ] ) + 1 );
    Is malloc allocating memory locating memory enough for one arquments or can this memory allocated used to store three arquments.
    thanks
    or is it better to allocate memory to store maximum number of chars.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quzah's code is demonstrating how to allocate memory to copy a string... the malloc call is allocating a memory block big enough to hold each string in the array in turn, copying the string, printing it and then freeing the memory. If you want to copy the entire argv array, you need to make dest into an array of pointers to strings (char *dest[argc])

    However, as demonstrated in my post #2 there's no need for all that since argc gives you the number of strings, and argv is already an array of strings, no magic, just a perfectly ordinary array of strings. There's no need to copy them unless you intend to change them...

    Try the code in post #2 ... you'll see how it works.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    Thanks , I want to copy many argv , i.e i want to copy the names
    Code:
    mr james and john .
    mr james , isaac .
    the names (james, john , isaac )
    my problem is that I don,t know the size of what I am copying .
    I need to allocate memory big enough to hold many strings.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's impossible to do what you want apparently.


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

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    Ok.
    I gat it , I thank I need to count what am trying to copy , then allocate memory for that .
    I think that will solve my problem .

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    Hello can I get a help on this please, The program complied and ran , it worked and outputed something on the first file , but I get errors saying
    [code]
    Program received signal EXC_BAD_ACCESS, Could not access memory.
    Code:
    int main(int argc , char **argv[]) {
        
        int i = 0 ;
        int sir_count = 0; // for counting sir and sirs
        for(i = 0; i < argc; i++) {
            
            if( strcmp( argv[i] , "Sir") == 0 ) {  // counting sir 
                ++sir_count;
            }
            if( strcmp( argv[i] , "Sirs") == 0 ) { // counting sirs
                 ++sir_count;
                 if( strcmp( argv[i+2] , "and") == 0 ) {
                     ++sir_count;
                 }
                 if( strcmp( argv[i+1][ strlen( argv[i+1] )-1 ] , "," ) == 0  ) {
                     ++sir_count;
                     
                     if( !strcmp( argv[i+3] , "and") ==0 ) {
                     ++sir_count;
                     }
                 }
             }
             
        }
    
        printf("The number of Sir/Sirs in the file is : %d ", sir_count );
        return EXIT_SUCCESS;       
    }

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    I get errors on the second file.

  11. #11
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by code_lover View Post
    I get errors on the second file.
    Code:
    argv[i+2]
    You cannot do this (i+2) inside your loop! i will count up to the number of elements in the array argv, so when you put i+2 in there you will try to access elements beyond the size of your array (memory which you don't have access to) at the end of the loop.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You are exceeding the bounds of your array on your array, argv, on line 12, 15, and 18 strcmp checks.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    If I can't do argv[i+1] , how do I get access to the next argv if I can,t wait for the statement to return to the loop.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can do it, so long as you make sure i+1 is less than argc.


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

  15. #15
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    You can do it, so long as you make sure i+1 is less than argc.
    I don,t understand . passing argv[i+2] , lets say i at that moment is 2 , then argv[i+2] is still the same with argv[4]
    Please clear me on this ,Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. argv[]
    By rakeshkool27 in forum C Programming
    Replies: 13
    Last Post: 04-18-2010, 10:36 AM
  2. main() ; argv[1] = string at argv[0]???
    By UCnLA in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 12:16 AM
  3. Question on *argv vs. *argv[]
    By movl0x1 in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 06:03 PM
  4. argv
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 03-04-2002, 06:41 PM
  5. argv
    By GaPe in forum C Programming
    Replies: 7
    Last Post: 01-07-2002, 04:25 PM