C printf 编译器警告

发布于 2024-09-30 17:18:52 字数 1324 浏览 2 评论 0原文

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <errno.h>

int main(int argc, char *argv[])
{
    int fd, offset;
    char *data;
    struct stat sbuf;
    int counter;

    if (argc != 2) {
        fprintf(stderr, "usage: mmapdemo offset\n");
        exit(1);
    }

    if ((fd = open("mmapdemo.c", O_RDONLY)) == -1) {
        perror("open");
        exit(1);
    }

    if (stat("mmapdemo.c", &sbuf) == -1) {
     perror("stat");
        exit(1);
    }

    offset = atoi(argv[1]);
    if (offset < 0 || offset > sbuf.st_size-1) {
        fprintf(stderr, "mmapdemo: offset must be in the range 0-%ld\n",sbuf.st_size-1);
        exit(1);
    }

    data = mmap((caddr_t)0, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);

    if (data == (caddr_t)(-1)) {
        perror("mmap");
        exit(1);
    }

    // print the while file byte by byte

    while(counter<=sbuf.st_size)
        printf("%c", data++);

    return 0;
}

这给了我如下错误:

gcc mmapdemo.c -o mmapdemo
mmapdemo.c: In function 'main':
mmapdemo.c:48: warning: format '%c' expects type 'int', but argument 2 has type 'char *'

请帮我解决问题。

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <errno.h>

int main(int argc, char *argv[])
{
    int fd, offset;
    char *data;
    struct stat sbuf;
    int counter;

    if (argc != 2) {
        fprintf(stderr, "usage: mmapdemo offset\n");
        exit(1);
    }

    if ((fd = open("mmapdemo.c", O_RDONLY)) == -1) {
        perror("open");
        exit(1);
    }

    if (stat("mmapdemo.c", &sbuf) == -1) {
     perror("stat");
        exit(1);
    }

    offset = atoi(argv[1]);
    if (offset < 0 || offset > sbuf.st_size-1) {
        fprintf(stderr, "mmapdemo: offset must be in the range 0-%ld\n",sbuf.st_size-1);
        exit(1);
    }

    data = mmap((caddr_t)0, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);

    if (data == (caddr_t)(-1)) {
        perror("mmap");
        exit(1);
    }

    // print the while file byte by byte

    while(counter<=sbuf.st_size)
        printf("%c", data++);

    return 0;
}

This gives me error as follows:

gcc mmapdemo.c -o mmapdemo
mmapdemo.c: In function 'main':
mmapdemo.c:48: warning: format '%c' expects type 'int', but argument 2 has type 'char *'

Please help me to solve the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身软味 2024-10-07 17:18:52
printf("%c", *data++);

data 是一个char *%c 格式说明符告诉 printf 需要一个 char。要从 char * 获取 char,您需要使用 * 运算符取消引用指针。

也就是说,您的程序仍然无法正常工作,因为您没有在打印循环中递增计数器,也没有初始化它。我会选择:

for (size_t i = 0; i < sbuf.st_size; ++i) {
    printf("%c", data[i]);
}

相反。我没有检查你的程序的其余部分,但鉴于我查看的三行中有三个严重错误,我怀疑其余部分没有错误。

printf("%c", *data++);

data is a char *. The %c format specifier tells printf to expect a char. To get a char from a char *, you need to dereference the pointer using the * operator.

That said, your program still won't work properly because you're not incrementing counter in your print loop, nor have you initialized it. I would go with:

for (size_t i = 0; i < sbuf.st_size; ++i) {
    printf("%c", data[i]);
}

instead. I haven't inspected the rest of your program, but given that there were three serious errors in three lines that I looked at, I doubt that the rest is bug-free.

初雪 2024-10-07 17:18:52

要逐字节打印出来,需要使用

printf("%c ", *data++)

or 打印出十六进制值:

printf("%02X", *data++);

to print it out byte by byte, need to use

printf("%c ", *data++)

or to print out the hex values:

printf("%02X", *data++);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文