0

I got stuck in a univ project as follows: I was doing it before I knew the format of the input, so I started reading it with %s, and it was a char[32]. Then when the project was released, I realized I needed to read the input as int. So now I started to read it as int and now I don't want to make again all other functions I made, and they are receiving the arguments as an array of chars (char[32]). So I made a function to convert the int value to int*, because I can't return char[32]. Hence I did, on main, a simple for to pass the values in int* to char[32]. The problem is that, when I print it on main, I see exactly the same values, but when I pass this new char[32] to my functions, I get a bug now. I guess my problem is because of '\0' or something like this.

A simple demonstration is below:

int* convert_dec_to_bin(int n){
    printf("\n");
    int i, j, k;
    int *bits;
    bits = (char*)malloc(32*sizeof(int));
    for(i = 31, j = 0; i >= 0; --i){
        printf("%d", n & 1 << i ? 1 : 0);
        if(n & 1 << i){
            bits[j] = 1;
        }else{
            bits[j] = 0;
        }
        j++;
    }
    printf("\n");
    return bits;
}


int main(){


    int i, k, instructionNameInt;
    char type;
    int *bits;
    char bitsC[32];
    //char instructionBinary[32]; I was reading like this before, ignore this line
    int instructionBinary; //Now I read like this
    scanf("%d", &instructionBinary);
    bits = convert_dec_to_bin(instructionBinary); //This is a function where I pass the int decimal input to 32 bits in binary as int*.

    //Making probably the wrong conversion here, I tried to put '\0' in the end but somehow I failed
    for(k = 0; k < 32; k++){
        bitsC[k] = bits[k];
    }
    printf("\n");


    type = determine_InstructionType(bitsC);

    printf("TYPE: %c\n", type);

    instructionNameInt = determine_InstructionName(bitsC, type);

    And several other functions...

Can someone light me up how can I fix it? I spent several hours and still didn't achieve to pass this correctly to an array of chars.

Aizshing
  • 17
  • 4
  • 1
    If you want 32 binary bits plus a `'\0'`, then you need an array of 33 characters. Also, the code needs to convert the integer values `0` and `1` to the character values `'0'` and `'1'`. – user3386109 May 30 '18 at 01:06
  • You need to learn what is ascii representation of the digit and the number represented by this digit – 0___________ May 30 '18 at 01:15
  • 1
    @user3386109, I was totally distracted about the '0' and '1', it turns out that this and the 33 characters was the problem! rotfl Thanks for the help! – Aizshing May 30 '18 at 01:48
  • Possible duplicate of [Is it necessary to supply the null character when declaring an character's array?](https://stackoverflow.com/questions/23286738/is-it-necessary-to-supply-the-null-character-when-declaring-an-characters-array) – phuclv May 30 '18 at 01:58
  • 1
    Please explain `bits = (char *)malloc`... , where `bits` is an `int *` – M.M May 30 '18 at 02:37
  • `1 << 31` causes undefined behaviour (if you have 32-bit ints), you should use `uint32_t` – M.M May 30 '18 at 02:39
  • If you used to have `char[32]` before, why would you want to make it an array of integers now? – Gerhardh May 30 '18 at 07:40
  • you also shouldn't [cast the result of malloc in C](https://stackoverflow.com/q/605845/995714) – phuclv May 30 '18 at 07:47
  • @M.M totally wrong casting, I meant (int*)malloc(...). Also, thanks for the tip about casting the result of a malloc. – Aizshing Jun 04 '18 at 06:16

0 Answers0