Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

argv and static cast not ciompiling with Unix C++

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
pcomb
Asked:
pcomb
  • 3
  • 3
1 Solution
 
TommySzalapskiCommented:
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
 
TommySzalapskiCommented:
You might need to include stdlib.h or cstdlib for atoi to work.
0
 
Neil RussellTechnical Development LeadCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
pcombAuthor Commented:
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
 
Neil RussellTechnical Development LeadCommented:
"(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
 
Neil RussellTechnical Development LeadCommented:
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
 
TommySzalapskiCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now