嵌入式学习--C语言-补充

指针函数

概念

本质上函数返回值指针类型

格式

数据类型 * 函数名参数列表)

{

函数体

return 地址//失败一般返回NULL

}

函数指针

概念:本质是指针,指向的函数

格式:

数据类型 *指针名)(参数列表)

数据类型: 与指向函数的返回值类型保持一致

参数列表:与指向函数的参数列表保持一致

基本用法

#include<stdio.h>

int add(int a,int b)
{
    return a+b;
}

int sub(int a,int b)
{
    return a-b;
}
//2.函数指针作为函数的参数,实现接口重用---》多态
int test(int (*p) (int,int))
{
    printf("%d\n",p(3,4));
    return 0;
}
int main(int argc, char const *argv[])
{
    //1.函数指针
    int (*p) (int,int);
    p=add;
    printf("%d\n",p(2,3));
    p=sub;
    printf("%d\n",p(2,3));
    //2.作为参数传参
    test(add);
    test(sub);
    return 0;
}

函数指针数组

概念

本质数组存放函数指针

格式

数据类型 *数组名[元素个数]) 参数列表)

数据类型:和指向函数的返回值类型一致

参数列表:和指向的函数的参数列表一致

例如

#include<stdio.h>
int add(int a,int b)
{
    return a+b;
}

int sub(int a,int b)
{
    return a-b;
}
int main(int argc, char const *argv[])
{
    int (*arr[2])(int,int)={add,sub};
    // arr[0]=add;arr[1]=sub;
    for(int i=0;i<2;i++)
        printf("%d\n",arr[i](5,9));

    return 0;
}


练习:

a) 一个整型数

int a;

b) 一个指向整型的指针

int *b;

c)一个指向指针的指针,它指向的指针是一个指向一个整型数

int **c;

d)一个有10个整型数的数组

int d[10];

e)一个有10个指针的数组,该指针是指向一个整型数的

int *e[10];

f)一个指向有10个整型数数组的指针

int (*f)[10]

g)一个指向函数的指针, 该函数有一个整型参数并返回一个整型数

int (*g)(int)

h)一个有10个指针的数组,该指针指向一个函数,该函数有一个整型参数并返回一个整型数

int (*h[10])(int)

1.有以下程序,其输出结果是()。

#include<stdio.h>

void swap1(int c[])

{

        int t;

        t=c[0];c[0]=c[1];c[1]=t;

}

void swap2(int c0,int c1)

{

        int t;

        t=c0;c0=c1;c1=t;

}

int main( )

{

        int a[2]={3,5},b[2]={3,5};

        swap1(a);

        swap2(b[0],b[1]);

        printf("%d %d %d %d\n",a[0],a[1],b[0],b[1]);

A)5 3 5 3 B)5 3 3 5 C)3 5 3 5     D)3 5 5 3

2.有以下程序,执行后输出结果是()。

#include <stdio.h>

void f(int a[],int i,int j)

{

        int t;

        if(i<j)

        {

                t=a[i]; a[i]=a[j];a[j]=t;

                f(a,i+1,j-1);

        }

}

main( )

{

        int i,aa[5]={1,2,3,4,5};

        f(aa,0,4);

        for(i=0;i<5;i++)

                printf("%d,",aa[i]);

        printf("\n");

}  

A)5,4,3,2,1, B)5,2,3,4,1, C)1,2,3,4,5, D)1,2,3,4,5,

3.函数调用strcat(strcpy(str1,str2),str3)的功能是()。

A)将串str1复制到串str2中后在连接到串str3之后

B)将串str1连接到串str2之后再复制到串str3之后

C)将串str2复制到串str1中后再将串str3连接到串str1之后

D)将串str2连接到串str1中后再将串str1复制到串str3中

4.有以下程序,其输出结果是()。

#include <stdio.h>

void sort(int a[],int n)

{

        int i,j,t;

        for(i=0;i<n-1;i+=2)

                for(j=i+2;j<n;j+=2)

                        if(a[i]<a[j]){t=a[i];a[i]=a[j];a[j]=t;}

}

main()

{

        int aa[10]={1,2,3,4,5,6,7,8,9,10},i;

        sort(aa,10);

        for(i=0;i<10;i++)printf("%d,",aa[i]);

        printf("\n");

}  

A)1,2,3,4,5,6,7,8,9,10,          B)10,9,8,7,6,5,4,3,2,1,

C)9,2,7,4,5,6,3,8,1,10,          D)1,10,3,8,5,6,7,4,9,2,

条件编译

根据是否定义

#define 宏名

#ifdef 宏名

        /*code1*/

#else

        /*code2*/

#endif

执行顺序:判断宏是否定义,如果定义了就编译code1,否则编译code2

根据宏值

#define 宏名

#if 宏名

        /*code1*/

#else

        /*code2*/

#endif

执行顺序:判断宏的值是否为0,如果不为0编译code1,否则编译code2

例:

防止头文件重复包含

#ifndef 宏名

#define 宏名

        /*code*/

#endif

例如:

main.c fun.c Makefile head.h

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值