转换'整数字符串'到整数数组 [英] Converting 'integer strings' to integer array

查看:152
本文介绍了转换'整数字符串'到整数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个整数数组到我的程序通过。有没有更好的方式来将其转换为整数?目前,我得到一个错误:可变大小的物体可能无法初始化

 为(i = 0; I< ARGC,我++)
{
    INT ARR [I] =的atoi(argv的[I]);
}


解决方案

假设 ARGC 的argv 是参数传递给主,这是不可能的的argv [0] 是要转换成整数的东西。 的argv [0] 通常包含程序的名称。

您code片断声明局部循环体的数组。你可能想要的是循环体之外定义一个数组,你要分配给循环体中的各个数组元素。

  INT ARR [ARGC]
对于(i = 1; I< ARGC,我++)
{
    改编[I] =的atoi(argv的[I]);
}

I'm trying to pass in an array of integers into my program. Is there a better way to convert it to integers? I'm currently getting an error: "Variable sized object may not be initialized"

for(i = 0; i < argc; i++)
{
    int arr[i] = atoi(argv[i]);
}

解决方案

Assuming argc and argv are the arguments passed to main, it is unlikely that argv[0] is something that you want to convert into an integer. argv[0] usually contains the name of the program.

Your code snippet is declaring an array local to the loop body. What you likely want is an array defined outside the loop body, and you want to assign to individual array elements within the loop body.

int arr[argc];
for(i = 1; i < argc; i++)
{
    arr[i] = atoi(argv[i]);
}

这篇关于转换'整数字符串'到整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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