I', learning C and I'm getting no output for some reason, probably I don't return as I should but how I should? (described the problem in the comments below :D) Any help is appreciated!
#include <stdio.h>
#include <ctype.h>
#include <string.h>
char *makeUpperCase (char *string);
int main()
{
printf(makeUpperCase("hello")); //Here there is no output, and when I'm trying with the format %s it returns null
return 0;
}
char *makeUpperCase(char *string)
{
char str_out[strlen(string) + 1];
for (int i = 0; i < strlen(string); ++i)
str_out[i] = toupper(string[i]);
printf(str_out); //Here I get the output.
return str_out;
}
You declared within the function a local variable length array that will not be alive after exiting the function
So your program has undefined behavior.
If the function parameter declared without the qualifier
constthen it means that the function changes the passed string in place. Such a function can be defined the following wayOtherwise you need to allocate dynamically a new string. For example
Here is a demonstration program.
The program output is