C 中的数组算术问题
我有一个使用 cc 编译的长文件中的 C 代码。但是当我尝试在 gcc 上编译时,它给出了错误。我在小程序中获取了该特定代码并尝试在 cc 上编译,但在那里失败了。
这是来源:
#include <stdio.h>
int main (int argc, char **argv)
{
char unsigned FileName[100];
char test[100];
FileName[strstr(FileName,test) - FileName] = 0;
return 0;
}
这一行导致了问题: FileName[strstr(FileName,test) - FileName] = 0;
CC 上的错误是:
"foo.c", line 10: operands have incompatible types:
int "-" pointer to unsigned char
而 gcc 上的错误是:
foo.c:10: error: invalid operands to binary - Both are same.
但是当我在 CC 上编译原始文件时,它会编译并给出警告。像这样:
"dbtprc.c", line 643: warning: argument #1 is incompatible with prototype:
prototype: pointer to const char : "/usr/include/iso/string_iso.h", line 133
argument : pointer to unsigned char
"dbtprc.c", line 643: warning: improper pointer subtraction
你能帮忙解释一下为什么这里给出警告“不正确的指针减法”并且示例程序显示错误吗?
I have a C code in long file that is compiled using cc. But when I tried to compile on gcc it gives error. I took that particular code in small program and try to compile on cc but it failed over there.
Here is source:
#include <stdio.h>
int main (int argc, char **argv)
{
char unsigned FileName[100];
char test[100];
FileName[strstr(FileName,test) - FileName] = 0;
return 0;
}
This line is causing the problem:FileName[strstr(FileName,test) - FileName] = 0;
error on CC is :
"foo.c", line 10: operands have incompatible types:
int "-" pointer to unsigned char
and on gcc is :
foo.c:10: error: invalid operands to binary - Both are same.
But when I compile original file on CC it compiled and just give a warning. Like this:
"dbtprc.c", line 643: warning: argument #1 is incompatible with prototype:
prototype: pointer to const char : "/usr/include/iso/string_iso.h", line 133
argument : pointer to unsigned char
"dbtprc.c", line 643: warning: improper pointer subtraction
Can you please help why here it is giving warning "improper pointer subtraction" and sample program it is showing error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
错误或警告没有太大区别,只是显示编译器认为问题的严重程度。
但为什么使用
unsigned char
作为文件名呢?这与strstr
冲突,后者仅处理char*
的参数和返回类型。这就是编译器试图以不同方式告诉您的内容。
An error or a warning is not much different, just showing how serious the compiler believes the issue is.
But why do you use
unsigned char
for the filename? That's in conflict withstrstr
which only handleschar*
, both for its parameter and return type.That's what the compilers try to tell you, in different ways.
您是否错过了包含
?如果是,则猜测 strsrt 的原型,默认情况下它返回一个 int,因此指针操作无效。
否则,符号不匹配似乎是警告/错误的原因。
在表的两次出现之前使用
(char*)
强制转换,它就会消失。Don't you missed to include
<string.h>
??If yes, the prototype of strsrt is guessed and by default it returns an int, hence the invalid pointer operation.
Otherwise, it appears that the signedness mismatch is the cause of the warning/error.
Use a
(char*)
cast before the two occurence of your table and it will go.C 中有什么叫做数组算术的东西吗?阅读以下内容:
数组不是指针。
并了解如何使用 strstr()。
Is there anything called Array Arithmetic in
C
? Read this:Arrays are not Pointers.
And see how to use strstr().
这是未定义的行为,就在那里!您指的是甚至未分配的内存位置。
编辑:
现在你有另一个问题:即使你成功声明了两个数组,你也未能清理/初始化它们。只有上帝知道
strstr()
会返回给您什么。您编译此代码时遇到的问题是 strstr() 需要 2
const char*
并且您将 Filename 定义为 unsigned:char unsigned FileName[100];
That's undefined behaviour, right there! You are referring to a memory location that wasn't even allocated.
EDIT:
Now you have another problem: even though you sucessfully declared both arrays, you failed to clean/initialize them. Only god knows what
strstr()
will return to you.The problem you have compiling this code is that strstr() takes 2
const char*
and you defined Filename as unsigned:char unsigned FileName[100];