tips~function pointer

An simple example:

#include<stdio.h>

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

int main()
{
    int (*func)(int,int);
    func=&plus; //point to the function ‘‘plus(int,int)‘‘
    printf("the result is %d\n",(*func)(4,7));
    return 0;
}

Another example:

#include <stdio.h>
#define MAX_COLORS  256

typedef struct {
    char* name;
    int red;
    int green;
    int blue;
} Color;

Color Colors[MAX_COLORS];

void eachColor (void (*fp)(Color *c)) {
    int i;
    for (i=0; i<MAX_COLORS; i++)
        (*fp)(&Colors[i]);
}

void printColor(Color* c) {
    if (c->name)
        printf("%s = %i,%i,%i\n", c->name, c->red, c->green, c->blue);
}

int main() {
    Colors[0].name="red";
    Colors[0].red=255;
    Colors[1].name="blue";
    Colors[1].blue=255;
    Colors[2].name="black";

    eachColor(printColor);
}

For more,go to How do function pointers in C work?-Stackoverflow

时间: 2024-10-25 17:50:40

tips~function pointer的相关文章

C++, function pointer

0. Function pointers are among the most powerful tools in C.  It can be used to implement function callback in C. C++ takes a slightly different route for callbacks. 1. Function pointer // (1)define a function bool lengthCompare(const string&, const

类非静态成员的函数指针 的使用 Function pointer of a non-static member function of a class

you can get the pointer of the method, but it has to be called with an object typedef void (T::*MethodPtr) (); MethodPtr method = &T::MethodA; T *obj = new T(); obj->*method(); If you need to have non-object pointer and you want to use object then

function pointer x + y

#include <iostream> using namespace std; double add (double x, double y) { return x + y; } double calculate (double m, double n, double (*pf)(double, double)) { return (*pf)(m, n); } int main() { double sum = 0; double a, b; cin >> a >>

jquery提示信息 tips

1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 <% 5 String path = request.getC

JS生成tips小工具

效果: html代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="jquery.min.js"></script> <script src="tips.js">&l

(C/C++) Callback Function

原文: http://www.codeguru.com/cpp/cpp/cpp_mfc/callbacks/article.php/c10557/Callback-Functions-Tutorial.htm Callback Functions Tutorial Introduction If you are reading this article, you probably wonder what callback functions are. This article explains

VC6.0 The value of ESP was not properly saved across a function call 错误解决方法

调用DLL函数,出现错误 Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling con

C++ virtual table pointer - vptr

To implement virtual functions, C++ uses a special form of late binding known as the virtual table. The virtual table is a lookup table of functions used to resolve function calls in a dynamic/late binding manner. The virtual table sometimes goes by

智能指针(smart pointer)(2):unique_ptr

Unique pointer: Manages the storage of a pointer, providing a limited garbage-collection facility, with little to no overhead over built-in pointers (depending on the deleter used). These objects have the ability of taking ownership of a pointer: onc