Solved

Why am I getting a seg fault?

Posted on 2015-02-04
7
51 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
7 Comments
 
LVL 13

Accepted Solution

by:
Mark Bullock earned 250 total points
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
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 32

Expert Comment

by:sarabande
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now