2
1

More than 5 years have passed since last update.

cで関数ポインタ

Last updated at Posted at 2018-12-21

概要

cで関数ポインタやってみた。

サンプルコード

#include <stdio.h>

void number(int i)
{
    printf ("%d\n", i);
}
void fizz(int i)
{
    printf ("fizz\n");
}
void buzz(int i)
{
    printf ("buzz\n");
}
void fizzbuzz(int i)
{
    printf ("fizzbuzz\n");
}
int main()
{
    int i;
    void (* funcp[]) (int) = {
        number,
        fizz,
        buzz, 
        fizzbuzz
    };
    for (i = 1; i < 100; i++) 
    {
        (* funcp[!(i % 3) + !(i % 5) * 2]) (i);
    }
    return 0;
}

成果物

以上。

2
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1