473,378 Members | 1,407 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

pointer to two-dimensional array as argument

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;
}
Feb 14 '08 #1
6 7906
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;
}
Feb 14 '08 #2
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>
Feb 14 '08 #3
Thanks a lot! That does it!
Feb 14 '08 #4
On Feb 14, 12:32 pm, ThomasW <x.zupf...@web.dewrote:
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.
Feb 14 '08 #5
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>

Feb 14 '08 #6
On 14 Feb., 22:01, John Bode <john_b...@my-deja.comwrote:
>
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.
Feb 17 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert Street | last post by:
Hi! I'm rather new at c++ and I'm totally confused with this kind of typecasting: typedef signed char int8_t; typedef signed short int16_t; typedef struct broadcast_hdr {
3
by: Dennis Chang | last post by:
Hi all, I was reading about function pointers and came across something which intrigued me. K&R2 calls qsort (pg.119) within main as so: qsort( (void **) lineptr, 0, nlines-1, (int (*) (void...
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
26
by: Meenu | last post by:
Hi, Can two different far pointers contain two different addresses but refer to the same location in memory?
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
8
by: tfelb | last post by:
Hey group! I have 2 questions. I saw functions with char *dst = (char *)src. In that case if I remember what I've learned I assign (an) (the) address of src to dst. Right? But I can assign...
19
by: bowlderyu | last post by:
Hello, all. If a struct contains a character strings, there are two methods to define the struct, one by character array, another by character pointer. E.g, //Program for struct includeing...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.