Solved

Why am I getting a seg fault?

Posted on 2015-02-04
7
57 Views
Last Modified: 2015-05-20
Here's the code

int main(int argc, char* argv[1])
{
char charOld = argv[4][0];
char charNew = argv[5][0];
}
~

Open in new window


I know I'm not seeing something simple. What am I over looking in terms of the seg fault??
0
Comment
Question by:cgunix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 13

Accepted Solution

by:
Mark Bullock earned 250 total points
ID: 40590398
argv is an array of char*, which point to strings.
argv[0] contains a pointer to the first string, the name or pathname of the program.
argv[0][1] contains the second character of that string.
So swap the indexes in your code, e.g. argv[0][4]
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40590400
Why aren't you checking the value of argc? How would you know how many arguments were passed to your program?
0
 
LVL 13

Expert Comment

by:Mark Bullock
ID: 40590412
argc is always >= 1 the first argument is the name or pathname of the program.
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 250 total points
ID: 40590436
If you don't pass at least 5 command line arguments to  your program, argv[5] will be undefined.

If the program name is not at least 5 characters long, argv[0][5] will be undefined.
0
 
LVL 34

Expert Comment

by:sarabande
ID: 40591617
I think Mark is right and you mixed up the array indices. the first index counts the pointers to char, the second index counts the characters in the argument string.

If you don't pass at least 5 command line arguments to  your program, argv[5] will be undefined.
it is not only undefined but argv[5] is an invalid pointer value - probably 0 - what causes the segmentation fault. when accessing the argv array elements you always should follow the advice kaufmed has given and check for the argc argument before accessing argv elements:

if (argc > 1)
{
       // now it is safe to use the argv[1] argument
       int len = strlen(argv[1];
       ....
}
if (argc > 2)
{
       // now it is safe to use the argv[2] argument
       std::cout << argv[2] << std::endl;
       ...
}
....

Open in new window


or

if (argc <= 3)
{
       std::cout << "program requires at least 3 arguments" << std::endl;
       return -1;
}
// here it is safe to access argv[1], argv[2], or argv[3]

Open in new window


Sara
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question