Interesting array problem

This is a discussion on Interesting array problem within the C Programming forums, part of the General Programming Boards category; Hello, I trying to print the byte strings for two alphanumeric characters (required to store this using dynamically allocated memory) ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    Interesting array problem

    Hello, I trying to print the byte strings for two alphanumeric characters (required to store this using dynamically allocated memory) that are input by a user, using two character arrays. When I print the results, they are valid, yet the second array much to my suprise prints its binary string along with the binary string for the first character input as well! Can someone help elaborate on why this is happen (I highly suspect it has something to do with how I am managing memory). I am justing getting back into the swing of using c.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void calculateBitString(char, char[]);
    
    int main() {
        char* charPtr = (char*)malloc(2);
    
        char bitString1[8];
        char bitString2[8];
    
        printf("Please input an alphanumeric character: ");
        scanf("%c", &charPtr[0]);
        fflush(stdin);
        //printf("Please input another alphanumeric character: ");
        scanf("%c", &charPtr[1]);
    
        calculateBitString(charPtr[0], bitString1);
        calculateBitString(charPtr[1], bitString2);
    
         printf("%s\n", bitString1);
         printf("%s\n", bitString2);
         
       
       return 0;
    }
    
    void calculateBitString(char input, char string[8]){
        unsigned char temp;
        unsigned char mask =0x80;
        int i;
           for(i= 0; i < 8 ; i++){
             temp = input & mask;
             if (temp == 0)
                string[i]= '0';
             else
                string[i]='1';
             mask = mask>>1;
          }
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    You're not terminating your strings:
    Code:
    void calculateBitString(char input, char string[8]){
        unsigned char temp;
        unsigned char mask =0x80;
        int i;
           for(i= 0; i < 7 ; i++){  //Make this 7, or make string have room for 9 chars instead of 8
             temp = input & mask;
             if (temp == 0)
                string[i]= '0';
             else
                string[i]='1';
             mask = mask>>1;
          }
        string[7] = '\0'; // Terminate the string
    }
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    2
    Thanks for the help! I forgot about the null character terminator for strings.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    #1
    Code:
    char* charPtr = (char*)malloc(2);
    Casting malloc calls should be avoided in C.


    #2
    Code:
    fflush(stdin);
    This is undefined behavior. fflush is only relevant for output streams, not input streams... of course removing this then likely brings it own problem related to the reading of the second character from the input stream. There are ways of dealing with this issue without resorting to an undefined fflush call.



    #3
    Where is the call to free to accompany your malloc call?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Sorting array problem :)
    By BEST in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2009, 12:57 PM
  3. problem with qsort and array via calloc
    By Zimbobo in forum C Programming
    Replies: 2
    Last Post: 11-22-2009, 11:25 PM
  4. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  5. help w/ array problem
    By rhythm313 in forum C++ Programming
    Replies: 3
    Last Post: 11-11-2002, 11:12 PM

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