gpt4 book ai didi

c - 常量与变量不匹配

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:55:02 27 4
gpt4 key购买 nike

有人能解释一下为什么常数与if()条件中的变量不匹配吗?

z3 tests # cat deftst.c
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <fcntl.h>

#define MY_BLOCK_SIZE 512

main() {
int fd,ret;
size_t block_size;

fd=open("/dev/loop2",O_DIRECT|O_RDWR|O_NONBLOCK);
printf("fd=%d\n",fd);

ret = ioctl(fd, BLKSSZGET, &block_size);
printf("ret=%d\n",ret);
if (block_size!=MY_BLOCK_SIZE) {
printf("block_size=%d , MY_BLOCK_SIZE=%d\n",block_size,MY_BLOCK_SIZE);
printf("error, block size don't match the constant!\n");
}
}
z3 tests # gcc -o deftst deftst.c
deftst.c: In function 'main':
deftst.c:21:3: warning: format '%d' expects type 'int', but argument 2 has type 'size_t'
z3 tests # dd if=/dev/zero of=device.dat bs=512 count=128
128+0 records in
128+0 records out
65536 bytes (66 kB) copied, 0.00167074 s, 39.2 MB/s
z3 tests # losetup /dev/loop2 device.dat
z3 tests # ./deftst
fd=3
ret=0
block_size=512 , MY_BLOCK_SIZE=512
error, block size don't match the constant!
z3 tests #

我试着用十六进制打印,并确认它在常量和变量中都是相同的值。真奇怪。

最佳答案

似乎blksszget需要一个指向int的指针,您的块大小可能很长。见here

关于c - 常量与变量不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21092228/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com