Solved

argv and static cast not ciompiling with Unix C++

Posted on 2014-09-30
7
241 Views
Last Modified: 2014-09-30
int main(int argc, char argv[])

xx = static_cast<int>(argv[1]);


this compiles in visual studio but fails on argv error = "unix second argument of int main should be char *argv
"

If I change to *argv then stat_cast lines fails with
invalid static_cast from type char to type int
0
Comment
Question by:pcomb
  • 3
  • 3
7 Comments
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 40353060
argv should be an array of character arrays (because it is usually a list of options or other words).

So you want (int argc, char* argv[]) or similar

What exactly are you trying to do? Do you want
xx = static_cast<int>(argv[1][0]); to pull the first character out of the first argument or something like
xx = atoi(argv[1]); to convert the first argument to a number
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 40353071
You might need to include stdlib.h or cstdlib for atoi to work.
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40353133
It would be interesting to know what it is you are trying to achieve here.
I am sure that there are neater ways to get where you are trying to go.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:pcomb
ID: 40353140
am reading in a number of integer values and characters eg
./prog 1 2 3 4 +

if i use (int argc, char* argv[])  it does not cmpile in linux invalid static_cast from type char to type int as per my initial explanaton

thanks
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40353154
"(int argc, char* argv[]) " is Perfectly correct.
However...
"xx = static_cast<int>(argv[1]);" is not, that is the error.  As Tommy already said, you should use xx = atoi(argv[1]); for the first arg etc..
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40353165
int main  (int argc, char* argv[])
{
int xx;
if (argc > 1)
    {
      for (count = 1; count < argc; count++)
      {
        printf("argv[%d] = %s\n", count, argv[count]);
          xx = atoi(argv[count]);
        printf("argv[%d] = %d\n", count, xx);
      }
    }
}
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 40353272
atoi will fail on characters. Are they always single characters like 1, 2, +, A, or can they be multiple, like 34, +:-, hi, ...?

If you are just hacking something simple together and want to only use single characters, then use static_cast<int>(argv[1][0]) to just pull the first character off of the argument.

If you want arbitrary length things, then why are you casting to int if it can be characters?
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Before You Read The Article Please make sure you understand these two concepts: Variable Scope (http://www.php.net/manual/en/language.variables.scope.php) and Property Visibility (http://www.php.net/manual/en/language.oop5.visibility.php).  And to …
The Fluent Interface Design Pattern You can use the Fluent Interface (http://en.wikipedia.org/wiki/Fluent_interface) design pattern to make your PHP code easier to read and maintain.  "Fluent Interface" is an object-oriented design pattern that r…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 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

23 Experts available now in Live!

Get 1:1 Help Now