zoukankan      html  css  js  c++  java
  • c语言的函数指针

    • 简单定义并间接调用
    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<time.h>
    void singasong()
    {
        printf("%s
    ","我爱的你啊,我爱的你,你在哪里啊,在哪里~");
    }
    
    
    void main()
    {
        void (*p)();//声明一个函数类型指针
        p = singasong;//让指针指向定义的函数
        p();//间接调用
        system("pause");
    }

    输出结果:

    • 直接调用
    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<time.h>
    int gloabalnum = 888;
    int * getnum()
    {
        return &gloabalnum;
    }
    
    void main()
    {
        time_t ts;//时间种子类型
        int a[10];
        int *q = NULL;
        srand((unsigned int) time(&ts));//随机生成时间种子
        for (int i=0; i < 10; i++)
        {
            a[i] = rand() % 100;
        }
        for (int i = 0; i < 10; i++)
        {
            printf("%d
    ",a[i]);
        }
        q = themax(a,10);
        printf("最小数是:%d
    ", *q);
        printf("另外,全局变量是%d
    ",*(getnum()));
        system("pause");
    }

    输出结果:

    • 字符串指针相互赋值
    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<time.h>
    void singasong()
    {
        printf("%s
    ","我爱的你啊,我爱的你,你在哪里啊,在哪里~");
    }
    char * mystrcpy(char * srcstr, char * targetstr)
    {
        char * laststr = NULL;
        if (srcstr == NULL)
        {
            targetstr = "哟~您这源字符串空的啊~";
        }
        else
        {
            laststr = targetstr;
            while (*srcstr != '')
            {
                *targetstr = *srcstr;
                srcstr++;
                targetstr++;
            }
            *targetstr = '';//还是很有必要,否则将跟随乱码
        }
        return laststr;
    }
    
    void main()
    {
        //函数返回值是指针
        char *z = (char*)malloc(sizeof(char));
        char *q = (char*)malloc(sizeof(char));
        z = mystrcpy("阿凡提",q);
        printf("%s
    ",z);
        system("pause");
    }

  • 相关阅读:
    基于html5拖拽api实现列表的拖拽排序
    vue组件keepAlive的使用
    阿里云OSS 服务端签名后直传之分片上传(结合element-ui的upload组件)
    element-ui(vue)upload组件的http-request方法的使用
    javascript知识梳理之数据类型
    解决nginx 出现 413:Request Entity Too Large
    git基本命令
    npm源管理
    element-ui的upload组件的clearFiles方法的调用
    vue实例的生命周期
  • 原文地址:https://www.cnblogs.com/saintdingspage/p/12019372.html
Copyright © 2011-2022 走看看