سؤال

I'm fairly new to C and have started out writing a small library with functionality to get length of strings, reversing strings, converting binary data in char buffers to ints and shorts. Just for the sake of education and getting a better grasps of the low level functionality that are already available in string.h etc.

The problem is that the errors I'm having are random to some degree. I have 4 functions:

  • getStringLength (Gets the length of a string)
  • reverseString (Reverses a string using XOR replacement and using the getStringLength to get the length)
  • charsToUShort (Converts a char array pointer with two bytes (not counting null term) of binary data into a unsigned short)
  • charsToUInt (Converts a char array pointer with four bytes (not counting null term) of binary data into a unsigned int)

Basically a problem occurs when I test all these functions in my main function. When all functions are used the iterator in reverseString which will go from 0 to length / 2 is set as 32767. So basically when the reversal of the string is getting iterated the loop doesn't even start since the iterator is 32767. Despite that it is initialized as 0. If I only use 3 of these for functions, for example if I remove charsToUInt i my main function it all works as expected.

The main question

  • What advice do you have to solve a problem like this?
  • All other advices are also very welcome!

Error prone code for clarification

getStringLength:

unsigned int getStringLength(char *str){
    unsigned int i = 0;

    while(str[i]){
        i++;
    }

    return i;
}

reverseString:

void reverseString(char *str){
    int i, m = 0;
    unsigned int l = getStringLength(str);

    m = l >> 1;

    while(i < m){
        str[i] ^= str[l - 1];
        str[l - 1] ^= str[i];
        str[i] ^= str[l - 1];
        i++;
        l--;
    }
}

charsToUShort:

unsigned short charsToUShort(char *str){
    unsigned int l = getStringLength(str);
    unsigned short result = 0;

    if(l != 2){
        return 0;
    }else{
        result |= str[0] << 8;
        result |= str[1] << 0;
        return result;
    }
}

charsToUInt:

unsigned int charsToUInt(char *str){
    unsigned int l = getStringLength(str);
    unsigned int result = 0;

    if(l != 4){
        return 0;
    }else{
        result |= str[0] << 24;
        result |= str[1] << 16;
        result |= str[2] << 8;
        result |= str[3] << 0;
        return result;
    }
}

Test output for clarification

Here's the output from the testing with error result:

0: reverseString failed! Expected value: 'olleH', actual value: 'Hello'
1: charsToUShort passed! Expected value: '0x6261', actual value: '0x6261'
2: charsToUInt passed! Expected value: '0x62616364', actual value: '0x62616364'

And here's the expected result:

0: reverseString passed! Expected value: 'olleH', actual value: 'olleH'
1: charsToUShort passed! Expected value: '0x6261', actual value: '0x6261'
2: charsToUInt passed! Expected value: '0x62616364', actual value: '0x62616364'
هل كانت مفيدة؟

المحلول

Your code only sets m to zero, i is left uninitialized, you want int i = 0, m = 0;, else the loop will never execute a single iteration

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top