简体   繁体   English

使用atoi填充一组int

[英]Using atoi to fill an array of ints

First time asking a question on here. 第一次在这里问一个问题。 Apologies if there's already threads about this but i had a few searches and didn't quite find what i think i was looking for. 抱歉,如果已经有关于此的线索,但我有一些搜索,并没有找到我认为我正在寻找的东西。 I'm very new to C and am working through a few homework exercises for my microcontroller systems class. 我是C的新手,正在为我的微控制器系统课程做一些功课。 We're currently working through easy exercises before we get into embedded C and I'm trying to write a program that'll take a line of text consisting of 10 numbers separated by commas and fill an array of int s with it. 在进入嵌入式C之前,我们正在进行简单的练习,并且我正在尝试编写一个程序,该程序将包含由逗号分隔的10个数字组成的一行文本,并用它填充int数组。 As a hint we were told to use a substring and atoi . 作为提示,我们被告知使用substringatoi I think i'm close to getting it right but i can't get it to output my numbers properly. 我认为我接近正确,但我无法正确输出我的数字。

Also i'm not looking spoon fed answers. 我也不是在寻找勺子喂答案。 A few hints would suffice for now. 一些提示就足够了。 I'd like to try figuring it out myself before asking for the solution. 在寻求解决方案之前,我想尝试自己搞清楚。

Here is my code: 这是我的代码:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int a[10];
    char str[] = {1,2,3,4,5,6,7,8,9,10}; //contains string of numbers
    int i;
    puts("This prints out ten numbers:");

    for (i = 0; i < 10; i++)
    {
        a[i] = atoi(str);
        printf("%d", a[i]);
            //i'm guessing the problem lies in one of the above two lines
    }
    return 0;
}

This is outputting the following: 这输出如下:

This prints out ten numbers:
0000000000

Thanks to anyone that can help! 感谢任何可以提供帮助的人! Chris 克里斯

You said that you have to use a line of text separated by commas but you've actually declared a char array containing ten (binary) integers. 你说你必须使用逗号分隔的一行文本,但实际上你已经声明了一个包含十个(二进制)整数的char数组。 To get that into a string you just need to do this: 要将其转换为字符串,您只需要执行此操作:

char str[] = "1,2,3,4,5,6,7,8,9,10";

Then you'll need someway to process this string to get each number out and into your array of int . 然后,您将需要处理此字符串以将每个数字输出到您的int数组中。

First off, you should declare a string as follows: 首先,您应该声明一个string ,如下所示:

char str[] = {"1,2,3,4,5,6,7,8,9,10"};

the " made the numbers a whole string. Next, you'll need to tokenize them and using the <string.h> library which will come quite handy in this situation. "使数字成为一个完整的字符串。接下来,你需要将它们标记化并使用<string.h>库,这在这种情况下会非常方便。

Here is how you do tokenizing: 以下是您如何进行标记:

define a token buffer first: 首先定义一个令牌缓冲区:

char* token;

token = strtok(str,",");   //think of it as substring, the part of the str before the comma
for (i = 0; i < 10; i++)
{
    a[i] = atoi(token);
    printf("%d\t", a[i]);
            //i'm guessing the problem lies in one of the above two lines
    token = strtok(NULL, ","); //this line is also required for tokenizing the next element
}

Using the strtok() function, you separated the elements between the comas, and got yourself the number strings. 使用strtok()函数,您可以在coma之间分隔元素,并获得数字字符串。 Used atoi() function to convert them into integers and printed them. 使用atoi()函数将它们转换为整数并打印出来。 You can see this reference for strtok() function for better understanding. 您可以在strtok()函数中看到此参考 ,以便更好地理解。

The problem lies in how you're creating the string. 问题在于你是如何创建字符串的。
Please excuse my previous answer, I misunderstood your question: 请原谅我之前的回答,我误解了你的问题:

Simply put, the declaration should be as follows: 简而言之,声明应如下:

char str[] = "1,2,3,4,5,6,7,8,9, 10, 12";

Next, you can use strtok to separate the string into an array of strings omittied the separator (which is in your case the comma), then pass the array members to atoi 接下来,您可以使用strtok将字符串分隔为一个字符串数组,这些字符串在分隔符中显示(在您的情况下是逗号),然后将数组成员传递给atoi

Now, why is your code not working? 现在,为什么你的代码不起作用?
First, characters should be surrounded by the apostrophes or else the compiler will take the number you pass literally as the ASCII value. 首先,字符应该用撇号包围,否则编译器会将您传递的数字作为ASCII值。

Second, arrays in C like this: char str[] = {'1', '2', '3', '4', '5'}; 其次,C中的数组如下: char str[] = {'1', '2', '3', '4', '5'}; don't mean a comma separated string, these commas separate the ARRAY members, each in its own index and not as a whole string. 并不是指逗号分隔的字符串,这些逗号分隔ARRAY成员,每个成员都在自己的索引中,而不是整个字符串。

Your definition of char str[] = {1,2,3,4,5,6,7,8,9,10}; 你对char str[] = {1,2,3,4,5,6,7,8,9,10}; actually sets the values of the chars to 1 to 10. 实际上将字符的值设置为1到10。

In the ASCII-chart of characters, these are unprintable control-characters. 在字符的ASCII图表中,这些是不可打印的控制字符。 Writing '1' instead of 1 will set the value to the ASCII-value of 1, which is 0x31. 写入'1'而不是1将把值设置为ASCII值1,即0x31。

another mistake is that the commas in your definition only seperate the values in the definition, so the result is a array of chars without any seperation, so 12345678910. 另一个错误是你的定义中的逗号只是分隔定义中的值,所以结果是一个没有任何分隔的字符数组,所以12345678910。

so the correct way would be char str[] = "1,2,3,4,5,6,7,8,9,10"; 所以正确的方法是char str[] = "1,2,3,4,5,6,7,8,9,10";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM