Closed Thread
Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Storing numbers in a character array

  1. #1
    KeilanS's Avatar
    KeilanS is offline Programming Professional
    Join Date
    Oct 2008
    Location
    Canada
    Posts
    211
    Rep Power
    15

    Storing numbers in a character array

    I'm writing a program that needs to take in a 2000 x 2000 multidimensional array.

    All the entries in the array are numbers between -1 and 100.

    So I use a nested loop to take all the numbers and store them in the array. But when I output it gives me all single characters, or it will give me the ASCII character. Not the number.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    CPD
    CPD is offline Learning Programmer
    Join Date
    Nov 2008
    Posts
    57
    Rep Power
    12

    Re: Storing numbers in a character array

    I'm writing a program that needs to take in a 2000 x 2000 multidimensional array.
    That's a lot of space. Do you have to have all of the numbers in memory at once?
    But when I output it gives me all single characters, or it will give me the ASCII character. Not the number.
    I guess you're using an array of char to hold the numbers? char is treated specially on the assumption that you want a character and not a small integer, so you need to do some casting to int when printing.

  4. #3
    KeilanS's Avatar
    KeilanS is offline Programming Professional
    Join Date
    Oct 2008
    Location
    Canada
    Posts
    211
    Rep Power
    15

    Re: Storing numbers in a character array

    I do have all the numbers in memory at once. I realize it's taking up about 4MB of memory.

    And I will try the cast thing. Thanks.

  5. #4
    KeilanS's Avatar
    KeilanS is offline Programming Professional
    Join Date
    Oct 2008
    Location
    Canada
    Posts
    211
    Rep Power
    15

    Re: Storing numbers in a character array

    Yep, that helped! I also found out another problem. The program was storing the -1 as 2 characters.

    The first one was '-' and the second was '1'. So I fixed that by inputting into an int, then storing the int as a char.

  6. #5
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Storing numbers in a character array

    A nonstandard way of fixing that would be to declare the array as an array of __int8. I'm pretty sure that's just a Microsoft extension.

    Code:
    char myData[2000][2000]; //requires typecasting
    __int8 myOtherData[2000][2000]; //no typecasting
    __int8 is signed by default.

  7. #6
    KeilanS's Avatar
    KeilanS is offline Programming Professional
    Join Date
    Oct 2008
    Location
    Canada
    Posts
    211
    Rep Power
    15

    Re: Storing numbers in a character array

    My computer prof requires us to compile and run our programs under Fedora Linux. Thanks though, I'll keep that in mind.

  8. #7
    TkTech's Avatar
    TkTech is offline The Crazy One
    Join Date
    Jun 2006
    Location
    Canada
    Posts
    1,412
    Blog Entries
    1
    Rep Power
    31

    Re: Storing numbers in a character array

    From my base/types.h file, these are guaranteed to be the proper size by C/C++

    Code:
    typedef unsigned int   u32int;
    typedef          int   s32int;
    typedef unsigned short u16int;
    typedef          short s16int;
    typedef unsigned char  u8int;
    typedef          char  s8int;

  9. #8
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Storing numbers in a character array

    C99 specifies minimum widths. These could be off.

  10. #9
    Join Date
    May 2008
    Posts
    2,126
    Blog Entries
    1
    Rep Power
    33

    Re: Storing numbers in a character array

    Check out 7.18.1.1 and 7.18.1.2 for information on the exact specifications. Although it is basically implementation defined, but required if the width exists.

    Format: intN_t and uintN_t ; where N is the width

  11. #10
    KeilanS's Avatar
    KeilanS is offline Programming Professional
    Join Date
    Oct 2008
    Location
    Canada
    Posts
    211
    Rep Power
    15

    Re: Storing numbers in a character array

    Sorry guys, these last 3 messages are over my head.

    Beyond what I've learnt in the class so far.

Closed Thread
Page 1 of 3 123 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. array of character type
    By jackson6612 in forum C and C++
    Replies: 11
    Last Post: 06-11-2011, 03:05 PM
  2. Replies: 11
    Last Post: 11-09-2010, 06:23 AM
  3. Cant display full character array
    By kramman in forum C and C++
    Replies: 1
    Last Post: 10-26-2010, 08:58 PM
  4. Storing extremely large numbers
    By ahmed in forum C and C++
    Replies: 4
    Last Post: 11-27-2009, 06:06 AM
  5. Storing characters in an array
    By John in forum C and C++
    Replies: 3
    Last Post: 03-03-2008, 10:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts