#A1431. 指针练习:ForEach

指针练习:ForEach

题目描述

程序填空,使得输出结果为:

1,4,9,16,25,

h,e,l,l,o,!,

#include <bits/stdc++.h>
using namespace std;

void ForEach(void * a, int width, int num,_____) {// 在该行补充你的代码
    for(int i = 0; i < num; ++i)
        f((char*)a + width * i);
}

void PrintSquare(void * p) {
    int * q = (int*)p;
    int n = *q;
    cout << n * n << ",";
}

void PrintChar(void * p) {
    char * q = (char*)p;
    cout << *q << ",";
}

int main() {
    int a[5] = {1, 2, 3, 4, 5};
    char s[] = "hello!";
    ForEach(a, sizeof(int), 5, PrintSquare);
    cout << endl;
    ForEach(s, sizeof(char), 6, PrintChar);
    return 0;
}

输入格式

输出格式

1,4,9,16,25,

h,e,l,l,o,!,

1,4,9,16,25,
h,e,l,l,o,!,