c - 需要摆脱 memset 警告

标签 c warnings 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));
    [...]

最佳答案

如有疑问,请查看手册页:

$ 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 的函数原型(prototype).

另请注意,您的代码中存在错误 - 您需要更改:

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

到:

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

关于c - 需要摆脱 memset 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12965905/

相关文章:

c - 我需要知道我是否正确使用 getline

c - 如何在 c 中执行多个读/写 HDD/USB 字节

c++ - 如何为指向数组的指针执行 memset?

c - 如何使用 memset 向像素数组添加颜色?

c - 将指针传递给保证归零的内存

c - dtrace 从应用程序访问全局变量

c - 将 unsigned char 数组的内容分成两半

ios - 如何解决 iOS 中的此警告?内存泄漏

c - 程序有效,但无法理解警告

javascript - 什么时候浏览器会在 SSL 站点上抛出不安全内容警告