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?
Thanks for any help!
B
|
|
0
|
|
|
|
Reply
|
bint1668 (31)
|
12/17/2011 12:46:01 AM |
|
"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.
--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
|
|
0
|
|
|
|
Reply
|
or1 (145)
|
12/17/2011 1:13:04 AM
|
|
On Saturday, December 17, 2011 8:46:01 AM UTC+8, Bint wrote:
> 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?
>
> Thanks for any help!
> B
The size of a double is platform dependent.
Is the compiler smart enough to figure out the correct value of
points + j*3 ?
|
|
0
|
|
|
|
Reply
|
dihedral88888 (782)
|
12/17/2011 8:13:42 AM
|
|
88888 Dihedral <dihedral88888@googlemail.com> wrote:
> Is the compiler smart enough to figure out the correct value of
> points + j*3 ?
If it weren't, then the compiler would be non-standard.
What do you think "points + j*3" means?
|
|
0
|
|
|
|
Reply
|
nospam270 (2853)
|
12/17/2011 8:16:13 AM
|
|
On Saturday, December 17, 2011 4:16:13 PM UTC+8, Juha Nieminen wrote:
> 88888 Dihedral <dihedr...@googlemail.com> wrote:
> > Is the compiler smart enough to figure out the correct value of
> > points + j*3 ?
>
> If it weren't, then the compiler would be non-standard.
>
> What do you think "points + j*3" means?
points+1 means add to ponits 1 size of a double but if a double has to start at some limited address, e.g. 8 byte boundaries only , some strange thing might happen if the compiler is not smart enough
|
|
0
|
|
|
|
Reply
|
dihedral88888 (782)
|
12/17/2011 8:51:04 AM
|
|
88888 Dihedral <dihedral88888@googlemail.com> wrote:
> On Saturday, December 17, 2011 4:16:13 PM UTC+8, Juha Nieminen wrote:
>> 88888 Dihedral <dihedr...@googlemail.com> wrote:
>> > Is the compiler smart enough to figure out the correct value of
>> > points + j*3 ?
>>
>> If it weren't, then the compiler would be non-standard.
>>
>> What do you think "points + j*3" means?
>
> points+1 means add to ponits 1 size of a double but if a double has to start at some limited address, e.g. 8 byte boundaries only , some strange thing might happen if the compiler is not smart enough
If "points" is a valid pointer to an array (of at least 2 elements), then
why wouldn't "points + 1" be a valid pointer?
|
|
0
|
|
|
|
Reply
|
nospam270 (2853)
|
12/17/2011 10:23:10 AM
|
|
> "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?
Thanks
B
|
|
0
|
|
|
|
Reply
|
bint1668 (31)
|
12/17/2011 6:06:10 PM
|
|
"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.
--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
|
|
0
|
|
|
|
Reply
|
or1 (145)
|
12/17/2011 6:45:31 PM
|
|
"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.
--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
|
|
0
|
|
|
|
Reply
|
or1 (145)
|
12/17/2011 11:16:47 PM
|
|
On Dec 17, 1:46=A0am, Bint <b...@ign.com> wrote:
> 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? =A0Can I use a double pointer?
>
> Can I have something like this:
> double *points =3D (double *)malloc(10*3*sizeof(double));
>
> for int (j =3D 0; j < 10; j++) {
> =A0 =A0 =A0*(points + j*3 + 0) =3D x;
> =A0 =A0 =A0*(points + j*3 + 1) =3D y;
> =A0 =A0 =A0*(points + j*3 + 2) =3D z;
>
> }
>
> And then call the function with points as the argument? =A0It doesn't com=
pile
> but I was wondering if I could cast it? =A0Or do I need something else in=
stead
> 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:
> =A0double array[500][3];
>
> I don't want that because I don't know the number of points, see?
Problem decomposition to the rescue! ;-)
Here's a possibly better way to look at things: double pointlist [][3]
says that "Function" receives an array of double[3] (do you know of
cdecl.org? Try it, it helps with such things). So let's call that D3:
typedef double D3[3];
(More precisely, Function receives a pointer to a D3 (D3* pointlist),
but one can write that as double pointlist[][3], or D3 pointlist[];
that is, all of
void Function(double pointlist[][3]);
void Function(D3 pointlist[]);
void Function(D3* pointlist);
are one and the same as far as compiler is concerned. IMO, the third
form is best because it conveys the meaning the best, and avoids much
of mental juggling about the "feature" of C in play here, the array
decaying to a pointer when appearing in an expression.)
Now... If you decide to go for C++ route, things get easier on the
call side:
void Function(D3* pointlist) {...}
D3* p =3D new D3[10];
Function(p);
If you still want pure C, things are again a bit easier (IMO):
D3* p =3D malloc(10*sizeof(D3));
Function(p);
Finally, as others noted, "points + j*3 + 0" is wrong. You need
points[j][0]. Again, this is because "points" is a pointer to a D3,
and when you increment it, you get a pointer to the next D3 (not a
pointer to a double). And, of course, when you dereference that, you
get a D3, not double, so about that casting: no, that's the wrong
idea. Further, IMO, in pointer manipulation such as the above, a cast
indicates a bug.
Goran.
|
|
0
|
|
|
|
Reply
|
goran.pusic (299)
|
12/18/2011 7:08:30 AM
|
|
On 17 joulu, 02:46, Bint <b...@ign.com> wrote:
> And then call the function with points as the argument? =A0It doesn't com=
pile
Let's rewrite this to C++:
1. Create a point class or struct with x, y, z variables
2. Use std::vector to store them if you don't know the size of the
array
3. Pass vector's reference to a function processing the array
|
|
0
|
|
|
|
Reply
|
paulkp (164)
|
12/18/2011 8:46:36 AM
|
|
On Sat, 2011-12-17, Bint wrote:
> 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?
....
Switch to a datatype which uses std::vector, or perhaps vectors of
vectors. Much easier and safer to handle.
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
|
|
0
|
|
|
|
Reply
|
nntp24 (1550)
|
12/18/2011 5:03:09 PM
|
|
On Monday, December 19, 2011 1:03:09 AM UTC+8, Jorgen Grahn wrote:
> On Sat, 2011-12-17, Bint wrote:
> > 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?
> ...
>
> Switch to a datatype which uses std::vector, or perhaps vectors of
> vectors. Much easier and safer to handle.
>
> /Jorgen
But that means slower most of the time. There's no free lunch.
I prefer to re-size my own pointer manually at certain situations only
but this not for a novice.
|
|
0
|
|
|
|
Reply
|
dihedral88888 (782)
|
12/18/2011 6:04:24 PM
|
|
"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
|
|
0
|
|
|
|
Reply
|
jt68 (1134)
|
12/18/2011 10:07:42 PM
|
|
Krice <paulkp@mbnet.fi> wrote:
> On 17 joulu, 02:46, Bint <b...@ign.com> wrote:
> > And then call the function with points as the argument? It doesn't compile
> Let's rewrite this to C++:
> 1. Create a point class or struct with x, y, z variables
> 2. Use std::vector to store them if you don't know the size of the
> array
> 3. Pass vector's reference to a function processing the array
Fine as long as the function receiving the array of points isn't
some external function from a library written in C that can't be
simply made to work with C++ classes and vectors...
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
|
|
0
|
|
|
|
Reply
|
jt68 (1134)
|
12/18/2011 10:14:21 PM
|
|
"Jens Thoms Toerring" <jt@toerring.de> wrote in message
news:9l76heFm6gU1@mid.uni-berlin.de...
> "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;
> }
Yes I was thinking of suggesting this to the OP. It's much nicer this way.
>
> 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...
This was also what I thought. I guess the OP has simplified the function
and the 'actual' lib function will also have a size parameter. Or maybe the
function has access to the runtime size parameter by some other channel,
IDK.
--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
|
|
0
|
|
|
|
Reply
|
or1 (145)
|
12/18/2011 10:34:09 PM
|
|
On Sun, 2011-12-18, Jens Thoms Toerring wrote:
> Krice <paulkp@mbnet.fi> wrote:
>> On 17 joulu, 02:46, Bint <b...@ign.com> wrote:
>> > And then call the function with points as the argument? �It doesn't compile
>
>> Let's rewrite this to C++:
>
>> 1. Create a point class or struct with x, y, z variables
>> 2. Use std::vector to store them if you don't know the size of the
>> array
>> 3. Pass vector's reference to a function processing the array
>
> Fine as long as the function receiving the array of points isn't
> some external function from a library written in C that can't be
> simply made to work with C++ classes and vectors...
Surely we cannot assume that is the case? He /did/ post to
comp.lang.c++ and said nothing about such limitations as far as I can
tell.
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
|
|
0
|
|
|
|
Reply
|
nntp24 (1550)
|
12/19/2011 12:01:30 PM
|
|
I would have answered, use
double **points = (double **)malloc(10*3*sizeof(double));
, to illustrate the principles involved, but I know better.
Building a type up with successive typedefs is also an old C technique
and here it illustrates the principles, too, no quarrel.
On Sat, 17 Dec 2011 23:08:30 -0800 (PST), Goran
<goran.pusic@gmail.com> wrote:
>Problem decomposition to the rescue! ;-)
>
>typedef double D3[3];
|
|
0
|
|
|
|
Reply
|
heckja_in (15)
|
1/5/2012 2:59:43 PM
|
|
|
17 Replies
40 Views
(page loaded in 0.223 seconds)
Similiar Articles: Simple error : method format(String, Object[]) is not applicable ...I know this is a simple question, so any help would be very much appreciated. ... templates and const > member functions(and arguments) as ... Simple error : method format ... inserting class objects into maps - comp.lang.c++.moderated ...The term 'class' when used as a template argument is a bit of a misnomer, since ... I know this is a simple question, so any help would be very much appreciated. How to separate command line args - comp.lang.rexxI want to pass multiple arguments to a REXX script via a commandline. There is a ... Found: e f -end How many REXX programmers does it take to answer a simple question? Simple XML processing in AWK - comp.lang.awkThere seem to be a stream of questions about doing some simple XML scanning in AWK. ... ssh and passing second arguments through awk - comp.unix.shell ... ... Comparison of a Simple Join done by EG and Hand Coded - 22 times ...HOW IS A SIMPLE JOIN DONE USING EG QUERY AND FILTER ... Finally, this question is trying to get my attention, so ... The modern versus old technology argument is constantly ... Passing model file as a string parameter in cadence ocean script ...The foreach loop would then deal with each section, simple as that. Assuming I understood your question ... ... function is a normal function that evaluates all its arguments ... Undefined function or method 'No' for input arguments of type ...Hi, I am writing a simple program for my lab and I ... Undefined function or method...for input arguments of type 'char' programming.itags.org: Matlab question ... x86-64 and calling conventions - comp.compilersOr, in general, there are all sorts of ugly questions wrt the topic of ... even on Linux), which is basically about the same (it being fairly simple to dump arguments ... Simple Ping in 'C' - comp.unix.programmerssh and passing second arguments through awk - comp.unix.shell ... ... I have a second question (assumed to be ... Simple XML processing in AWK - comp.lang ... intent(out) for pointer dummy argument - comp.lang.fortran ...Simple changes like this aren't going to fix that. That's why it ... of any good reason to allow "intent(out)" for a pointer > argument? If I understand your question ... Axes Label Question - comp.soft-sys.matlabHi, So this should be simple for someone to answer ... solution is to call imagesc with the axes arguments, so ... One last question: the y axis has its values inverted ... pass arguments in bash - comp.unix.programmer... Post Question | Groups ... stuck with a problem which is about passing arguments between scripts. Please look at below, there are two simple ... Simple XY Plot - comp.lang.java.guiPlot 2D - with third argument (colorbar) - comp.soft-sys.matlab ... simple xy plot using MFC: xy, mfc, plot, using simple xy plot code using MFC? ... This question has been ... OpenBoot Prom / 'go' Question - comp.unix.solaris... this is in the wrong group, but I saw a similar question ... Unix sysadmin and it seems like its just a simple ... For argument's sake lets assume the following; 10.10.7 ... simple pthread example - comp.unix.programmer... Post Question | Groups ... derived from templates. - comp ..... and arguments) as ... not being found ... loadlibrary not working on a simple ... A Simple Argument Against AbortionIt comes down to just one question. Let me borrow an example from Gregory Koukl to show you what I mean. A Simple Argument Against Abortion – Page 3 of 8 What is a clear, simple example of inductive reasoning? - Yahoo ...Weak argument are the contrary( in simple terms, they are what judgemental people are known ... allows a small comment, for a full answer pls open a new question ... 7/23/2012 3:34:04 PM
|