简体   繁体   English

将字符数组转换为C中的整数数组

[英]convert array of characters to array of integers in C

I'm passing in an argument to a C program: 我将参数传递给C程序:

program_name 1234 程序名1234

int main (int argc, char *argv[]) {

    int     length_of_input = 0;    
    char*   input           = argv[1];


    while(input[length_of_input]) {
        //convert input from array of char to int
        length_of_input++;
    }
}

I want to be able to use each digit of the argument passed into the function separately as an integer. 我希望能够将传入函数的参数的每个数字分别用作整数。 atoi(input[]) throws a compile-time error. atoi(input [])引发编译时错误。

This code doesn't compile: 该代码无法编译:

while(input[length_of_input]) {
    int temp = atoi(input[length_of_input]);
    printf("char %i: %i\n", length_of_input, temp);
    length_of_input++;
}
int i;
for (i = 0; input[i] != 0; i++){
    output[i] = input[i] - '0';
}

Seeing as this is homework you could also do 看到这是家庭作业,您也可以做

output[i] = input[i] - '0';

but be careful that input[i] is actually a digit (ie it's between '0' and '9' )! 但请注意input[i]实际上是一个数字(即它在'0''9' )!

First you have to check how much space you need to allocate for the integer array. 首先,您必须检查需要为整数数组分配多少空间。 This can be done with strlen() function or iterating trough the string and checking how many valid characters are found. 可以使用strlen()函数或遍历字符串并检查找到了多少个有效字符来完成此操作。 Then you must iterate through the string and convert every (valid) character to equivalent integer number. 然后,您必须遍历字符串,并将每个(有效)字符转换为等效的整数。 It is hard to use atoi() or scanf() family of functions here since they except array as input. 这里很难使用atoi()scanf()函数系列,因为它们将数组作为输入。 Better solution would be to write your own little converter function or snippet for the conversion. 更好的解决方案是编写自己的转换函数或代码片段。

Here is small example app which converts string to array of ints. 这是一个将字符串转换为整数数组的小型示例应用程序。 If the character is not a valid decimal digit, -1 is placed into array. 如果字符不是有效的十进制数字,则将-1放入数组中。

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

int main(int argc, char *argv[])
{
    int length, i;
    int *array;
    char *input = argv[1];

    /* check if there is input */
    if(input == NULL) return EXIT_FAILURE;

    /* check the length of the input */
    length = strlen(input);
    if(length < 1) return EXIT_FAILURE;

    /* allocate space for the int array */
    array = malloc(length * sizeof *array);
    if(array == NULL) return EXIT_FAILURE;

    /* convert string to integer array */
    for(i = 0; i < length; ++i) {
        if(input[i] >= '0' && input[i] <= '9')
            array[i] = input[i] - '0';
        else
            array[i] = -1; /* not a number */
    }

    /* print results */
    for(i = 0; i < length; ++i)
        printf("%d\n", array[i]);

    /* free the allocated memory */
    free(array);

    return EXIT_SUCCESS;
}

Also check these questions: 还要检查以下问题:

You can to test if argument is a number whith isdigit() 您可以测试参数是否为带isdigit()的数字

http://www.cplusplus.com/reference/clibrary/cctype/isdigit/ http://www.cplusplus.com/reference/clibrary/cctype/isdigit/

and use atoi function . 并使用atoi函数。

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/ http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

And be careful in use 并小心使用

char*   input           = argv[1];

copy the string from argv to input (after to use malloc), it's better. 将字符串从argv复制到输入(使用malloc之后),效果更好。

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

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