2012-10-19 114 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));