hash table variable question

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

    hash table variable question

    On pages 144 to 145 in the book "The C Programming Language" by K & R,
    they have part of the following

    #define HASHSIZE 101

    static struct nlist *hastab[HASHSIZE];

    struct nlist *lookup(char *s)
    {
    struct nlist *np;

    for (np = hashtab[hash(s)]; np != NULL; np = np->next)
    if (strcmp(s, np->name) == 0) // found
    return np;
    return NULL; /* not found*/
    }

    Wouldn't

    struct nlist *np;

    in lookup(), since it doesn't have the static keyword(?), be an
    automatic variable in this case?
  • Iman S. H. Suyoto

    #2
    Re: hash table variable question

    Chad wrote:
    On pages 144 to 145 in the book "The C Programming Language" by K & R,
    they have part of the following
    >
    #define HASHSIZE 101
    >
    static struct nlist *hastab[HASHSIZE];
    >
    struct nlist *lookup(char *s)
    {
    struct nlist *np;
    >
    for (np = hashtab[hash(s)]; np != NULL; np = np->next)
    if (strcmp(s, np->name) == 0) // found
    return np;
    return NULL; /* not found*/
    }
    >
    Wouldn't
    >
    struct nlist *np;
    >
    in lookup(), since it doesn't have the static keyword(?), be an
    automatic variable in this case?
    Yes, np is auto.

    Comment

    • Jack Klein

      #3
      Re: hash table variable question

      On Sat, 19 Jul 2008 20:13:47 -0700 (PDT), Chad <cdalten@gmail. com>
      wrote in comp.lang.c:
      On pages 144 to 145 in the book "The C Programming Language" by K & R,
      they have part of the following
      >
      #define HASHSIZE 101
      >
      static struct nlist *hastab[HASHSIZE];
      >
      struct nlist *lookup(char *s)
      {
      struct nlist *np;
      >
      for (np = hashtab[hash(s)]; np != NULL; np = np->next)
      if (strcmp(s, np->name) == 0) // found
      return np;
      return NULL; /* not found*/
      }
      >
      Wouldn't
      >
      struct nlist *np;
      >
      in lookup(), since it doesn't have the static keyword(?), be an
      automatic variable in this case?
      Yes, the pointer 'np' defined in the first line of the function
      lookup() has automatic storage duration.

      I'm looking into my crystal ball, and taking a guess at the question
      that I think you really meant to ask. Here is the answer I think
      you're looking for.

      The pointer is automatic, so the pointer object 'np' itself goes out
      of scope and its lifetime ends at the end of the function.

      The function returns a pointer value, either the value contained in
      'np' if a match was found, or a null pointer if no match was found.

      NULL is always a valid value to assign to any type of pointer,
      although of course it can't be dereferenced.

      If a match is found, the function returns the value of 'np', which is
      the address of one of the structures in the static array 'hashtab'.

      So even though the pointer used inside the function is automatic, the
      address value that the function returns, if a match is found, is the
      address of a static structure that still exists when the function
      returns.


      --
      Jack Klein
      Home: http://JK-Technology.Com
      FAQs for
      comp.lang.c http://c-faq.com/
      comp.lang.c++ http://www.parashift.com/c++-faq-lite/
      alt.comp.lang.l earn.c-c++

      Comment

      • Chad

        #4
        Re: hash table variable question

        On Jul 19, 8:37 pm, Jack Klein <jackkl...@spam cop.netwrote:
        On Sat, 19 Jul 2008 20:13:47 -0700 (PDT), Chad <cdal...@gmail. com>
        wrote in comp.lang.c:
        >
        >
        >
        On pages 144 to 145 in the book "The C Programming Language" by K & R,
        they have part of the following
        >
        #define HASHSIZE 101
        >
        static struct nlist *hastab[HASHSIZE];
        >
        struct nlist *lookup(char *s)
        {
        struct nlist *np;
        >
        for (np = hashtab[hash(s)]; np != NULL; np = np->next)
        if (strcmp(s, np->name) == 0) // found
        return np;
        return NULL; /* not found*/
        }
        >
        Wouldn't
        >
        struct nlist *np;
        >
        in lookup(), since it doesn't have the static keyword(?), be an
        automatic variable in this case?
        >
        Yes, the pointer 'np' defined in the first line of the function
        lookup() has automatic storage duration.
        >
        I'm looking into my crystal ball, and taking a guess at the question
        that I think you really meant to ask. Here is the answer I think
        you're looking for.
        >
        The pointer is automatic, so the pointer object 'np' itself goes out
        of scope and its lifetime ends at the end of the function.
        >
        The function returns a pointer value, either the value contained in
        'np' if a match was found, or a null pointer if no match was found.
        >
        NULL is always a valid value to assign to any type of pointer,
        although of course it can't be dereferenced.
        >
        If a match is found, the function returns the value of 'np', which is
        the address of one of the structures in the static array 'hashtab'.
        >
        So even though the pointer used inside the function is automatic, the
        address value that the function returns, if a match is found, is the
        address of a static structure that still exists when the function
        returns.
        >
        Wow. Is the pattern of questions in my posting history that obvious?
        Anyhow, I thought about it and it makes sense.

        Chad

        Comment

        • Richard Heathfield

          #5
          Re: hash table variable question

          Chad said:
          On Jul 19, 8:37 pm, Jack Klein <jackkl...@spam cop.netwrote:
          <snip>
          >>
          >I'm looking into my crystal ball, and taking a guess at the question
          >that I think you really meant to ask. Here is the answer I think
          >you're looking for.
          >>
          <snip>
          >>
          >So even though the pointer used inside the function is automatic, the
          >address value that the function returns, if a match is found, is the
          >address of a static structure that still exists when the function
          >returns.
          >>
          >
          Wow. Is the pattern of questions in my posting history that obvious?
          Yep, I'm afraid so. Until I saw Jack's answer, I was all set to write
          something very similar.

          --
          Richard Heathfield <http://www.cpax.org.uk >
          Email: -http://www. +rjh@
          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
          "Usenet is a strange place" - dmr 29 July 1999

          Comment

          • Barry Schwarz

            #6
            Re: hash table variable question

            On Sat, 19 Jul 2008 20:53:24 -0700 (PDT), Chad <cdalten@gmail. com>
            wrote:

            snip
            >Wow. Is the pattern of questions in my posting history that obvious?
            >Anyhow, I thought about it and it makes sense.
            The appearance of clairvoyance is an illusion. The question comes up
            sufficiently frequently that I would expect some even have a canned
            answer ready to be pasted into the response.


            Remove del for email

            Comment

            • vippstar@gmail.com

              #7
              Re: hash table variable question

              On Jul 20, 6:13 am, Chad <cdal...@gmail. comwrote:
              On pages 144 to 145 in the book "The C Programming Language" by K & R,
              they have part of the following
              I doubt it. There's a // comment there, probably inserted by you. When
              you give attribute to some code, do not modify it, unless obvious.
              (ie // Chad: found)

              <snip>
              in lookup(), since it doesn't have the static keyword(?), be an
              automatic variable in this case?
              You've already got an excellent answer, but I just wanted to add that
              static is a storage-class specifier and also a keyword.

              Comment

              • Jack Klein

                #8
                Re: hash table variable question

                On Sun, 20 Jul 2008 00:04:04 -0700, Barry Schwarz <schwarzb@dqel. com>
                wrote in comp.lang.c:
                On Sat, 19 Jul 2008 20:53:24 -0700 (PDT), Chad <cdalten@gmail. com>
                wrote:
                >
                snip
                >
                Wow. Is the pattern of questions in my posting history that obvious?
                Anyhow, I thought about it and it makes sense.
                >
                The appearance of clairvoyance is an illusion. The question comes up
                sufficiently frequently that I would expect some even have a canned
                answer ready to be pasted into the response.
                I'm not really claiming clairvoyance, although I suppose it never
                hurts to awe the newbies, I suppose.

                Actually, I almost missed this. I had written a lively dissertation
                about how the storage class of a pointer need not have any
                relationship to the storage class of the data pointed to.

                Then, at the very last second, a second, and fortunately correct,
                interpretation hit me.

                --
                Jack Klein
                Home: http://JK-Technology.Com
                FAQs for
                comp.lang.c http://c-faq.com/
                comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                alt.comp.lang.l earn.c-c++

                Comment

                Working...