2012-10-19 112 views
0

如果我編譯下面的代碼我得到這樣的警告:需要擺脫的memset警告

警告:內建函數的memset不兼容的隱式聲明[默認啓用]

void transform(int **a, int m, int n) 
{ 
    int *row = malloc(m*sizeof(int)); 
    int *col = malloc(n*sizeof(int)); 
    memset(row, 0, sizeof(row)); 
    memset(col, 0, sizeof(col)); 
    [...] 
+1

'的sizeof(行)'是什麼指針大小是你的平臺上。即4中的32位env ..更正確的事情是'memset(raw,0,m * sizeof(int))' - 只是一個註釋:) – artapet

+0

'p = malloc(x * y)'後跟一個'memset (p,0,x * y)'與'p = calloc(x,y)'的調用相同。 – alk

+0

[編譯器錯誤:memset未在此範圍內聲明]的可能重複(http://stackoverflow.com/questions/2505365/compiler-error-memset-was-not-declared-in-this-scope)。其實,我的意思是[如何解決編譯器警告函數memset隱式聲明](http://stackoverflow.com/questions/2144617/how-to-resolve-compiler-warning-implicit-declaration-of-function-memset)。 – Antonio

回答

6

如果有疑問,看手冊頁:

$ man memset 

MEMSET(3)    BSD Library Functions Manual    MEMSET(3) 

NAME 
    memset -- fill a byte string with a byte value 

LIBRARY 
    Standard C Library (libc, -lc) 

SYNOPSIS 
    #include <string.h> 
    ^^^^^^^^^^^^^^^^^^^ 

這告訴你,你需要#include <string.h>爲了使編譯器看到函數原型爲memset

還要注意的是,你在你的代碼中的錯誤 - 你需要改變:

memset(row, 0, sizeof(row)); 
memset(col, 0, sizeof(col)); 

到:

memset(row, 0, m * sizeof(*m)); 
memset(col, 0, n * sizeof(*n));