qfedu-c-level/day10/homework/h15.c

37 lines
906 B
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 设计函数接收一个数值类型的5行3列的二维数组和查找的数值返回数值在二维数组的序号。
// 【提示】第一个元素的序号为0 第二行第一个元素的序号为3
#include <stdio.h>
int find(int (*arr)[3], int num)
{
int *p = arr[0]; // arr[0] == &arr[0][0]
// int *p = &arr[0][0]; // arr[0] == &arr[0][0]
int n = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
// if (arr[i][j] == num)
if (*(p + i * 3 + j) == num) // arr[i][j] == *(p+i*3+j)
{
return n;
}
n++; // 序号加1
}
}
return -1; // 没找到
}
int main()
{
int a[5][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 9, 9},
{10, 11, 12},
{13, 14, 15}};
printf("%d\n", find(a, 9)); // 输出结果7
return 0;
}