高手些帮帮忙啊!帮我编下这个指针函数题
用指针实现:有N*M个士兵排成N行M列,找出这样的士兵(即指出他的位置),他的年(原始数据,键盘输入)
程序代码:
#include <stdio.h> #include <stdlib.h> #define N 3 #define M 4 void f(char (*p)[M],char *c) { int col,row,i; char *point = &p[0][0]; for (i = 1; i <= N * M; i ++) { if (*c == *point++) { if (i % M == 0) { col = i / M; row = M; } else { col = i / M + 1; row = i % M; } } } printf("col = %d,row = %d\n",col,row); } int main() { /*初始化士兵*/ char a[N][M] = { {'a','b','c','d'}, {'e','f','g','h'}, {'i','j','k','l'} },ch; printf("Please input the name of soldiers\n"); scanf("%c",&ch); //输入要查找的士兵名字 f(a,&ch); return EXIT_SUCCESS; }