Why does this seemingly simple implementation of strupr not work?

0

Error implementation in GCC:

    char* strupr( char *str )
    {
    while(*str) {
        *str++ = toupper(*str);
        }
    }

Note that str is used twice in assignment .

    
asked by anonymous 02.04.2016 / 14:06

1 answer

3

There are 2 problems: adding the pointer there will generate an undefined behavior and is not returning anything, have to change the signature of the function or return something. This works:

#include <stdio.h>
#include <ctype.h>

void strupr(char *str) {
    while (*str) {
        *str = toupper(*str);
        str++;
    }
}

int main(void) {
    char texto[] = "teste";
    strupr(texto);
    printf("%s", texto);
    return 0;
}

See running on ideone .

    
02.04.2016 / 14:53