函数指针

本文详细解析了C语言中复杂的指针定义,并修正了一个错误的示例程序。通过具体实例,帮助读者理解不同类型的指针及其用法。


1. Write in words the data type of the identifier involved in the following definitions.

(1float (**b)[10];
(
2float *(*b)[10];

(
3float (*b[10])();
(
4float *((*b)[10]);

(
5float (*p)(int);
(
6float (*(*p)(int,int))(int);


答案:
(1) b是一个二级指针,它指向的是一个一维数组的指针,数组的元素都是float。
(2) b是一个指针,它指向一个一维数组,数组元素都是float *。
(3) b是一个数组,b有10个元素,元素都是函数的指针,指向的函数类型是没有参数且返回值类型为float的函数。
(4) 与(2)是一样的,b是一维数组的指针。
(5) 函数指针。
(6) p是一个函数的指针,指向的函数的类型是有两个int参数并且返回一个函数指针的函数,返回的函数指针指向有一个int参数且返回int的函数。


2. Find the defects in each of the following programs.

#include <stdio.h>

int max(int x, int y)
{
    
return x > y ? x : y;
}

int main()
{
    
int max(x, y);
    
int *= &max;

    
int x = (*p)((*p)(1,2),3);

    printf(
"Among 1, 2, 3, the maximal integer is: %d \n", x);

    
return 0;
}



正确的程序如下:

#include <stdio.h>

int max(int x, int y)
{
    
return x > y ? x : y;
}

int main()
{
    
int max(intint); //Error #1
    int (*p)(intint= &max; //Error #2

    
int x = (*p)((*p)(1,2),3);

    printf(
"Among 1, 2, 3, the maximal integer is: %d \n", x);

    
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值