Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这个C指针错误是什么意思?为什么会这样 #包括 #包括 #包括 typedef uint8_t字节; int main(int argc,char*argv[]) { char*block=malloc(字节*512); FILE*FILE=fopen(argv[1],“r”); fclose(argv[1]); }_C - Fatal编程技术网

这个C指针错误是什么意思?为什么会这样 #包括 #包括 #包括 typedef uint8_t字节; int main(int argc,char*argv[]) { char*block=malloc(字节*512); FILE*FILE=fopen(argv[1],“r”); fclose(argv[1]); }

这个C指针错误是什么意思?为什么会这样 #包括 #包括 #包括 typedef uint8_t字节; int main(int argc,char*argv[]) { char*block=malloc(字节*512); FILE*FILE=fopen(argv[1],“r”); fclose(argv[1]); },c,C,我不知道我到底做错了什么,我还没有开始编写任何实际的代码,我对C中的指针还不熟悉。我已经有错误了,甚至没有做任何事情,我很抱歉 #include <stdio.h> #include <stdlib.h> #include <stdint.h> typedef uint8_t BYTE; int main(int argc, char *argv[]) { char *block = malloc(BYTE * 512); FILE *fi

我不知道我到底做错了什么,我还没有开始编写任何实际的代码,我对C中的指针还不熟悉。我已经有错误了,甚至没有做任何事情,我很抱歉

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
    char *block = malloc(BYTE * 512);
    FILE *file = fopen(argv[1], "r");
     
    fclose(argv[1]);
}
clang-ggdb3-O0-std=c11-Wall-Werror-Wextra-Wno符号比较-Wno未使用参数-Wno未使用变量-Wshadow recover.c-lcrypt-lcs50-lm-o recover
recover.c:9:26:错误:意外的类型名称“BYTE”:应为表达式
char*block=malloc(字节*512);
^
recover.c:12:12:错误:不兼容的指针类型将“char*”传递给类型为“FILE*”的参数(也称为“struct\u IO\u FILE*”)[-Werror,-Wincompatible指针类型]
fclose(argv[1]);
^~~~~~~
/usr/include/stdio.h:213:26:注意:在这里将参数传递给参数“\uu stream”
extern int fclose(文件*\uu流);
^
产生2个错误。
生成:**[:恢复]错误1
使用类型的大小代替类型

clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    recover.c  -lcrypt -lcs50 -lm -o recover
recover.c:9:26: error: unexpected type name 'BYTE': expected expression
    char *block = malloc(BYTE * 512);
                         ^
recover.c:12:12: error: incompatible pointer types passing 'char *' to parameter of type 'FILE *' (aka 'struct _IO_FILE *') [-Werror,-Wincompatible-pointer-types]
    fclose(argv[1]);
           ^~~~~~~
/usr/include/stdio.h:213:26: note: passing argument to parameter '__stream' here
extern int fclose (FILE *__stream);
                         ^
2 errors generated.
make: *** [<builtin>: recover] Error 1
更好的是,不要使用任何类型

//char *block = malloc(BYTE * 512);
char *block = malloc(sizeof(BYTE) * 512);

对于
fclose
您应该将
文件*
作为参数传递,对于您的情况,它将是
fclose(文件)

BYTE
不是一个变量,而是一种类型,
argv[1]
的类型是
char*
而不是
FILE*
。用简单的英语告诉我们你认为
malloc(BYTE*512)
在做什么。你只询问“指针错误”,但你有两个错误(在三行代码中-命中率很高!)。当然“
将'char*'传递给'FILE*
'类型的参数”是不言自明的吗?这样分配:
block=malloc(sizeof*block*512)
这是一个问题。谢谢!改为使用块[512]。除了分配给
char*
而不是
BYTE*
没有意义之外,你能帮我处理其他错误吗?要么是
BYTE
块,要么是
char
@Clifford
uint8\t
块,存在
uint8\t,BYTE,char都是一个字节
sizeof*block
避免了“正确类型”的问题。@chux,我很感激。这是关于OP代码的问题,而不是你的答案。它应该被应用到这个问题上,抱歉。在这种情况下,这可能无关紧要,但在语义上是有缺陷的。在创建类型别名时遇到麻烦,然后不一致地使用它,这会导致维护问题。
char *block = malloc(sizeof *block * 512);