command line arguments

This is a discussion on command line arguments within the C Programming forums, part of the General Programming Boards category; ok this deal with command line arguments is a bit weird and hard to understand. basically I got the following ...

  1. #1
    Unregistered
    Guest

    command line arguments

    ok this deal with command line arguments is a bit weird and hard to understand.


    basically I got the following code that isn't working, it's giving a
    Segmentation Fault and I don't know why

    Code:
    
      /* prints command line in reverse order */
    
      #include <stdio.h>
    
        main(int argc, char *argv[])
        {
          int i;
         for( i=argc ; i > 0; i-- )
             printf("%s\n", argv[i]);
      
          return i;
        }
    wouldn't i=argc in loop (argc -which is the total # of arguments on command line)? then decrement to 0???

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    argc is the size of the array -- like all arrays, the last element is argc - 1.

    Change to i = argc -1 and it will work.

  3. #3
    Unregistered
    Guest

    Thumbs up WORKS LIKE A CHARM !!

    Thanks V. that worked beautifully.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 01:11 PM

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