简体   繁体   English

C function 原型:\\char *strinv(const char *s);

[英]C function prototype: \\char *strinv(const char *s);

char *strinv(const char *s); //that's the given prototype

I'm a bit insecure about the *strinv part.我对 *strinv 部分有点不安全。 Does it mean that the function is automatically dereferenced when called?这是否意味着调用时会自动取消引用 function? Or that the function is defined as a pointer?还是将 function 定义为指针?

Thanks in advance for clarification.提前感谢您的澄清。

This function declaration本 function 声明

char * strinv(const char *s);

declares a function that has the return type char * .声明一个返回类型为char *的 function 。 For example the function can allocate dynamically memory for a string and return pointer to that string.例如,function 可以为字符串动态分配 memory 并返回指向该字符串的指针。

Here is a demonstrative program that shows how the function for example can be defined.这是一个演示程序,展示了如何定义例如 function。

#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程序 output 是

!sdlroW olleH

A declaration of a pointer to the function can look the foolowing way指向 function 的指针的声明看起来很愚蠢

char * ( *fp )( const char * ) = strinv;

To dereference the pointer and call the pointed function you can write要取消引用指针并调用指向的 function 您可以编写

( *fp )( s );

though it is enough to write虽然写就够了

fp( s );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM