Thread: convert char array to int array

  1. #1
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37

    convert char array to int array

    What is the best method to do this? This is my attempt, which failed by the way .

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 5
    
    int main (int argc, char *argv[])
    {
    int i;
    char core[MAX];
    int coreint[MAX];
    strcpy (core, argv[1]);
    
            for(i =0; i < MAX-1; i++)
                    coreint[i] = atoi(core[i]);
    
    printf("%d\n", coreint[1] + coreint[2]);
    return EXIT_SUCCESS;
    }
    I'm trying to convert a command argument to a set of integers, eg ./program 417 causes

    coreint[0] = 4
    coreint[1] = 1
    coreint[2] = 7

    which are all integers.

    Any assistance is appreciated.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    atoi expects a char pointer, you are giving it a char. Even if you use &core[i], there will be a problem since the null terminator ('\0') will be after 7, so when i=0, &core[i]="417", when i=1, &core[i]="17", when i=2, &core[i]="7".

    You don't need atoi. C characters are ASCII values, eg. "a"=97. "0"=48 and "9"=57, so to convert a single char to an integer you can use:
    Code:
    coreint[i]=core[i]-'0';
    You don't need to strcpy arvg[1] either, you can use argv[1][i].
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37
    Thanks, so would argv[1][i]; turn the string into an integer, I'm not entirely sure what that does. I modified the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 5
    
    int main (int argc, char *argv[])
    {
    int i;
    char core[MAX];
    int coreint[MAX];
    core = argv[1][i];
    
            for(i =0; i < MAX-1; i++)
                    coreint[i]=core[i]-'0';
    
    printf("%d\n", coreint[1] + coreint[2]);
    return EXIT_SUCCESS;
    }
    But the compiler refuses to set core = argv[1][i]; Lastly, what is the -0 for in coreint[i]=core[i]-'0';?

    Thanks again!

  4. #4
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    alternatively you can use arthematic manipulations to get a number digit by digit...

    first you find the length and save the argument as number
    Code:
    int coreint,len,divisor=1;
    len=strlen(argv[1]);
    coreint=atoi(argv[1]);
    then you use the following piece of code to extract each digit
    Code:
        for(x=0;x<len-1;x++)
            divisor*=10;  
        while(divisor!=0)
        {
            digit=coreint/divisor;
            printf("%d\n",digit);
            coreint%=divisor;
            divisor/=10;
        }
    Last edited by creeping death; 03-25-2009 at 11:11 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM