Find the answer to your Linux question:
Results 1 to 1 of 1
// I want to work with command line arguments // This is the code thats giving 'Segmentation Fault': int main(int argc, char * argv[ ] ) { char * userName; ...
  1. #1
    Just Joined!
    Join Date
    Oct 2009
    Posts
    1

    Post [SOLVED] Segment Fault

    // I want to work with command line arguments //

    This is the code thats giving 'Segmentation Fault':

    int main(int argc, char * argv[ ] )
    {


    char * userName;
    strcpy(userName, argv[2] );


    char * objName;
    strcpy(objName, argv[3] )

    }


    I understand here I attempt to point a read only memory blocks of argv, which was not allowed.

    But I don't know how to make my way around. I want to copy the command line arguments (all the strings of argv ) to different variables (here I tried userName and objName pointers).

    I don't want to create static arrays for storing them.
    I even tried character-by-character copy... given in following code:

    char * userName="";
    char * objName="";

    for(int i=2; i<=3; i++)
    {
    for(int j=0; j< strlen( argv[ i ] ); j++)
    objName[ j ] = argv[ i ][ j ];

    }

    But even this does not work

    Please help !
    Last edited by namerohit; 10-04-2009 at 07:02 AM. Reason: To format text for better legibility

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...