理解铸造argv在c ++ [英] understanding casting of argv in c++

查看:120
本文介绍了理解铸造argv在c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始学习C ++。我来自python和web开发的背景,所以记住这一点。



我想写一个程序,它接受第二个参数,它是一个整数,并做的东西。但是,然后它开始行动疯狂,我意识到这是一个问题与铸造。所以我写了:

  int main(int argc,char ** argv){
int iMynum = int argv [1]);
cout<< argv [1]<< endl;
cout<< iMynum< endl;
return 0;
};

运行 myprogram 100 p>

  100 
3429646

我不明白这一点。我发现类似的问题,SO,但答案是复杂和困惑我更多。我试着这样做:

  int iMynum =(int)* argv [1] 

其结果是:

  100 
49

p>

更新



显然,我是个白痴 - int(argv [1])是python。所以答案真的帮助我明白我在做什么,但建议代码解决方案没有工作(除了我采取的atoi不推荐)。我一直在寻找并尝试这个:

  into mynum = const_cast< int>(argv [1] [0])

并且得到一个错误,说 const_cast不能将'char'转换为'int'

解决方案

你似乎试图获得一个 int char -pointer—可以从123其中只包含一个内存地址! —成一个整数。



这不是它的工作原理。 Cast是静态类型系统的工具,而不是魔术运行时数据变换器。



Stack Overflow有很多关于解析字符串(甚至是你的C字符串)成整数的问题。例如,您可以使用 std :: stringstream boost :: lexical_cast std :: strtol 或(在C ++ 11中) std :: stoi



我会留给你去研究这个话题,现在你知道你做了什么错误,接下来要找什么。






这里是我会做的:

  #include< iostream> 
#include< cassert>
#include< boost / lexical_cast.hpp>

int main(int argc,char ** argv)
{
assert(argc> = 2);
const int iMynum = boost :: lexical_cast< int>(argv [1]);

std :: cout<< argv [1]<< '\\\
';
std :: cout<< iMynum< '\\\
';
};


I recently started learning C++. I come from a background of python and web development so bear that in mind.

I wanted to write a program which takes a second argument which is an integer and does stuff. But then it started acting crazy and I realized this was a problem with casting. So I wrote this:

int main(int argc, char** argv) {
    int iMynum = int(argv[1]);
    cout << argv[1] << endl;
    cout << iMynum << endl;
    return 0;
};

running myprogram 100 results in:

100
3429646

I don't understand this. I found similar questions on SO but the answers were complicated and confused me even more. I tried doing this instead:

int iMynum = (int) *argv[1];

Which resulted in:

100
49

What is going on here?

update

Obviously, I'm an idiot - int(argv[1]) is python. So the answers really helped me understand what I was doing, but suggested code solution did not work (except for atoi which I take is not recommended). I kept looking and tried this:

into mynum = const_cast<int>(argv[1][0])

And got an error saying const_cast cannot convert 'char' to 'int'. So what should I use instead?

解决方案

You seem to be trying to obtain an int from a string like "123" by simply casting a char-pointer — which contains only a memory address! — into an integer.

That's not how it works. Casts are tools of the static type system, not magical runtime data transformers. You have to parse the string.

There are plenty of questions on Stack Overflow about parsing strings (even C-strings like yours) into integers. For example, you can use a std::stringstream, boost::lexical_cast, std::strtol or (in C++11) std::stoi.

I leave it up to you to go and research this topic, now that you know what mistake you made and what to look for next.


Here's what I'd do:

#include <iostream>
#include <cassert>
#include <boost/lexical_cast.hpp>

int main(int argc, char** argv)
{
    assert(argc >= 2);
    const int iMynum = boost::lexical_cast<int>(argv[1]);

    std::cout << argv[1] << '\n';
    std::cout << iMynum << '\n';
};

这篇关于理解铸造argv在c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆