pointer to two-dimensional array as argument

T

ThomasW

Hi all,

I'm not a very experienced C programmer and probably miss something
very basic, so please forgive me.

I modified a function that tests whether a point lies within a polygon
(from the comp.graphics.algorithms FAQ at http://www.cgafaq.info/wiki/Point_in_polygon).
Instead of two one-dimensional arrays I wanted to use one two-
dimensional array as argument. But I can't figure out how to call it
properly. Can someone help me?

Thanks a lot, and here is the code:


#include <stdio.h>

int pnpoly(int npol, long int *p[2], long int x, long int y)
{
int i, j, c = 0;
for (i = 0, j = npol-1; i < npol; j = i++) {
if ((((p[1]<=y) && (y<p[j][1])) ||
((p[j][1]<=y) && (y<p[1]))) &&
(x < (p[j][0] - p[0]) * (y - p[1]) / (p[j][1] - p[1])
+ p[0]))
c = !c;
}
return c;
}

int main()
{
long int path[4][2]={{0,0},{100,0},{100,100},{0,100}};
//what do I have to write instead of path[0]
//to get the correct pointer type?
printf("%d\n",pnpoly(4,path[0],50,50));
return 0;
}
 
M

monkeyflip

Below are my edits...

#include <stdio.h>

#define SIZE1 4
#define SIZE2 2

int pnpoly(int npol, long int p[][SIZE2], long int x, long int y)
{
int i, j, c = 0;

printf ("test = %d\n", p[2][1]);

for (i = 0, j = npol-1; i < npol; j = i++) {
if ((((p[1]<=y) && (y<p[j][1])) ||
((p[j][1]<=y) && (y<p[1]))) &&
(x < (p[j][0] - p[0]) * (y - p[1]) / (p[j][1] - p[1])
+ p[0]))
c = !c;
}
return c;

}

int main()
{
long int path[SIZE1][SIZE2]={{0,0},{100,0},{100,100},{0,100}};
//what do I have to write instead of path[0]
//to get the correct pointer type?
printf("%d\n",pnpoly(4,path,50,50));
return 0;
}
 
J

John Bode

Hi all,

I'm not a very experienced C programmer and probably miss something
very basic, so please forgive me.

I modified a function that tests whether a point lies within a polygon
(from the comp.graphics.algorithms FAQ athttp://www.cgafaq.info/wiki/Point_in_polygon).
Instead of two one-dimensional arrays I wanted to use one two-
dimensional array as argument. But I can't figure out how to call it
properly. Can someone help me?

Thanks a lot, and here is the code:

#include <stdio.h>

int pnpoly(int npol, long int *p[2], long int x, long int y)

int pnpoly(int npol, long int (*p)[2], long int x, long int y)

T *p[X] -- X-element array of pointer to T
T (*p)[X] -- pointer to X-element array of T
{
int i, j, c = 0;
for (i = 0, j = npol-1; i < npol; j = i++) {
if ((((p[1]<=y) && (y<p[j][1])) ||
((p[j][1]<=y) && (y<p[1]))) &&
(x < (p[j][0] - p[0]) * (y - p[1]) / (p[j][1] - p[1])
+ p[0]))
c = !c;
}
return c;

}

int main()
{
long int path[4][2]={{0,0},{100,0},{100,100},{0,100}};
//what do I have to write instead of path[0]
//to get the correct pointer type?
printf("%d\n",pnpoly(4,path[0],50,50));


printf("%d\n", pnpoly(4, path, 50, 50));

Remember that when an array identifier appears in most contexts, its
type is converted from "array of T" to "pointer to T", and its value
is set to point to the first element in the array.

In the case of a 2-d array, T is another array type, so what you wind
up with is a pointer to an array. So, given the definition

int a[3][4];

when the array identifier a appears in a context other than a sizeof
or address-of(&) expression, its type will be converted from "3-
element array of 4-element array of int" to "pointer to 4-element
array of int", or int (*p)[4].
return 0;

}

Hope that helps.
 
T

ThomasW

T *p[X] -- X-element array of pointer to T
T (*p)[X] -- pointer to X-element array of T


Ah, that makes it clearer to me why it works the one way, but not the
other. I'm still not really understanding what pointer points where
with those arrays, but maybe it will become clear if I reread your
post later after sleeping on it. Thanks a lot.

Remember that when an array identifier appears in most contexts, its
type is converted from "array of T" to "pointer to T", and its value
is set to point to the first element in the array.

In the case of a 2-d array, T is another array type, so what you wind
up with is a pointer to an array. So, given the definition

int a[3][4];

when the array identifier a appears in a context other than a sizeof
or address-of(&) expression, its type will be converted from "3-
element array of 4-element array of int" to "pointer to 4-element
array of int", or int (*p)[4].
return 0;

Hope that helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top