+ 3
It is recommended to use `int` for `main` function return type, details here https://en.cppreference.com/w/c/language/main_function
Your main function return type is `int`, not `char` ~ (code edited).
The errors came from incorrect parameter type for `name` function definition and forward declaration. Change type of parameter <t> from `char` to `char*` to get the code to run.
(Edited)
+ 1
Verstappen,
A string in C is `char` array, and variable <b> in `main` is a string (`char` array). When passing a string as argument for a function, `char*` is used.
But if the variable is just a `char` (not `char` array), then it is safe to define argument type as `char`.
Passing variable as argument to function (by using pointer) also means the changes to the argument will take direct effect, and reflects in function caller scope. So changes to argument <t> in `name` function will reflect in `main` function (caller).
BTW, last time I read your code the `main` function had `int` as return type, now it is changed to `char`. Just change the return type back to `int` to get rid of the warning.
+ 1
Verstappen,
I meant to say that any changes made to the argument <t> inside `name` function will reflect in actual variable <b> in `main` function, because they are one thing under different name (<b> in `main`, <t> in `name`).
As changes to arguments passed by pointer are directly effective, it's at your discretion whether or not to return the changed argument.
+ 1
Verstappen,
Look at this little snippet. Here in changes() <number> parameter is readable and writable, but in no_changes() <number> parameter is read-only, and will trigger error cause we try to change the data referred by the pointer.
#include <stdio.h>
void changes( int *number )
{
*number *= 2;
}
void no_changes( int const *number )
{
*number *= 2; // Error: can't change const argument
}
int main()
{
int n = 5;
changes( &n ); // works okay
printf( "after changes() n = %d\n", n );
no_changes( &n ); // compile error
printf( "after no_changes() n = %d\n", n ); // not displayed cause above error
return 0;
}
+ 1
Verstappen,
Pointers is a topic big enough to be written in a book, and I am also still not fully understanding it but the basic use cases. So here a link to an e-book that might explain it better than I could ever did with my limited knowledge.
http://www.iitk.ac.in/esc101/current/Lectures/ESc101Lec25_26pointers.pdf