3
// set all values in the hash table to null
for(int i = 0; i < HASH_SIZE; i++)
{
    hashtable[i] = NULL;
}

I keep getting this error message in response to hashtable[i]:

assignment makes integer from pointer without a cast [-Werror]

Why?

2 Answers 2

7

If hashtable is an array of integers then hashtable[i] expects an integer and NULL is a pointer.

So you're trying to assign a pointer value to an integer variable(without a cast), this is usually just a warning but since you have -Werror all warnings turn into errors.

Just use 0 instead of NULL.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I see. I could also declare an array of pointers and store the value that way. I get it now. Thanks!
1

NULL is defined as (void*)0 in stddef.h

#ifndef _LINUX_STDDEF_H
#define _LINUX_STDDEF_H

#undef NULL
#if defined(__cplusplus)
#define NULL 0
#else
#define NULL ((void *)0)
#endif

#endif

If the hashtable is integer array, like

#include <stdio.h>
#define HASH_SIZE 100
int main()
{
int i = 0, hashtable[HASH_SIZE];
for(i = 0; i < HASH_SIZE; i++)
{
    hashtable[i] = NULL;
}
return 0;
}

this warning: assignment makes integer from pointer without a cast will be shown.

2 Comments

What makes you believe the OP uses Linux? What makes you believe NULL must be defined in this particular way? Note that making assumptions by extrapolation from one system makes you in for surprises. Instead, reading and citing the ISO C Standard is likely to provide definitive answers. Such answers are independent of OS and header contents.
@Jens, Thanks. I will try to follow ISO C std :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.