让我们说,我有一个函数,操纵一个二维阵列,它接收指向2D阵列从主功能作为它的参数。
现在,我想修改(假设增加10到每个元素)的二维数组中的每个元素。
我想知道如何通过二维数组遍历与给我一个指针,返回新修改后的数组的指针。
假设指针 A
包含了二维数组的起始地址。
INT add_10(INT *一)
{
INT I,J,
B〔M] [N] = {0};
对于(i = 0; I<米;我++)
为(J = 0; J&所述N; J ++)
B〔I] [J] = 10 + A [1] [J]。
}
解决方案
为int * add_10(const int的* DEST,
const int的* SRC,
const int的男,
const int的N)
{
INT *也就是说,既不= DEST;
memmove与(DEST,SRC,M * N * sizeof的(INT));
对(INT I = 0; I&≤(M * N); ++ⅰ)
*也就是说,既不++ + = 10;
返回DEST;
}
Let us say I have a function which manipulates a 2D array which receives a pointer to the 2D array from the main function as its parameter.
Now, I want to modify(assume add 10 to each element) each element of the 2D array.
I am interested in knowing about traversing through the 2D array with a single pointer given to me and return the pointer of the newly modified array.
Assume pointer a
contains the initial address of the 2D array.
int add_10(int *a)
{
int i, j,
b[M][N] = {0};
for(i = 0; i < M; i++)
for(j = 0; j < N; j++)
b[i][j] = 10 + a[i][j];
}
解决方案
int* add_10(const int *dest,
const int *src,
const int M,
const int N)
{
int *idest = dest;
memmove(dest, src, M * N * sizeof(int));
for(int i = 0; i < (M * N); ++i)
*idest++ += 10;
return dest;
}