char *strinv(const char *s); //that's the given prototype
I'm a bit insecure about the *strinv part. Does it mean that the function is automatically dereferenced when called? Or that the function is defined as a pointer?
Thanks in advance for clarification.
This function declaration
char * strinv(const char *s);
declares a function that has the return type char *
. For example the function can allocate dynamically memory for a string and return pointer to that string.
Here is a demonstrative program that shows how the function for example can be defined.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * strinv(const char *s)
{
size_t n = strlen( s );
char *t = malloc( n + 1 );
if ( t != NULL )
{
size_t i = 0;
for ( ; i != n; i++ ) t[i] = s[n-i-1];
t[i] = '\0';
}
return t;
}
int main(void)
{
const char *s = "Hello Worlds!";
char *t = strinv( s );
puts( t );
free( t );
return 0;
}
The program output is
!sdlroW olleH
A declaration of a pointer to the function can look the foolowing way
char * ( *fp )( const char * ) = strinv;
To dereference the pointer and call the pointed function you can write
( *fp )( s );
though it is enough to write
fp( s );