Warning C memset

This is my warning.

warning: passing argument 2 of β€˜memset’ makes integer from pointer without a cast

Then I changed as follows (make NULL equal to 0)

Before Change : memset(key, NULL, KEY_BYTES);
After Change : memset(key, 0, KEY_BYTES);

Warning deleted.

I am using the linux compiler and gcc, C.

This is the right action.

+3
source share
6 answers

Depending on the implementation, the C standard library sometimes defines NULL as a null integer (which avoids the warning you saw), but sometimes defines NULL as a null value in a void pointer. The last implementation raises the warning you saw.

, NULL . , NULL , . memset, , , NULL.

http://en.wikipedia.org/wiki/Stdlib.h:

NULL

stdlib.h stddef.h NULL, , .

NULL , int zero, int zero , * :

#define NULL 0
#define NULL 0L
#define NULL ((void *) 0)

C 0 0, , .

+13

, NULL ((void *) 0). , - memset , .

+3

memset int. NULL - C. :

0 , void *, .

, NULL (void *)0 . memset , .

: 0, , . , T, : p q :

T *p = NULL;
T *q = 0;

, p, :

if (p) ...
if (p != NULL) ...
if (p != 0) ...

, 0 : . , , . , 0, .

, , i 0, p:

int i = 0;
int *p = i;

.

( , , .)

+3

, . NULL NULL ( #define NULL 0), memset int, , 0, , int.

+1

memset , - . NULL, , C. . 0 .

0

memset(key, NULL, KEY_BYTES); NULL is the (void*)0 {ie; void* typecasted of int zero}. and the memset 2nd argument should be of type "unsigned int" .

. gcc ( ) .

0

Source: https://habr.com/ru/post/1736258/


All Articles