Thread: This pointers a mystery, please help with my misery.

  1. #1
    Registered User
    Join Date
    Aug 2022
    Posts
    40

    This pointers a mystery, please help with my misery.

    Hey Guys,

    This is a crazy problem. I'm doing some testing to make sure I understand how to use the pointers to arrays before I finish the code. If I run the code and type in 00 or 11 it works correctly or at least appears to. If you type in 10, the result is 'twenty'. It's crazy!

    Code:
    //http://knking.com/books/c2/answers/c13.html
    #include <stdio.h>
    
    
    int main(){
        char *digOne[]={"twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};
        char *digTwo[]={"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
        char *digThree[]={"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
        char num[2];
        printf("Enter a two-digit number: ");
        scanf("%s", num);
    
    
        if(num[0]==48&&num[1]==48){         //if 00 is entered
            printf("%s",digTwo[0]);         //prints zero
        }
        else if(num[0]==49&&num[1]==48){    //if 10 is entered
            printf("%s",digThree[0]);       //prints twenty?   CRAZY!!!
        }
        else if(num[0]==49&&num[1]==49){    //if 11 is entered
            printf("%s",digThree[1]);       //prints eleven
        }
        return 0;
    }
    If I comment out
    Code:
    //char *digOne[]={ ...
    and then run the program, if I enter 10 again, it prints zero. What The Heck is wrong with what I wrote or with my head?

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    The code you have posted does not behave as you say, so the problem appears to be with your head.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Aug 2022
    Posts
    40
    Here's a screen capture.
    This pointers a mystery, please help with my misery.-untitled-jpg

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Do you realize that you are overflowing the bounds of the "num" array when you enter more than one character?

    Remember that the "%s" specifier retrieves a string, automatically adding the end of string character to the end of the string.

    You need an array of at least size 3 and you should specify the number of characters scanf() should try to retrieve to avoid buffer overflow.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Actually, there is a problem. num needs at least 3 characters of space since the scanf will write the two digit chars and the '\0' string terminating char. You should really give it 10 or more chars. There's not much to gain by being stingy. You can even limit the number of chars the scanf writes to it:
    Code:
    char nums[10];
    scanf("%9s", nums);  // the number here needs to be one less than the array size.
    Last edited by john.c; 12-12-2022 at 08:16 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User
    Join Date
    Aug 2022
    Posts
    40
    OMG guys. Thank you very much. So many of my bugs are stupid errors. Embarrassed. For what it's worth, I'm off of my project and reading a book as suggested by this forum...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dereferencing mystery?
    By john.c in forum C Programming
    Replies: 6
    Last Post: 06-03-2020, 03:31 PM
  2. SQRT Mystery...
    By KneeLess in forum C Programming
    Replies: 7
    Last Post: 03-23-2004, 07:49 AM
  3. MAKEINTRESOURCE mystery
    By SMurf in forum Windows Programming
    Replies: 2
    Last Post: 09-03-2003, 08:06 AM
  4. Mystery.
    By Nutshell in forum C Programming
    Replies: 1
    Last Post: 01-27-2002, 01:41 AM
  5. The great mystery
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 39
    Last Post: 08-15-2001, 08:08 PM

Tags for this Thread