I wrote a function in C that transforms all lowercase letters into uppercase with pointers. It looks like the following:
char *ft_strupcase(char *str)
{
char *str_begin;
str_begin = str;
while (*str != '\0')
{
if (*str >= 'a' && *str <= 'z')
{
*str = *str - 32;
}
str++;
}
return (str_begin);
}
However, if I test with the following main function, it will give a bus error:
#include "ft_strupcase.c"
int main(void)
{
printf("The string \"I am your father\" is now %s\n", ft_strupcase("I am your father"));
return (0);
}
The error message looks like the following:
/bin/sh: line 1: 79346 Bus error: 10
But if I create a string variable to store the string, there is no bus error. For example, if I test it with the following main function:
#include "ft_strupcase.c"
int main(void)
{
char test_string[50] = "I am your father";
printf("The string \"I am your father\" is now %s\n", ft_strupcase(test_string));
return (0);
}
It works fine, and the output is:
The string "I am your father" is now I AM YOUR FATHER
I don't really understand how the first test differs from the second test. Can anyone explain to me why this happens?