Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #12643
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Simple argument question |
| Date | 2011-12-18 22:07 +0000 |
| Organization | Freie Universitaet Berlin |
| Message-ID | <9l76heFm6gU1@mid.uni-berlin.de> (permalink) |
| References | <CB114269.B5F7%bint@ign.com> <jcgqb9$9qu$1@adenine.netfront.net> <CB123632.B739%bint@ign.com> <jcio0k$1tni$1@adenine.netfront.net> <jcj7t9$2clu$1@adenine.netfront.net> |
"Paul" <pchrist <nospam>or@yahoo.co.uk> wrote:
> "Paul or@yahoo.co.uk>" <pchrist<nospam> wrote in message
> news:jcio0k$1tni$1@adenine.netfront.net...
> >
> > "Bint" <bint@ign.com> wrote in message news:CB123632.B739%bint@ign.com...
> >>
> >>> "Bint" <bint@ign.com> wrote in message
> >>> news:CB114269.B5F7%bint@ign.com...
> >>>> Hi can someone answer a simple question involving arguments to
> >>>> functions?
> >>>>
> >>>> I need to call a library function which expects an argument like this
> >>>> Function(double pointlist[][3]);
> >>>>
> >>>> My question is how do I make one of those if I am just wanting to
> >>>> malloc
> >>>> the
> >>>> memory? Can I use a double pointer?
> >>>>
> >>>> Can I have something like this:
> >>>> double *points = (double *)malloc(10*3*sizeof(double));
> >>>>
> >>>> for int (j = 0; j < 10; j++) {
> >>>> *(points + j*3 + 0) = x;
> >>>> *(points + j*3 + 1) = y;
> >>>> *(points + j*3 + 2) = z;
> >>>> }
> >>>>
> >>>> And then call the function with points as the argument? It doesn't
> >>>> compile
> >>>> but I was wondering if I could cast it? Or do I need something else
> >>>> instead
> >>>> of a double pointer for my points?
> >>>>
> >>>> Does that make sense? I am confused about how to call the function with
> >>>> anything but something like:
> >>>> double array[500][3];
> >>>>
> >>>> I don't want that because I don't know the number of points, see?
> >>>>
> >>> Hi.
> >>> When you create 2d arrays using new you may use pointer to pointer types
> >>> ,
> >>> but this is a different type from the built in array type.
> >>> In your function argument your 2d array will decay into a pointer of
> >>> type:
> >>> double (*)[3]
> >>>
> >>> Each dimensional array is pointed to by a pointer of type one dimension
> >>> less. For example a one dimensional array of doubles is pointed to by a
> >>> double*( the same as a pointer to a single double ). A 2-d array is
> >>> pointed
> >>> to by a type T(*)[S] . a 3d array is pointed to by a type T(*)[S1][S2]
> >>> etc
> >>> etc.
> >>>
> >>> When you create your pointer with malloc you are creating a pointer of
> >>> type
> >>> double* which is a pointer to a 1d array, or a pointer to a single
> >>> double.
> >>> But the function argument needs you to pass a 2d array.
> >>>
> >>
> >> So how would I create and pass something valid to this function, say for
> >> an
> >> array of [10][3], but specifying that 10 at run-time? Do I use new
> >> instead
> >> of malloc?
> >>
> > To solve your problem you need to cast the return value from the
> > allocation function to a suitable pointer type, which is compatable with
> > the function argument.
> > IIRC malloc returns a void pointer so intead of casting it to a double*,
> > as you have done in your example, you can cast it to a double(*)[3]. This
> > pointer type would then be suitable to pass to the function:
> >
> > double (*points)[3] = (double (*)[3])malloc(10*3*sizeof(double));
> >
> > Function( points);
> >
> >
> > HTH.
> >
> Note: your for loop will need to be modified as each increment on this type
> of pointer will increment it by a complete row of 3 elelments.
I guess you mean that he should remove the multiplication of
'j' by three. But then, as an added bonus due to getting the
types right, he now could rewrite the initialization loop more
naturally as
for ( size_t j = 0; j < 10; ++j ) {
points[ j ][ 0 ] = x;
points[ j ][ 1 ] = y;
points[ j ][ 2 ] = z;
}
What I am still wondering about though is how the function re-
ceiving the 2-d array is supposed to know how many "points"
it will get - all it receives is a pointer to (the first three
coordinates of) the first point, but no information about how
many more there are...
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Simple argument question Bint <bint@ign.com> - 2011-12-16 18:46 -0600
Re: Simple argument question "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2011-12-17 01:13 +0000
Re: Simple argument question Bint <bint@ign.com> - 2011-12-17 12:06 -0600
Re: Simple argument question "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2011-12-17 18:45 +0000
Re: Simple argument question "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2011-12-17 23:16 +0000
Re: Simple argument question jt@toerring.de (Jens Thoms Toerring) - 2011-12-18 22:07 +0000
Re: Simple argument question "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2011-12-18 22:34 +0000
Re: Simple argument question 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-17 00:13 -0800
Re: Simple argument question Juha Nieminen <nospam@thanks.invalid> - 2011-12-17 08:16 +0000
Re: Simple argument question 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-17 00:51 -0800
Re: Simple argument question Juha Nieminen <nospam@thanks.invalid> - 2011-12-17 10:23 +0000
Re: Simple argument question Goran <goran.pusic@gmail.com> - 2011-12-17 23:08 -0800
Re: Simple argument question Heck <heckja_in@ordertostymieharvestersverizon.net> - 2012-01-05 09:59 -0500
Re: Simple argument question Krice <paulkp@mbnet.fi> - 2011-12-18 00:46 -0800
Re: Simple argument question jt@toerring.de (Jens Thoms Toerring) - 2011-12-18 22:14 +0000
Re: Simple argument question Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-12-19 12:01 +0000
Re: Simple argument question Tobias Müller <troplin@bluewin.ch> - 2011-12-19 17:17 +0000
Re: Simple argument question Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-12-20 00:33 +0000
Re: Simple argument question Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-12-18 17:03 +0000
Re: Simple argument question 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-18 10:04 -0800
csiph-web