?
Solved

argv and static cast not ciompiling with Unix C++

Posted on 2014-09-30
7
Medium Priority
?
268 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Ever wonder how to "do" object oriented programming (OOP)?
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

719 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