A question on variable defination

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

    #1

    A question on variable defination

    Hi, all,

    a question on the following defination:
    an external function is defined as:
    nmriopen(char *const filename, const int np1, const float step1,
    const int np2, const float step2)

    what's the difference if it is defined as:
    nmriopen(char filename, int np1, float step1,
    int np2, float step2)

    and what if use "int *const np1" instead of "const int np1"?

    Thanks!

    Jing

  • pete

    #2
    Re: A question on variable defination

    sunnylele wrote:[color=blue]
    >
    > Hi, all,
    >
    > a question on the following defination:
    > an external function is defined as:
    > nmriopen(char *const filename, const int np1, const float step1,
    > const int np2, const float step2)
    >
    > what's the difference if it is defined as:
    > nmriopen(char filename, int np1, float step1,
    > int np2, float step2)
    >
    > and what if use "int *const np1" instead of "const int np1"?[/color]

    const qualified arguments
    don't mean anything to the function caller.
    const qualified arguments
    only affect the way that the function definition must be written.

    --
    pete

    Comment

    • Eric Sosman

      #3
      Re: A question on variable defination



      sunnylele wrote On 03/31/06 13:11,:[color=blue]
      > Hi, all,
      >
      > a question on the following defination:
      > an external function is defined as:
      > nmriopen(char *const filename, const int np1, const float step1,
      > const int np2, const float step2)
      >
      > what's the difference if it is defined as:
      > nmriopen(char filename, int np1, float step1,
      > int np2, float step2)[/color]

      I assume you mean `char *filename' rather than
      `char filename'.

      The `const' keyword amounts to a promise (but a weak
      one, because it can be circumvented) that the program will
      not write to the `const'-qualified object. For your `int'
      and `float' arguments, it means that the compiler will
      object if you try to change their values within the body
      of the function. Such changes cannot affect the caller,
      of course, so the "protection " isn't worth much -- but it's
      there if you want it.

      For the pointer argument things are a little different,
      because the thing that is `const' isn't the pointer value
      itself, but the thing it points at. (In this case; it's
      possible to get the other effect instead, or in addition.)
      That *is* a matter of interest to the caller, because the
      function has promised (weakly) not to change the contents
      of the string the argument presumably points to. The
      caller can do things like

      char[] myfile = ...;
      if (nmriopen(myfil e, 1, 0.01, 42, 98.6) < 0)
      fprintf(stderr, "nmriopen failed for %s\n",
      myfile);

      .... without worrying that nmriopen() will change what's in
      myfile[] and thus make nonsense of the error message.
      [color=blue]
      > and what if use "int *const np1" instead of "const int np1"?[/color]

      A similar effect: The function promises (weakly) not to
      use the pointer `np1' to change the value that it points at.

      --
      Eric.Sosman@sun .com

      Comment

      • Eric Sosman

        #4
        Re: A question on variable defination



        Eric Sosman wrote On 03/31/06 14:08,:

        [... a bunch of nonsense he regrets ...]
        [color=blue]
        > sunnylele wrote On 03/31/06 13:11,:
        >[color=green]
        >>Hi, all,
        >>
        >>a question on the following defination:
        >>an external function is defined as:
        >>nmriopen(ch ar *const filename, const int np1, const float step1,
        >> const int np2, const float step2)
        >>
        >>what's the difference if it is defined as:
        >>nmriopen(ch ar filename, int np1, float step1,
        >> int np2, float step2)[/color]
        >
        >
        > I assume you mean `char *filename' rather than
        > `char filename'.
        >
        > [...]
        > For the pointer argument things are a little different,
        > because the thing that is `const' isn't the pointer value
        > itself, but the thing it points at. (In this case; it's
        > possible to get the other effect instead, or in addition.)[/color]

        Ahh, blagnabbit! I mis-read your declaration, and made
        a fool of myself. `char * const filename' is a pointer to
        `char', where the pointed-to `char' is modifiable but the
        pointer variable itself is not. As with the other `const'-
        qualified arguments, this is not terribly useful. (Hence
        seldom used, hence unfamiliar, hence easily mis-read by
        overconfident shoot-from-the-hip people ...)
        [color=blue][color=green]
        >>and what if use "int *const np1" instead of "const int np1"?[/color][/color]
        [color=blue]
        > A similar effect: The function promises (weakly) not to
        > use the pointer `np1' to change the value that it points at.[/color]

        Same mistake by yours truly: The pointer variable `np1'
        is unchangeable, but the integer that it points to can be
        modified.

        --
        Eric.Sosman@sun .com

        Comment

        Working...