指针函数 和 函数指针 有感

本文详细介绍了C语言中使用指针函数实现字符串复制的过程,并通过代码实例展示了如何利用函数指针来调用该函数,同时讨论了在不同情况下输出结果的处理方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

#include <stdio.h>
#include <string.h>
#include <assert.h>

char *strcopy(char *str_dest , char *str_source)               //指针函数
{
        char *p;
        assert ((str_dest != NULL) && (str_source != NULL));
        p = str_dest;
        while (*str_source != '\0')
        {
                *str_dest = *str_source;
                str_source++;
                str_dest++;
        };
        return p;
}

void main(void)
{
        char *a="hello";
        char b[10];
        char *c;       

#if 1
        printf("1----a = %s\n",a);     
        char *(*function)(char *, char *);                  //函数指针,要注意了,函数指针类型要和需要调用的函数类型一样,否则会有警告
        function = strcopy;                                         //不必写地址符

        c = (*function)(b , a);                                     //函数直接写
//      c = function(b , a);

        printf ("b = %s\n", b);
        if (c != NULL)
        {
                printf ("c = %s\n", c);
        }
        else
        {
                printf ("c is NULL pointer");
        }
#endif
#if 0
        c = strcopy(b , a);
        printf ("b = %s , %x\n", b,b);
        printf ("c = %s, %x\n", c,&c);
#endif
}
             

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值