Thread: convert char array to int array

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    5

    convert char array to int array

    Hi, im having a bit of trouble with getting my string converted into an array of ints, as i am quite new to c.
    Basically the program reads in user input (which is only integers) as a string or array of chars, and i want to change it to an array of integers and print it out again. Heres what i have so far..


    Code:
    int main() {
    
      char num1[255];
      int num10[255];
      int i;
      int j;
      
      fscanf(stdin, "%s", num1);  
      j = strlen(num1);
    
      
      for (i = 0; i < j; i++){
       num1[i] == num10[i];
       printf("%d", num10[i]);
       }
       
    }
    basically this just prints out an array filled with 0's.
    any help would be much appreciated.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And where do you assign anything to num10? (Assignment is =, not ==, and the thing assigned to must be on the left.)

    You have another problem, in that while assigning a character to an integer is meaningful, it perhaps doesn't mean what you want it to mean. (For instance, '0' is 48.)

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    ok thanks, i see what your saying. i guess the next question would be, if lets say the user input was a string of 1234, how do i get to print an integer array of 1234?

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I think you want to take a look at functions like atoi(), strtol() or sscanf().

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    ok so i had a look at using atoi() and didnt get very far. here is what i did in my for loop.


    Code:
    for (i = 0; i < j; i++) {
       k = atoi(num1[i]);
       num10[i] = k;
       printf("%d", num10[i]);
     }
    i got an error on compiling that said "passing arg 1 of 'atoi' makes pointer from integer without a cast. im not exactly sure what that means and how to get around it

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    atoi does the whole thing in one shot -- you give the array num1, not a character, and get back an integer that is the whole number, not a single digit.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    ohh i see. well then i guess i was going down the wrong track, because what i really want is each of the digits split up into an array of ints. so if i get a number 200 digits long i cant store that in an int, but rather each digit in the array of ints.

    But i am still stuck on how to go about it. Any more help will much appreciated!!

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Then ignore everything cpjust said and go back to assignment. As mentioned, things won't be exactly right, but you should be able to figure out the pattern to make it work.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    wow thanks! i just took 48 from the number it converted and it printed out the original. thanks heaps

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by skeme View Post
    wow thanks! i just took 48 from the number it converted and it printed out the original. thanks heaps
    That's not portable though, since it assumes you're always on an ASCII system; plus is uses a magic number in your code.
    A better thing would be to just subtract '0', ex:
    Code:
    num10[i] = num1[i] - '0';

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    i just took 48
    try to avoid magic numbers

    int res = strChar - '0';

    is clear and readable and portable, while

    int res = strChar - 48;

    is not
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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