hi guys, first time posting here :cheesy: i have problems with my assignment..
Here is a piece of code inside my main function, I have to convert decimal number to binary. I take decimal numbers divide by 2, and strcat() the remaining, then use a function strrev() to reverse it to get the correct output. The output is correct, but the problem is i always got 3 funny characters at the end of my output. I really dont know why, can someone help me out please :sad:

int decimal = charToDecimal(c);
  char *bin;
 
 
  do{
   if((decimal % 2)==1){
    decimal = decimal /2; 
    bin = strcat(bin,"1");
   }else if((decimal % 2)==0){
    decimal = decimal /2;
    bin = strcat(bin,"0");
   }
  }while(decimal>0); 
 
  printf("Converting decimal to binary. Answer is: %s",  strrev(bin));

The first problem I see is you defined a pointer bin but you never created space for it. A point simply points to space, yours points nowhere so your binary characters are going somewhere you really don't want them to.

Second problem is strrev() is not a standard function. You might be better off simply printing from the end to the beginning.

Comments
Yup.

If i type 3 as input i get "11=2Þ"
if i remove the strrev() function i get "Þ2=11" :P

> char *bin;
Listen to the people - you need to allocate some memory.
You can't just declare a pointer and assume you have an infintely long string you can play with at will.
This is just some random location in memory.

Start with char bin[100] = { 0 };

The first problem I see is you defined a pointer bin but you never created space for it. A point simply points to space, yours points nowhere so your binary characters are going somewhere you really don't want them to.

ok thanks problem solved, i declare a global variable char b[8]; and make char *bin=b point to b, it works. but i am just wondering making bin points to b does nothing but it works can someone explain why ??

ok thanks problem solved, i declare a global variable char b[8]; and make char *bin=b point to b, it works. but i am just wondering making bin points to b does nothing but it works can someone explain why ??

Pointing bin to b does nothing? I though it points bin to real space...

A pointer needs to point somewhere. But when you declare a pointer using char *bin; where is it pointing to? All it is is a holder of an address but no address has been loaded.

Then when you use bin = b; the address of b is now loaded, and bin points to the first byte of b. And life is good.


Now for your specific program, an easier solution is available. If you change char *bin; to char bin[9]; your problem is solved. In the first instance, bin is a pointer and must point somewhere. In the second, bin points directly to the array, so it is used as a direct pointer without changing any of your other code. And it must be 9, not 8. Remember, a C string must end in a 0, and a byte contains 8 bits -- therefore 9 values are required.

If i type 3 as input i get "11=2Þ"
if i remove the strrev() function i get "Þ2=11" :P

You could print the string backwards with a for loop:

for(int x = strlen(s)-1; x >= 0; x --) {
    cout << s[x];
}
This article has been dead for over six months. Start a new discussion instead.