Re: Simple argument question

Discussion in 'C++' started by 88888 Dihedral, Dec 17, 2011.

  1. 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 ?
     
    88888 Dihedral, Dec 17, 2011
    #1
    1. Advertising

  2. 88888 Dihedral <> 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?
     
    Juha Nieminen, Dec 17, 2011
    #2
    1. Advertising

  3. On Saturday, December 17, 2011 4:16:13 PM UTC+8, Juha Nieminen wrote:
    > 88888 Dihedral <> 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
     
    88888 Dihedral, Dec 17, 2011
    #3
  4. 88888 Dihedral <> wrote:
    > On Saturday, December 17, 2011 4:16:13 PM UTC+8, Juha Nieminen wrote:
    >> 88888 Dihedral <> 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?
     
    Juha Nieminen, Dec 17, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bhushit Joshipura

    defaulting argument to previous argument

    Bhushit Joshipura, Dec 29, 2003, in forum: C++
    Replies:
    5
    Views:
    419
  2. Ben Kial
    Replies:
    1
    Views:
    678
    Eric Enright
    Nov 15, 2004
  3. S?ren Gammelmark
    Replies:
    1
    Views:
    1,929
    Eric Sosman
    Jan 7, 2005
  4. nw
    Replies:
    0
    Views:
    332
  5. Reckoner
    Replies:
    11
    Views:
    717
    Steven D'Aprano
    Jan 19, 2009
Loading...

Share This Page