Solved

argv and static cast not ciompiling with Unix C++

Posted on 2014-09-30
7
266 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
[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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Ever wonder how to "do" object oriented programming (OOP)?
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

635 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