Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointer to two-dimensional array as argument

Reply
Thread Tools

pointer to two-dimensional array as argument

 
 
ThomasW
Guest
Posts: n/a
 
      02-14-2008
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[i][1]<=y) && (y<p[j][1])) ||
((p[j][1]<=y) && (y<p[i][1]))) &&
(x < (p[j][0] - p[i][0]) * (y - p[i][1]) / (p[j][1] - p[i][1])
+ p[i][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;
}
 
Reply With Quote
 
 
 
 
monkeyflip
Guest
Posts: n/a
 
      02-14-2008
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[i][1]<=y) && (y<p[j][1])) ||
((p[j][1]<=y) && (y<p[i][1]))) &&
(x < (p[j][0] - p[i][0]) * (y - p[i][1]) / (p[j][1] - p[i][1])
+ p[i][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;
}

ThomasW wrote:
> 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[i][1]<=y) && (y<p[j][1])) ||
> ((p[j][1]<=y) && (y<p[i][1]))) &&
> (x < (p[j][0] - p[i][0]) * (y - p[i][1]) / (p[j][1] - p[i][1])
> + p[i][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;
> }

 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      02-14-2008
monkeyflip wrote:

> Below are my edits...


Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
 
Reply With Quote
 
ThomasW
Guest
Posts: n/a
 
      02-14-2008
Thanks a lot! That does it!
 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      02-14-2008
On Feb 14, 12:32 pm, ThomasW <(E-Mail Removed)> wrote:
> 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[i][1]<=y) && (y<p[j][1])) ||
> ((p[j][1]<=y) && (y<p[i][1]))) &&
> (x < (p[j][0] - p[i][0]) * (y - p[i][1]) / (p[j][1] - p[i][1])
> + p[i][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.
 
Reply With Quote
 
Antoninus Twink
Guest
Posts: n/a
 
      02-14-2008
On 14 Feb 2008 at 19:25, Default User wrote:
> monkeyflip wrote:
>
>> Below are my edits...

>
> Please don't top-post. Your replies belong following or interspersed
> with properly trimmed quotes. See the majority of other posts in the
> newsgroup, or:
><http://www.caliburn.nl/topposting.html>


Please get a life. Your replies belong in /dev/null. See
<http://davidgerard.co.uk/fsckhead.html>

 
Reply With Quote
 
ThomasW
Guest
Posts: n/a
 
      02-17-2008
On 14 Feb., 22:01, John Bode <(E-Mail Removed)> wrote:
>
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Cast a pointer to array to base class pointer to array Hansen C++ 3 04-24-2010 03:30 PM
Pointer to array of array of const pointer RSL C++ 14 02-19-2010 02:06 PM
Array of pointer and pointer of array erfan C Programming 6 01-28-2008 08:55 PM
Array of pointer Vs Pointer to Array sangeetha C Programming 9 10-09-2004 07:01 PM



Advertisments