hash table variable question

C

Chad

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

Iman S. H. Suyoto

Chad said:
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.
 
C

Chad

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*/
}

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
 
B

Barry Schwarz

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
 
V

vippstar

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)

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top