Coder Social home page Coder Social logo

cproject's Introduction

CProject

C&C++基础复习

函数

函数指针:函数指针是指向函数的指针变量 虽然说是个变量,但是可以用函数指针调用函数和传递参数

指针函数的声明方式 返回值类型(*变量名)(参数类型,...)

typedef int (*fun_ptr)(int,int); //声明一个指向同样参数(int,int),返回值的为int的函数指针类型

回调函数:函数指针作为某个函数的参数

void populate_array(int* array,size_t arraySize,int(*getNextValue)(void )){
    for(size_t i=0; i<arraySize;i++){
        array[i]=getNextValue();
    }
}
// 获取随机值
int getNextValue(void){
    return rand();
}

int main() {
    int myArray[10];
    /* getNextRandomValue 不能加括号,否则无法编译,因为加上括号之后相当于传入此参数时传入了 int , 而不是函数指针*/
    populate_array(myArray,10,getNextValue);
    for(int i=0;i<10;i++){
        printf("%d ",myArray[i]);
    }
    return 0;
}

cproject's People

Contributors

captainone avatar wds1204 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.