Parsing command line arguments in C

  • Thread starter Thread starter TheSourceCode
  • Start date Start date
  • Tags Tags
    Line
Click For Summary

Discussion Overview

The discussion revolves around parsing command line arguments in C, specifically focusing on a programming assignment that requires displaying a user's name and age based on provided command line inputs. Participants explore the implementation details, potential errors, and clarify the requirements of the assignment.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant shares their code and describes an issue where the program crashes if the command line arguments do not include the '-age' key.
  • Another participant requests clarification on valid argument formats, noting discrepancies in the provided code regarding the expected position of the '-age' key.
  • A participant points out that since the age is optional, the program should not assume its presence and questions the logic that checks for argument counts.
  • Clarification is provided on valid input examples, including different placements of the '-age' argument.
  • One participant warns about potential out-of-bounds access in the code when the age is not supplied, emphasizing the importance of checking array bounds before accessing elements.
  • A later reply indicates that the original poster resolved their issue, suggesting they found the problem in their code.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding the requirements and implementation of the command line parsing. There is no consensus on the correctness of the original code, but there is agreement on the need for proper bounds checking.

Contextual Notes

The discussion highlights potential limitations in the original code, particularly regarding the handling of optional arguments and the assumptions made about the number of command line inputs. There are unresolved questions about the logic used to validate input arguments.

TheSourceCode
Messages
14
Reaction score
0
Here is the given assignment:

Write a code that would
parse the command line
and display the user’s first /
last name and (if given)
his/her age. The ‘-age’ key,
if present, could be the first
or, alternatively, the third
argument and indicates that
the following parameter is
age, as in the example
shown. If the command
line arguments are not
valid, an error message to
that effect should be
displayed. (You may use
string functions as needed.)

My program works as long as the user is inputting arguments with the -age key but crashes if any other time. Could I get a hint as to what is wrong?

Code:
#include <stdio.h>
int main(int argc, char *argv[]){ 
  if(argc < 3 || argc > 5 || argc == 4)
    printf("Invalid command line arguments.\n");
  if(!strcmp(argv[2], "-age") || !strcmp(argv[4], "-age"))
    printf("Invalid command line arguments.\n");
  if(argc == 3){
    printf("Your first name is: %s\n", argv[1]);
    printf("Your last name is: %s\n", argv[2]);
  }   
  if(!strcmp(argv[1], "-age")){
    printf("Your first name is: %s\n", argv[3]);
    printf("Your last name is: %s\n", argv[4]);
    printf("Your age is: %s\n", argv[2]);
  }
  if(!strcmp(argv[3], "-age")){
    printf("Your first name is: %s\n", argv[1]);
    printf("Your last name is: %s\n", argv[2]);
    printf("Your age is: %s\n", argv[4]);
  }
  system("PAUSE");	
  return 0;
}
 
Physics news on Phys.org
Can you provide an example valid arguments? It said the age argument could be first or third, but in your program, it looks like it's the third or fifth...so I'm a little lost.
 
"if(argc < 3 || argc > 5 || argc == 4)"

You said age if given, so that means age input is not necessarily expected right?
 
Sorry, let me clarify a little. Valid input would look something like this:

Code:
program.exe John Doe -age 43
or
Code:
program.exe -age 56 Jane Doe
or
Code:
program.exe Bob Smith
Where program.exe is argument 0 and so on...
 
If the age is not suppled, your program would attempt to read or write past the end of the argv array. Read your source code and attempt to find out where.

This is something that is very important to avoid. Every time you access an array element that might not exist, check the array size (or bounds) first!
 
Thanks for the feedback everyone, I got it to work! I really should have noticed the issue before.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K