scope q

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

    scope q

    I have taken an extraordinary leap into the modern world by purchasing
    webspace. In addition to my private concerns, I would like to make a
    part to which others, e.g. my nieces and ex-wife, can ftp. To keep a
    hold of the reigns, I shall write a program that changes the password,
    and the brains of this prog will be in C:
    #def MIN_WORD_LENGTH 9
    #def MAX_WORD_LENGTH 15

    /* subroutine for random permutation */
    void permute(char *, int)

    /* main

    char p[] = "abcdefghijklmn opqrstuvwxyz";
    char q[] = "ABCDEFGHIJKLMN OPQRSTUVWXYZ";
    char r[] = "0123456789 ";
    void swap(char *, char *);

    end main */

    void permute(char *m , int n)
    { ; }

    void swap(char *a, char *b)
    {
    char c;
    c = *a;
    *a = *b;
    *b = c;
    }
    /* end pseudosource */
    I'm going to build a word from p, q and r. At this point, I have 2
    questions:
    1) Does the scope look right? I've got the subroutine at file scope and
    the swap at block scope. If I make a call to swap from within permute(),
    is every ISO compiler going to know what I'm talking about?
    2) Is it foolhardy of me to think that I can permute a string given its
    pointer and length, without needing any information back, hence the void
    declaration? frank
    -------
  • Barry Schwarz

    #2
    Re: scope q

    On Mon, 29 May 2006 19:53:15 -0400, Frank Silvermann
    <invalid@invali d.net> wrote:
    [color=blue]
    >I have taken an extraordinary leap into the modern world by purchasing
    >webspace. In addition to my private concerns, I would like to make a
    >part to which others, e.g. my nieces and ex-wife, can ftp. To keep a
    >hold of the reigns, I shall write a program that changes the password,
    >and the brains of this prog will be in C:
    >#def MIN_WORD_LENGTH 9
    >#def MAX_WORD_LENGTH 15
    >
    >/* subroutine for random permutation */
    >void permute(char *, int)
    >
    >/* main
    >
    >char p[] = "abcdefghijklmn opqrstuvwxyz";
    >char q[] = "ABCDEFGHIJKLMN OPQRSTUVWXYZ";
    >char r[] = "0123456789 ";
    >void swap(char *, char *);
    >
    >end main */
    >
    >void permute(char *m , int n)
    >{ ; }
    >
    >void swap(char *a, char *b)
    >{
    >char c;
    >c = *a;
    >*a = *b;
    >*b = c;
    >}
    >/* end pseudosource */
    >I'm going to build a word from p, q and r. At this point, I have 2
    >questions:
    >1) Does the scope look right? I've got the subroutine at file scope and
    >the swap at block scope. If I make a call to swap from within permute(),
    >is every ISO compiler going to know what I'm talking about?[/color]

    All your subroutines are at file scope. You don't have a choice about
    this since C does not allow nested functions.

    If the prototype for swap() is really inside the body of main(), then
    when you call it inside permute(), the compiler will not know what
    types of arguments it takes or what kind of value it returns. In C99
    a diagnostic is required. In C89, the compiler must assume it returns
    an int, which is incorrect in this case. Many compilers will also
    generate an discretionary diagnostic about the missing prototype. If
    you move the code for swap() before the code for permute(), the
    problem goes away. The oft recommended approach here is to put your
    prototypes at file scope; it's just cleaner all around.
    [color=blue]
    >2) Is it foolhardy of me to think that I can permute a string given its
    >pointer and length, without needing any information back, hence the void
    >declaration? frank[/color]

    Since permute() receives a pointer to the string (as opposed to the
    string itself), it can update the string in place. You don't need to
    pass the length (though it is more efficient in my opinion) since
    permute could figure it out (e.g., strlen). Whether or not your
    function can generate a permutation with no other knowledge is an
    algorithm question. I expect the answer can be found in Knuth's
    masterpiece.


    Remove del for email

    Comment

    • Richard Heathfield

      #3
      Re: scope q

      Frank Silvermann said:
      [color=blue]
      > At this point, I have 2 questions:
      > 1) Does the scope look right? I've got the subroutine at file scope and
      > the swap at block scope.[/color]

      If you want a function for swap() - which is by no means necessary - it must
      be at file scope, since C does not support the nesting of functions.
      [color=blue]
      > If I make a call to swap from within permute(),
      > is every ISO compiler going to know what I'm talking about?[/color]

      C supports function-call syntax, yes.
      [color=blue]
      > 2) Is it foolhardy of me to think that I can permute a string given its
      > pointer and length, without needing any information back, hence the void
      > declaration?[/color]

      No, if you just mean a random permutation:

      for J = 0 to (N - 1)
      R = Pseudo-random number in range J to (N - 1)
      swap S[J], S[R]
      next
      all done

      --
      Richard Heathfield
      "Usenet is a strange place" - dmr 29/7/1999

      email: rjh at above domain (but drop the www, obviously)

      Comment

      • Frank Silvermann

        #4
        Re: scope q

        Barry Schwarz wrote:[color=blue]
        > On Mon, 29 May 2006 19:53:15 -0400, Frank Silvermann
        > <invalid@invali d.net> wrote:
        >[color=green]
        >> I have taken an extraordinary leap into the modern world by purchasing
        >> webspace. In addition to my private concerns, I would like to make a
        >> part to which others, e.g. my nieces and ex-wife, can ftp. To keep a
        >> hold of the reigns, I shall write a program that changes the password,
        >> and the brains of this prog will be in C:
        >> #def MIN_WORD_LENGTH 9
        >> #def MAX_WORD_LENGTH 15
        >>
        >> /* subroutine for random permutation */
        >> void permute(char *, int)
        >>
        >> /* main
        >>
        >> char p[] = "abcdefghijklmn opqrstuvwxyz";
        >> char q[] = "ABCDEFGHIJKLMN OPQRSTUVWXYZ";
        >> char r[] = "0123456789 ";
        >> void swap(char *, char *);
        >>
        >> end main */
        >>
        >> void permute(char *m , int n)
        >> { ; }
        >>
        >> void swap(char *a, char *b)
        >> {
        >> char c;
        >> c = *a;
        >> *a = *b;
        >> *b = c;
        >> }
        >> /* end pseudosource */
        >> I'm going to build a word from p, q and r. At this point, I have 2
        >> questions:
        >> 1) Does the scope look right? I've got the subroutine at file scope and
        >> the swap at block scope. If I make a call to swap from within permute(),
        >> is every ISO compiler going to know what I'm talking about?[/color]
        >
        > All your subroutines are at file scope. You don't have a choice about
        > this since C does not allow nested functions.[/color]
        I'm not understanding something here.
        [color=blue]
        > If the prototype for swap() is really inside the body of main(), then
        > when you call it inside permute(), the compiler will not know what
        > types of arguments it takes or what kind of value it returns. In C99
        > a diagnostic is required. In C89, the compiler must assume it returns
        > an int, which is incorrect in this case. Many compilers will also
        > generate an discretionary diagnostic about the missing prototype. If
        > you move the code for swap() before the code for permute(), the
        > problem goes away. The oft recommended approach here is to put your
        > prototypes at file scope; it's just cleaner all around.[/color]
        I'll be fine with declaring at file scope. Why would one, given a
        possible confrontation with something that sounds like 'agnostic', do
        otherwise?
        [color=blue][color=green]
        >> 2) Is it foolhardy of me to think that I can permute a string given its
        >> pointer and length, without needing any information back, hence the void
        >> declaration? frank[/color]
        >
        > Since permute() receives a pointer to the string (as opposed to the
        > string itself), it can update the string in place. You don't need to
        > pass the length (though it is more efficient in my opinion) since
        > permute could figure it out (e.g., strlen). Whether or not your
        > function can generate a permutation with no other knowledge is an
        > algorithm question. I expect the answer can be found in Knuth's
        > masterpiece.[/color]
        I need LESS than I thought? (Rhetorical question). Since I'll want
        cases to be equiprobable, the length will be known in main before it
        calls the subroutine. Seems like cheating. frank

        Comment

        • Barry Schwarz

          #5
          Re: scope q

          On Tue, 30 May 2006 03:31:42 -0400, Frank Silvermann
          <invalid@invali d.net> wrote:
          [color=blue]
          >Barry Schwarz wrote:[color=green]
          >> On Mon, 29 May 2006 19:53:15 -0400, Frank Silvermann
          >> <invalid@invali d.net> wrote:
          >>[color=darkred]
          >>> I have taken an extraordinary leap into the modern world by purchasing
          >>> webspace. In addition to my private concerns, I would like to make a
          >>> part to which others, e.g. my nieces and ex-wife, can ftp. To keep a
          >>> hold of the reigns, I shall write a program that changes the password,
          >>> and the brains of this prog will be in C:
          >>> #def MIN_WORD_LENGTH 9
          >>> #def MAX_WORD_LENGTH 15
          >>>
          >>> /* subroutine for random permutation */
          >>> void permute(char *, int)
          >>>
          >>> /* main
          >>>
          >>> char p[] = "abcdefghijklmn opqrstuvwxyz";
          >>> char q[] = "ABCDEFGHIJKLMN OPQRSTUVWXYZ";
          >>> char r[] = "0123456789 ";
          >>> void swap(char *, char *);
          >>>
          >>> end main */
          >>>
          >>> void permute(char *m , int n)
          >>> { ; }
          >>>
          >>> void swap(char *a, char *b)
          >>> {
          >>> char c;
          >>> c = *a;
          >>> *a = *b;
          >>> *b = c;
          >>> }
          >>> /* end pseudosource */
          >>> I'm going to build a word from p, q and r. At this point, I have 2
          >>> questions:
          >>> 1) Does the scope look right? I've got the subroutine at file scope and
          >>> the swap at block scope. If I make a call to swap from within permute(),
          >>> is every ISO compiler going to know what I'm talking about?[/color]
          >>
          >> All your subroutines are at file scope. You don't have a choice about
          >> this since C does not allow nested functions.[/color]
          >I'm not understanding something here.[/color]

          You said swap was at block scope (5 lines up). The function swap is
          not at block scope. It is at file scope. All functions are at file
          scope.
          [color=blue]
          >[color=green]
          >> If the prototype for swap() is really inside the body of main(), then
          >> when you call it inside permute(), the compiler will not know what
          >> types of arguments it takes or what kind of value it returns. In C99
          >> a diagnostic is required. In C89, the compiler must assume it returns
          >> an int, which is incorrect in this case. Many compilers will also
          >> generate an discretionary diagnostic about the missing prototype. If
          >> you move the code for swap() before the code for permute(), the
          >> problem goes away. The oft recommended approach here is to put your
          >> prototypes at file scope; it's just cleaner all around.[/color]
          >I'll be fine with declaring at file scope. Why would one, given a
          >possible confrontation with something that sounds like 'agnostic', do
          >otherwise?
          >[color=green][color=darkred]
          >>> 2) Is it foolhardy of me to think that I can permute a string given its
          >>> pointer and length, without needing any information back, hence the void
          >>> declaration? frank[/color]
          >>
          >> Since permute() receives a pointer to the string (as opposed to the
          >> string itself), it can update the string in place. You don't need to
          >> pass the length (though it is more efficient in my opinion) since
          >> permute could figure it out (e.g., strlen). Whether or not your
          >> function can generate a permutation with no other knowledge is an
          >> algorithm question. I expect the answer can be found in Knuth's
          >> masterpiece.[/color]
          >I need LESS than I thought? (Rhetorical question). Since I'll want
          >cases to be equiprobable, the length will be known in main before it
          >calls the subroutine. Seems like cheating. frank[/color]

          But will it be known in permute?


          Remove del for email

          Comment

          • Joe Smith

            #6
            Re: scope q


            "Richard Heathfield"[color=blue]
            > Frank Silvermann said:[/color]
            [color=blue][color=green]
            >> At this point, I have 2 questions:
            >> 1) Does the scope look right? I've got the subroutine at file scope and
            >> the swap at block scope.[/color]
            >
            > If you want a function for swap() - which is by no means necessary - it
            > must
            > be at file scope, since C does not support the nesting of functions.[/color]
            I'd be embarrassed to reveal how many times I've made this mistake. There
            is something down there that is really puzzling me, but maybe I need to be
            emphatic: hey, dummy, declare functions at file scope.
            [color=blue][color=green]
            >> If I make a call to swap from within permute(),
            >> is every ISO compiler going to know what I'm talking about?[/color]
            >
            > C supports function-call syntax, yes[/color]
            That is, when properly put at file scope.

            [color=blue][color=green]
            >> 2) Is it foolhardy of me to think that I can permute a string given its
            >> pointer and length, without needing any information back, hence the void
            >> declaration?[/color]
            >
            > No, if you just mean a random permutation:
            >
            > for J = 0 to (N - 1)
            > R = Pseudo-random number in range J to (N - 1)
            > swap S[J], S[R]
            > next
            > all done[/color]

            I have to bug out as OP on this, as not only have my newsreaders gone
            Indian, so have my Indians. C dreams. joe


            Comment

            • Keith Thompson

              #7
              Re: scope q

              "Joe Smith" <grumpy196884@n etzero.net> writes:[color=blue]
              > "Richard Heathfield"[/color]
              [...][color=blue][color=green]
              >> If you want a function for swap() - which is by no means necessary - it
              >> must
              >> be at file scope, since C does not support the nesting of functions.[/color]
              > I'd be embarrassed to reveal how many times I've made this mistake. There
              > is something down there that is really puzzling me, but maybe I need to be
              > emphatic: hey, dummy, declare functions at file scope.[/color]

              To be precise, function *declarations* at block scope are perfectly
              legal (though many would argue that they're bad style). Function
              *definitions* may only appear at file scope.

              --
              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

              Working...