A comparison between 2 char(s) is wrong

Asked

Viewed 102 times

1

I’m manipulating a string with a loop for... which generates a new string of the same, starting and ending from a certain position (would be "Hello" in that case).

When comparing the return of the manipulation with another string containing the same characters returns false, with ==.

Has a function to return the manipulation:

char *substring(char *str, int start, int end)
{
    char sub_r[start + end];
    unsigned int i;

    for(i = start; i != end; i ++)
        sub_r[i] = str[i];

    return sub_r;
}

So the problem is in the condition of the declaration of if. You should call the function printf. Yep:

int main()
{
    if(substring("Hello World", 0, 5) == "Hello")
        printf("yep");

    return 0;
}

substring("Hello World", 0, 5) returns "Hello", with the exact size. Also, when I try to call printf with this string appear other symbols instead of "Hello", or nothing.

Something wrong?

1 answer

4


The code has some problems. The main thing is to use the operator == to compare strings. In C a string is just a string and needs a proper function to compare all of them and return whether it is larger, smaller or equal. This function is the strncmp(). In fact any language needs to do so, but some hide it in the operator, but this is another subject and I will not go into details.

Another problem of this code is trying to return a local variable. This is not possible since the content in stack may not be available anymore. You have compiler you let do, which is bad since there is great possibility of memory corruption. In the compiler I used, neither compiles.

So the solution is to pass one buffer for the function with pre-allocated memory. Then who needs the text allocates as you like and releases if necessary. It is only necessary to use the malloc(). In some cases it is possible to use a array in place of the pointer and dynamic allocation.

I could even do the allocation within the function and return this pointer, but this is usually bad because the programmer might forget that he needs to do the release. It gets an asymmetrical thing, the function allocates and another function takes care of the release.

I made him return his own buffer so that the function can be used as expression as well, but if this were never necessary (unlikely in real code), it could return nothing, since the passage of the buffer is made by a pointer, therefore it is a reference to the actual object.

Note that I have simplified the function. This function is not secure, nothing guarantees that enough memory has been allocated to fit the substring. At the moment nothing prevents the end from being less than the beginning, which would be a mistake. It would be nice to improve it to treat these things.

I understand this is an exercise, but the use of memcpy() is more appropriate in this situation than tying up on one’s own.

There was another problem adding up the end and beginning of the string. Now there’s no more, but if this was still in the code, it would be wrong, it’s the opposite, it should be a difference.

Avoid using unnecessarily flagged type.

There are small code organizations that I’ve made too.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *substring(char *str, char *buf, int start, int end) {
    for (int i = start; i < end; i++)
        buf[i] = str[i];
    return buf;
}

int main() {
    char *texto = malloc(6);
    if (strncmp(substring("Hello World", texto, 0, 5), "Hello", 5) == 0)
        printf("yep");
    free(texto);
    return 0;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

In C++ everything would be different because it has a type string "native".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.