an unexpected result.

This is a discussion on an unexpected result. within the C Programming forums, part of the General Programming Boards category; So I have a piece of a program that creates 2D array of of characters of numbers 1 through a ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    163

    an unexpected result.

    So I have a piece of a program that creates 2D array of of characters of numbers 1 through a previously found number. Earlier in the code the total amount of numbers that needs to be cataloged is set as num_entries. I then do a basic for loop to convert integers into strings and load them into a new array(actually double pointer).

    code:
    Code:
    int num_entries = gather_num_entries();
    char *audio_nums[num_entries];
    char tempchar[num_entries][2];
    
    for(i = 0; i < num_entries; i = i + 1)
    {
            sprintf(tempchar[i], "%d", i);
            audio_nums[i] = tempchar[i];    
    }
    given a num_entries amount of 5 this should replicate this code:
    Code:
    char *audio_nums[5] = {
        "0",
        "1",
        "2",
        "3",
        "4",
        "5"
    };
    the output is really confusing me, though. It works just fine up until double digit numbers then goes off in its own little direction.

    result:
    Code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    101112131415161718
    1112131415161718
    12131415161718
    131415161718
    1415161718
    15161718
    161718
    1718
    18

    Can any of you spot something I'm missing?

    Thanks
    -System_159

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The problem is here:
    Code:
    char tempchar[num_entries][2];
    You can't fit "10" in a 2-character array (you need a '1', a '0', and a '/0'). So since %s prints until it sees a null character, it will print out all the 2-digit numbers at once.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    Hey, that fixed it. Completely forgot about the null thing.

    I knew it would be something odd like that. Now to tackle memory!

    Thanks a bunch!

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by System_159 View Post
    given a num_entries amount of 5 this should replicate this code:
    Code:
    char *audio_nums[5] = {
        "0",
        "1",
        "2",
        "3",
        "4",
        "5"
    };
    That's [6] items, not [5].
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    remember the first element in an array is numbered '0'

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    staggering insight there i know

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think you are trying to say that iMalc is wrong - which would be quite rare, and the post above is not one of those rare cases.

    But the number in brackets when declaring variables is the NUMBER OF ELEMENTS you want, which in your case is 6 - the elements are accessed as [0] .. [5].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    not saying anyone is wrong bud, was not directing any reply to iMalc, i have been to his homepages link...like i am gonna say how wrong is that guy on array elements, haha i think not

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Need help with basic calculation program.
    By StateofMind in forum C Programming
    Replies: 18
    Last Post: 03-06-2009, 12:44 AM
  3. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  4. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 04:27 AM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 05:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21