user1887339
user1887339

Reputation: 25

C Pointer: char to binary value

#include <stdio.h>
#include <string.h>

int * bin(char a);

int main(void){
        char a='a';
        int k=0;
        int *binary;

        binary=bin(a);
        for(k=0; k<8; k++){
                printf("%d", *binary++); 
                printf("\n");      
        }

        return 0;
}
int *bin(char a){

        int i=0;
        int *arr;
        int output[8];
        char c=a;
        for (i = 0; i <8 ; ++i) {
                 output[8-i-1] = (a >> i) & 1;

        }
        arr=&output[0];
//              for (i = 0; i <8 ; ++i) {
//              printf("%d", output[i]);
//      }
//      printf("\n");

        return arr;
}

The ouptut should be the binary value of the char 'a'which is: 0 1 1 0 0 0 0 1

but i got this instead: 0 -1216804320 -1218095335 -1216804320 10 -1076423592 -1218208721 -1216804320

Is this a pointer problem? How do i fix it so it would print the right answer? thx!!

Upvotes: 0

Views: 1821

Answers (2)

Grijesh Chauhan
Grijesh Chauhan

Reputation: 58281

First error I can notice is Scope problem, that you are returning address of local variable output[8];

 arr=&output[0];
 return arr;

That is wrong, scope of output[] is inside bin() function only.

You should allocate memory for output[] dynamically if you want to access it outside bin() function, like:

int *output = calloc(8, sizeof(int));

even you don't need extra variable just return output from bin()

I have corrected you code like below without much change:

int *bin(char a){
        int i=0;
        int *output = calloc(8, sizeof(int));
        for (i = 0; i <8 ; ++i) {
                 output[8-i-1] = (a >> i) & 1;
        }
        return output;
}

Notice removed unused variables c and arr

additionally, don't forgot to free dynamically allocated memory explicitly:

Second (Notice) be aware of Memory Clobbering Error because you are updating binary variable by ++ in printf function.

Just simple don't free(binary); it will produce run time error because you are modifying binary variable inside printf *binary++: first save return address in a extra pointer variable then free that. Like I am doing:

 b = binary=bin(a);      
 // loop 
  printf("%d", *binary++); 
 //             ^ changing binary variable to point new memory location  
 // after loop 
 free(b);

If you free(binary) then it would be wrong because you would free a memory location that is not allocated dynamically because your are changing binary variable.

get a working code here, read comments.

Upvotes: 1

RichieHindle
RichieHindle

Reputation: 281665

You're returning a pointer to a local variable (arr), whose contents are no longer valid when the function returns. Use malloc instead:

int main(void){
        ...
        int *arr = bin(a);
        ...
        free(arr);
        return 0;
}

int *bin(char a){
        int *arr = malloc(8 * sizeof int);
        ...
        return arr;
}

Upvotes: 4

Related Questions