functions arguments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Liberum

    #1

    functions arguments

    Hi,

    I want to pass a pointer to a function that will change from float**
    ptr to int** ptr during a series of calls and back again.
    In the declaration: int f(float** prt);
    I have to write float but I want it to be flexible here.
    There is the trivial solution of sending in two pointers but I wonder
    if there is a better way?

    /Sheldon

  • Eric Sosman

    #2
    Re: functions arguments



    Liberum wrote On 10/31/06 11:42,:
    Hi,
    >
    I want to pass a pointer to a function that will change from float**
    ptr to int** ptr during a series of calls and back again.
    In the declaration: int f(float** prt);
    I have to write float but I want it to be flexible here.
    There is the trivial solution of sending in two pointers but I wonder
    if there is a better way?
    What are you trying to do? A pointer will not "change"
    from one type to another; every expression in C has a type
    that is determined at compile time and never changes. It
    is possible to convert a pointer value of one type to a
    pointer value of a different type, subject to constraints,
    but it's not clear that's what you need -- in particular,
    the double indirection seems likely to run afoul of some
    of those pesky constraints.

    So, again: What are you trying to do? Try to explain
    the ultimate objective, not the immediate low-level task.

    --
    Eric.Sosman@sun .com

    Comment

    • Sheldon

      #3
      Re: functions arguments


      Eric Sosman skrev:
      Liberum wrote On 10/31/06 11:42,:
      Hi,

      I want to pass a pointer to a function that will change from float**
      ptr to int** ptr during a series of calls and back again.
      In the declaration: int f(float** prt);
      I have to write float but I want it to be flexible here.
      There is the trivial solution of sending in two pointers but I wonder
      if there is a better way?
      >
      What are you trying to do? A pointer will not "change"
      from one type to another; every expression in C has a type
      that is determined at compile time and never changes. It
      is possible to convert a pointer value of one type to a
      pointer value of a different type, subject to constraints,
      but it's not clear that's what you need -- in particular,
      the double indirection seems likely to run afoul of some
      of those pesky constraints.
      >
      So, again: What are you trying to do? Try to explain
      the ultimate objective, not the immediate low-level task.
      >
      --
      Eric.Sosman@sun .com
      I am extracting data of different types, i.e., float, int, and these
      are stored in hdf files. The original function had the target array
      sent in as a pointer of type float, but not all the data are stored as
      floats. Instead of creating another function for int datatypes I would
      like to modify the original function to handle this problem.

      /Sheldon

      Comment

      • Frederick Gotham

        #4
        Re: functions arguments

        Liberum:
        I want to pass a pointer to a function that will change from float**
        ptr to int** ptr during a series of calls and back again.
        In the declaration: int f(float** prt);
        I have to write float but I want it to be flexible here.
        There is the trivial solution of sending in two pointers but I wonder
        if there is a better way?

        Not quite sure what you're trying to do. Maybe you could do:

        typedef enum Type { Int, Float } Type;

        void SetToFive(Type const t,void *const p)
        {
        switch (t)
        {
        case Int: *(int*)p = 5; return;
        case Float: *(float*)p = 5; return;
        }
        }

        Or maybe even:

        typedef enum Type { Int, Float } Type;

        typedef union PtrToIntOrFloat {
        int *pi;
        float *pf;
        } PtrToIntOrFloat ;

        void SetToFive(Type const t,PtrToIntOrFlo at const p)
        {
        switch (t)
        {
        case Int: *p.pi = 5; return;
        case Float: *p.pf = 5; return;
        }
        }

        --

        Frederick Gotham

        Comment

        • Eric Sosman

          #5
          Re: functions arguments



          Sheldon wrote On 10/31/06 13:08,:
          Eric Sosman skrev:
          >
          >
          >>Liberum wrote On 10/31/06 11:42,:
          >>
          >>>Hi,
          >>>
          >>>I want to pass a pointer to a function that will change from float**
          >>>ptr to int** ptr during a series of calls and back again.
          >>>In the declaration: int f(float** prt);
          >>>I have to write float but I want it to be flexible here.
          >>>There is the trivial solution of sending in two pointers but I wonder
          >>>if there is a better way?
          >>
          > What are you trying to do? A pointer will not "change"
          >>from one type to another; every expression in C has a type
          >>that is determined at compile time and never changes. It
          >>is possible to convert a pointer value of one type to a
          >>pointer value of a different type, subject to constraints,
          >>but it's not clear that's what you need -- in particular,
          >>the double indirection seems likely to run afoul of some
          >>of those pesky constraints.
          >>
          > So, again: What are you trying to do? Try to explain
          >>the ultimate objective, not the immediate low-level task.
          >>
          >>--
          >>Eric.Sosman@s un.com
          >
          >
          I am extracting data of different types, i.e., float, int, and these
          are stored in hdf files. The original function had the target array
          sent in as a pointer of type float, but not all the data are stored as
          floats. Instead of creating another function for int datatypes I would
          like to modify the original function to handle this problem.
          I'm still failing to see the Big Picture. If "hdf files"
          refer to one of the Hierarchical Data Formats, why can't you
          use their (http://www.hdfgroup.org) free software to do what
          you need? (That's just a guess, of course: other candidate
          HDF's include Hubble Deep Field, Human Development Foundation,
          Housing Development Fund, Howard Douglas & Farnell Insurance,
          and the Hereditary Disease Foundation. The shortage of TLA's
          means that you're SOL trying to use them to convey sense. ;-)

          Anyhow, there seems to be a whole pile of software for
          dealing with "hdf files." What are you trying to do that's
          beyond its capabilities?

          --
          Eric.Sosman@sun .com

          Comment

          • Keith Thompson

            #6
            Re: functions arguments

            "Sheldon" <shejo284@gmail .comwrites:
            Eric Sosman skrev:
            >Liberum wrote On 10/31/06 11:42,:
            [snip]

            Please use a consistent name in your posts. You've posted in this
            thread as "Liberum" and "Sheldon"; that makes it difficult to follow
            the discussion. Thanks.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
            We must do something. This is something. Therefore, we must do this.

            Comment

            • Sheldon

              #7
              Re: functions arguments

              Eric Sosman skrev:
              Sheldon wrote On 10/31/06 13:08,:
              Eric Sosman skrev:

              >Liberum wrote On 10/31/06 11:42,:
              >
              >>Hi,
              >>
              >>I want to pass a pointer to a function that will change from float**
              >>ptr to int** ptr during a series of calls and back again.
              >>In the declaration: int f(float** prt);
              >>I have to write float but I want it to be flexible here.
              >>There is the trivial solution of sending in two pointers but I wonder
              >>if there is a better way?
              >
              What are you trying to do? A pointer will not "change"
              >from one type to another; every expression in C has a type
              >that is determined at compile time and never changes. It
              >is possible to convert a pointer value of one type to a
              >pointer value of a different type, subject to constraints,
              >but it's not clear that's what you need -- in particular,
              >the double indirection seems likely to run afoul of some
              >of those pesky constraints.
              >
              So, again: What are you trying to do? Try to explain
              >the ultimate objective, not the immediate low-level task.
              >
              >--
              >Eric.Sosman@su n.com

              I am extracting data of different types, i.e., float, int, and these
              are stored in hdf files. The original function had the target array
              sent in as a pointer of type float, but not all the data are stored as
              floats. Instead of creating another function for int datatypes I would
              like to modify the original function to handle this problem.
              >
              I'm still failing to see the Big Picture. If "hdf files"
              refer to one of the Hierarchical Data Formats, why can't you
              use their (http://www.hdfgroup.org) free software to do what
              you need? (That's just a guess, of course: other candidate
              HDF's include Hubble Deep Field, Human Development Foundation,
              Housing Development Fund, Howard Douglas & Farnell Insurance,
              and the Hereditary Disease Foundation. The shortage of TLA's
              means that you're SOL trying to use them to convey sense. ;-)
              >
              Anyhow, there seems to be a whole pile of software for
              dealing with "hdf files." What are you trying to do that's
              beyond its capabilities?
              >
              --
              Eric.Sosman@sun .com
              Thanks!

              Comment

              Working...