debugging - 6-bit binary counter in C (compiles, but then Windows encounters error) -


i extremely new c , trying make 6-bit binary counter, each return has 6 digits listed (i.e 000000, 000001,...). currently, solution compiles not execute once compiled (i warning says effect of "a problem caused windows stop working" , no output displayed). if figure out why happens, or suggest better way since know approach extremely convoluted, i'd appreciate help!

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdint.h>  long * convert(long dec){     if(dec == 0){        return 0;     }else{        return(long *)(dec % 2 + (10 * *convert(dec / 2)));     }  }  char* long_enough(char* num){     char* have_one = "0000";     char* have_two = "0000";     char* have_three = "000";     char* have_four = "00";     char* have_five = "0";        if(strlen(num) == 2){        strcat(have_one, num);     }else if(strlen(num) == 3){        strcat(have_two, num);     }else if(strlen(num) == 4){        strcat(have_three, num);     }else if(strlen(num) == 5){        strcat(have_four, num);     }else if(strlen(num) == 6){        strcat(have_five, num);     }  }        char main(){     int i;     int count = -1;     printf("\n");     for(i = 0; < 5; i++){        count++;        long* binnum = (long *)(convert(count));        char* new;        char done = sprintf(new, "%d", binnum);        long_enough((char *)(intptr_t)done);         printf("%s\n", long_enough((char *)(intptr_t)done));            }  }  

are using long* point nowhere? makes no sense. pointer meant point memmory region , memory region must have been allocated (either stack or heap).

it makes no sense multiplying binary number 10 either.

you need clarify thought.

when making "binary counter", think first means. given code posted, looks should split problem in 2 parts:

  1. count in binary.
  2. show value in human readable manner.

once you've splitted problem in two, i'll 1 , let other on own.

convert number binary:

well, computer numbers binary construction. let's suppose need, didactic reason, reinvent wheel in such way can address individual values.

you have 2 options: use array or use bit-mask.

by using array you'll waste more memory printing result easier.

by using bit-mask have allocate single integer (or char since need 6 bits) , shift left while testing original number.

// example using bit-masks / bit-shifts uchar. unsigned char to_bin6( unsigned int number ) {     unsigned char bin6 = 0;      // left align , clear out bits since care lowest 6.     number <<= ( 8 * sizeof( unsigned int ) ) - 6;      ( int count = 6; count; --count )         bin6 <<= number;      return bin6; } 

now arrays.

// example using char array. // array needs have additional element eos (end of string) marker. void to_bin6( unsigned int number, unsigned char bin6[ 7 ] ) {      // fills output buffer in same direction you'd expect read.     ( int count = 5; count >= 0; --count )         bin6[ count ] = ( number & ( 1 << count ) ) ?                         '1' : '0';    // feeds character '1' or '0' according bit value.     bin6[ 6 ] = '\0'; // eos: note not same '0'. } 

show number binary human reading:

that's part i'll leave you.

hint: using array easy.

tell me if need more.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -