0

I'm passing in an argument to a C program:

program_name 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.

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++;
}
Casey Flynn
  • 13,654
  • 23
  • 103
  • 194
  • please be more precise what you want. do you expect an int[] containing {1,2,3,4}? Also, this smells like homework, so tag it as such – Mario The Spoon Apr 26 '11 at 21:51
  • 1
    sure it's a tiny part of a larger homework assignment, as you may have seen from other questions i've asked today. But i don't think it needs to be tagged as such since it's a trivial language question. – Casey Flynn Apr 26 '11 at 21:56

4 Answers4

5
int i;
for (i = 0; input[i] != 0; i++){
    output[i] = input[i] - '0';
}
Thebigcheeze
  • 3,408
  • 2
  • 22
  • 18
1

Seeing as this is homework you could also do

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

but be careful that input[i] is actually a digit (i.e. it's between '0' and '9')!

James
  • 9,064
  • 3
  • 31
  • 49
1

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. 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. 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.

#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:

Community
  • 1
  • 1
Athabaska Dick
  • 3,855
  • 3
  • 20
  • 22
0

You can to test if argument is a number whith isdigit()

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

and use atoi function .

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.

Cloudson
  • 65
  • 5