char array to int array

Discussion in 'C Programming' started by rajus, Mar 11, 2006.

  1. rajus

    rajus Guest

    How to convert a char array to an int array?So if

    char s[]={"1234"};
    then the int array say 'num' should have values num[0]= 1 num[1]=2 and
    so on.
     
    rajus, Mar 11, 2006
    #1
    1. Advertising

  2. rajus

    Michael Mair Guest

    rajus schrieb:
    > How to convert a char array to an int array?So if
    >
    > char s[]={"1234"};
    > then the int array say 'num' should have values num[0]= 1 num[1]=2 and
    > so on.


    Allocate sufficient storage, say with
    int *array = malloc(strlen(s) * sizeof *array);
    if (array == NULL) {
    /* Your error handling here; in its absence: */
    exit(EXIT_FAILURE);
    }
    Then iterate through s, make sure that you have a digit,
    e.g. by using isdigit(); if you find a non-digit, terminate,
    if the non-digit is not '\0', emit an error message;
    whenever you have a digit, convert this digit to the
    digit's value (e.g. using "digit - '0'") and store it in
    the appropriate element of array.

    If your implementation does not work, post your compiling
    code here and explain your problem as clear as possible.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Mar 11, 2006
    #2
    1. Advertising

  3. rajus

    Neil Guest

    rajus wrote:
    > How to convert a char array to an int array?So if
    >
    > char s[]={"1234"};
    > then the int array say 'num' should have values num[0]= 1 num[1]=2 and
    > so on.
    >

    a loop and atoi()
     
    Neil, Mar 12, 2006
    #3
  4. rajus

    Jim Smith Guest

    Neil <> writes:

    > rajus wrote:
    >> How to convert a char array to an int array?So if
    >> char s[]={"1234"};
    >> then the int array say 'num' should have values num[0]= 1 num[1]=2 and
    >> so on.
    >>

    > a loop and atoi()


    No.
     
    Jim Smith, Mar 12, 2006
    #4
  5. rajus

    rajus Guest

    Thanks! The digit-' 0' is a great way to convert the char to int and
    also the dynamic allocation.
    Thanks.
     
    rajus, Mar 12, 2006
    #5
  6. "rajus" <> writes:
    > Thanks! The digit-' 0' is a great way to convert the char to int and
    > also the dynamic allocation.
    > Thanks.


    Please read <http://cfaj.freeshell.org/google/>.

    I'm not sure whether this has been mentioned in this thread, but the
    digit-'0' trick is guaranteed to work because the C standard requires
    the digit characters '0' through '9' to be consecutive and ordered.
    Keep in mind that there is no such guarantee for letters (and there
    are character sets in use where the letters are not consecutive).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 12, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,252
    Martien Verbruggen
    Jun 27, 2003
  2. trey

    newbie: char* int and char *int

    trey, Sep 10, 2003, in forum: C Programming
    Replies:
    7
    Views:
    428
    Irrwahn Grausewitz
    Sep 10, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,713
    Old Wolf
    Jan 20, 2004
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,152
    Ian Collins
    May 9, 2006
  5. gert
    Replies:
    20
    Views:
    1,222
Loading...

Share This Page