Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Simple argument question

Reply
Thread Tools

Re: Simple argument question

 
 
88888 Dihedral
Guest
Posts: n/a
 
      12-17-2011
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 ?
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      12-17-2011
88888 Dihedral <(E-Mail Removed)> 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?
 
Reply With Quote
 
 
 
 
88888 Dihedral
Guest
Posts: n/a
 
      12-17-2011
On Saturday, December 17, 2011 4:16:13 PM UTC+8, Juha Nieminen wrote:
> 88888 Dihedral <(E-Mail Removed)> 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

 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      12-17-2011
88888 Dihedral <(E-Mail Removed)> wrote:
> On Saturday, December 17, 2011 4:16:13 PM UTC+8, Juha Nieminen wrote:
>> 88888 Dihedral <(E-Mail Removed)> 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?
 
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
function argument dependent on another function argument? Reckoner Python 11 01-19-2009 03:31 AM
Template argument as template argument nw C++ 0 04-14-2008 01:36 PM
Function pointers, variable argument functions calling other variable-argument functions (sort of) S?ren Gammelmark C Programming 1 01-07-2005 09:41 PM
How to pass variable argument list to another function w/ variable argument list? Ben Kial C Programming 1 11-15-2004 01:51 AM
defaulting argument to previous argument Bhushit Joshipura C++ 5 12-30-2003 03:21 PM



Advertisments