Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Why am I getting a seg fault?

Posted on 2015-02-04
7
Medium Priority
?
59 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 14

Accepted Solution

by:
Mark Bullock earned 1000 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 14

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 1000 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 35

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

715 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