(C/C++ 学习笔记)函数指针语法基础

本文详细介绍了C语言中数组类型的定义与使用,以及如何通过函数指针实现函数间的灵活调用,提供了丰富的实例帮助理解核心概念。

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

#include "stdio.h"
#include "stdlib.h"
#include "string.h"


int array2[10];
//定义一个数组类型
typedef int (ArrayType)[10];

//定义一个指向数组的 数组指针类型 
typedef int (*PArrayType)[10];  // int *

void main_数组类型复习()
{
    //由数组类型定义变量
    ArrayType array; //int array2[10];
    array[0] = 1;

    {
        //用数组指针类型  去定义一个指针 
        PArrayType arrayp; //定义一个指向数组类型的指针
        arrayp = &array;
        (*arrayp)[0] = 2;
    }

    {
        //直接定义一个指向数组类型的指针
        int (*myParray)[10] =  &array;
            (*myParray)[0] = 3;
    }

    printf("hello...\n");
    system("pause");
}

//定义一个函数类型 
typedef int Func(int);

//函数名称就代表函数的入口地址 函数名称本身就是一个指针
int test(int a)
{
    return a*a;
}



void main12()
{
    //用函数类型 定义一个函数指针
    Func *myfun = NULL;
    myfun = test;

    //通过函数指针(函数的入口地址)可以指向函数体。(言外之意可以进行函数调用)
    //printf("%d \n", myfun(2));

    //对函数名取多少地址 都是一样
    myfun = &test;
    printf("%d \n", myfun(2));

    //通过通过函数指针可以执行一个函数调用

    //(*(*(test)));

    system("pause");
}

void f()
{
    printf("执行了f\n");
}
void main()
{
    //直接定义一个函数指针,并且赋值 
    void (*myf1)() = f;
    void (*myf2)() = &f;

    myf2();
    myf1();

    system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值