Question: 1. The following code attempts to create a new variable of type presence( a kind of structure) but when I run it through gdb, it shows that the variable has the same address in every iteration? Shouldn't a local variable get a different address when it is being redeclared?

```
presence* hashtable[MAX];
int i = 0;

printf("---------ENTERING DATA FOR THE HASHTABLE------------------\n");
while (i < MAX)
{

        presence item; /* Same address on every iteration */
        printf("Enter item id : ");
        scanf("%d",&item.id);

        printf("Enter item name : ");
        item.name = (char*)malloc(SIZE*sizeof(char));
        getchar();
        fgets(item.name,SIZE,stdin);

        putItem(hashtable,&item);
        i++;

}

```
2. When a item of type presence is put inside the hashtable, should I allocate the memory and then assign item or do it simply(refer the following code)

```
int putItem(presence* hashtable[], presence* item)
{
    int key = hash(item->id);
    hashtable[key] = (presence*)malloc(sizeof(presence)); /* Required or not? or do the below straight away?*/
    hashtable[key] = item;
}

```

Answer: 
For 1: You should have `presence *item;` pointer type variable, allocate it and use it to fill the data. Your current implementation uses same variable in each iteration.

```
while (i < MAX)
{

        presence *item; /* pointer */
        item = malloc(sizeof(*item));
        printf("Enter item id : ");
        scanf("%d",&item->id);

        printf("Enter item name : ");
        item->name = (char*)malloc(SIZE*sizeof(char));
        getchar();
        fgets(item->name,SIZE,stdin);

        putItem(hashtable,item);
        i++;

}

```

For 2: You don't need to allocate memory. The line

```
hashtable[key] = (presence*)malloc(sizeof(presence));

```

is not needed.

However make sure `key` is less than `MAX` which is size of `hashtable`. Also, you may want to handle clashes appropriately.
1. Local variables typically get only one address. As the variable `item` goes out of scope on the loop's back edge, the compiler is free to reuse its storage on the next execution of the loop. Either way, storing a pointer to a local variable to a data structure that lives longer than the scope of the local variable is a bad idea.
2. Depends on what you need: if the values in the hash table should be independent of the values that are passed in you should allocate memory in the function. If they shouldn't, then not.