L
Luke Wu
Hello,
I'm having some problems understanding 2 dimensional arrays. My
problem relates to the following code:
#include <stdio.h>
#define M 3
#define N 3
void ZeroInitArray(int **array, int m, int n); /* prototype */
int main(void)
{
int array[M][N];
int i, j;
for(i=0; i < M; i++)
{
for(j=0; j < N; j++)
{
array[j] = i * j;
}
}
ZeroInitArray(array, M, N);
return 0;
}
void ZeroInitArray(int **array, int m, int n)
{
int i, j;
for(i=0; i < m; i++)
{
for(j=0; j < n; j++)
{
array[j] = 0;
}
}
}
===END OF CODE===
Error message: My compiler reports that I am passing in an incorrect
pointer type.
I've read the FAQ on this subject, but still no enlightenment.
for example:
int x[5];
x = &x[0]; /* {pointer to first element) */
So what doesn't this apply to two dimensional arrays?
int x[5][5];
/* to me, this two dimensional array is in fact an array of arrays..
where x[0] is an array of int [5] and so is x[1], x[2] , .. , x[5]. So
x[0] would be equivalent to a pointer to the first element of it's
array.. &x[0][0]. So x[3] would be equivalent to &x[3][0]. The larger
array (that holds the subarrays) has the name x, and it would be
equivalent to the address of it's first element &x[0][0]. If this
address is sent properly to the receiving function I used, then there
should be no problem. Why is this illegal? Any insights would be
helpful */
I'm having some problems understanding 2 dimensional arrays. My
problem relates to the following code:
#include <stdio.h>
#define M 3
#define N 3
void ZeroInitArray(int **array, int m, int n); /* prototype */
int main(void)
{
int array[M][N];
int i, j;
for(i=0; i < M; i++)
{
for(j=0; j < N; j++)
{
array[j] = i * j;
}
}
ZeroInitArray(array, M, N);
return 0;
}
void ZeroInitArray(int **array, int m, int n)
{
int i, j;
for(i=0; i < m; i++)
{
for(j=0; j < n; j++)
{
array[j] = 0;
}
}
}
===END OF CODE===
Error message: My compiler reports that I am passing in an incorrect
pointer type.
I've read the FAQ on this subject, but still no enlightenment.
it's first element.From what I understand, an array's name is equivalent to a pointer to
for example:
int x[5];
x = &x[0]; /* {pointer to first element) */
So what doesn't this apply to two dimensional arrays?
int x[5][5];
/* to me, this two dimensional array is in fact an array of arrays..
where x[0] is an array of int [5] and so is x[1], x[2] , .. , x[5]. So
x[0] would be equivalent to a pointer to the first element of it's
array.. &x[0][0]. So x[3] would be equivalent to &x[3][0]. The larger
array (that holds the subarrays) has the name x, and it would be
equivalent to the address of it's first element &x[0][0]. If this
address is sent properly to the receiving function I used, then there
should be no problem. Why is this illegal? Any insights would be
helpful */