it is my first time posting here :) I've tried searching here, but failed to find an answer to my specific question. I am quite new to C, mainly used C# until now, so the whole pointers idea is not my piece of cake ..
I tried to create a function which turns an integer to a string, and I succeded until the part of returning the value .. I do not understand why inside the end of the function, the string is being printed correctly, but it is not the case at the main function. It would be appreciated if someone could take a look at the code and explain to me why it does not work (why it prints nothing at the main function).
Main Function:
int main()
{
printf("%s", int_to_string(4058));
return 0;
};
Int to String Function:
char* int_to_string(int n)
{
int len = int_length(n);
char str[len];
int count = 1;
while (n != 0)
{
char c = (n % 10) + '0';
str[len - count] = c;
count++;
n /= 10;
}
char* s = str;
printf(s); // Works perfectly, prints the number as a string of characters
return s;
};
Sub-Func - Int Length Function:
int int_length(int s)
{
int count = 0;
while(s != 0)
{
count++;
s /= 10;
}
return count;
};
Your help will be truly appreciated!