Solved

argv and static cast not ciompiling with Unix C++

Posted on 2014-09-30
7
259 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What is Waterfall Model? Waterfall model is the classic Software Development Life Cycle method practiced in software development process. As the name "waterfall" describes, this development is flowing downwards steadily like waterfall, i.e., procee…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

821 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