C语言 C 错误,“'预期'='','';' , 'asm' 或 '__attribute__' 在 'Bufferpar' 之前"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5814768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:31:03  来源:igfitidea点击:

C error, " 'expected '=' ',' ';' , 'asm' or '__attribute__' before 'Bufferpar' "

c

提问by Polishh

Small coding project using the Code::Blocks IDE, worked on buffer parsing functionality, when compiling I got a strange error (as seen above). I did some research online, generally it's because Code::Blocks is trying to compile C code when it's C++, this is not the case. This is C.

使用 Code::Blocks IDE 的小型编码项目,处理缓冲区解析功能,编译时出现奇怪的错误(如上所示)。我在网上做了一些研究,一般是因为 Code::Blocks 在 C++ 时试图编译 C 代码,事实并非如此。这是 C。

Exact message reads, again, as follows

确切的消息再次读取如下

Line 3 : error: expected '=', ',', 'ASM', or '__attribute__' before 'BufferPar'

Everything else seemed to compile fine except this little piece of code. I'm clueless, any ideas? I'm afraid I may be overlooking some minor detail....

除了这一小段代码之外,其他一切似乎都编译得很好。我一无所知,有什么想法吗?恐怕我可能忽略了一些小细节......

#include <string.h>

PCHAR BufferPar(PCHAR pPagebuffer, PCHAR pInitchar, PCHAR pFinalchar)
{
    PCHAR vPointer, pNchar, *phLocate;
    CHAR String[1024];

    if(pPagebuffer == NULL) return NULL;

    if((vPointer=strstr(pPagebuffer, pInitchar) == NULL){
       return vPointer;}
       else vPointer += strlen(pInitchar);

    *phLocate = vPointer;

    if((pNchar=strstr(vPointer, pFinalchar) == NULL)){
    return pNchar;}
    else pNchar[0]='
typedef char CHAR;
typedef char* PCHAR;
'; strcpy(String, vPointer); pNchar=[0]=pFinalchar=[0]; return String; }

回答by Michael Burr

You need a #define, typedefor appropriate header to declare/define PCHAR.

您需要一个#define,typedef或适当的标头来声明/定义PCHAR

If you're at a loss, try adding the following after the #include <string.h>:

如果您不知所措,请尝试在 之后添加以下内容#include <string.h>

if((vPointer=strstr(pPagebuffer, pInitchar) == NULL){ ... } 

there are several other problems in the snippet that the compiler will tell you about, so I'll let you just address them that way. However, I will say that you should pay close attention to the assignment operations in the ifclauses.

编译器会告诉您代码片段中的其他几个问题,因此我会让您以这种方式解决它们。但是,我会说您应该密切注意if子句中的赋值操作。

I'd go so far as to suggest you change your code that follows this pattern:

我什至建议您更改遵循此模式的代码:

vPointer=strstr(pPagebuffer, pInitchar);

if(vPointer == NULL){ ... }

to:

到:

#include <windows.h>

I think you'll save yourself a bit of grief in the long run by avoiding the needless complexity in the ifstatement's conditional expression.

我认为,从长远来看,通过避免if语句条件表达式中不必要的复杂性,您会为自己省去一点悲伤。

回答by selbie

Lots of issues beyond this code not compiling.

超出此代码的许多问题无法编译。

You should include windows.h at the top of the file to get rid of the issue around PCHAR and the error about BufferPar.

您应该在文件顶部包含 windows.h 以消除有关 PCHAR 的问题和有关 BufferPar 的错误。

if((vPointer=strstr(pPagebuffer, pInitchar) == NULL){

But that still won't fix the compile issue.

但这仍然不能解决编译问题。

Your expression:

你的表情:

if((vPointer=strstr(pPagebuffer, pInitchar)) == NULL){

Won't compile because you are missing an extra set of parens. Try this instead:

无法编译,因为您缺少一组额外的括号。试试这个:

if((pNchar=strstr(vPointer, pFinalchar) == NULL)){

Same thing here:

同样的事情在这里:

if((pNchar=strstr(vPointer, pFinalchar)) == NULL)){

Should be:

应该:

pNchar=[0];=pFinalchar=[0];

(also, the else clause after pNchar is broken)

(另外,pNchar 之后的 else 子句被破坏了)

And this line makes absolutely no sense:

这条线完全没有意义:

strcpy(String, vPointer);

This line...

这条线...

 return String;

...is unsafe because you are copying a string of indeterminate length into a buffer of fixed size length. Use strncpy or strcpy_s instead. Or at least do some smarts to validate that the source string isn't bigger than the buffer size of the fixed string.

...是不安全的,因为您将不确定长度的字符串复制到固定大小长度的缓冲区中。请改用 strncpy 或 strcpy_s。或者至少做一些聪明的事情来验证源字符串不大于固定字符串的缓冲区大小。

And most important:

而最重要的是:

##代码##

No no no!!! Don't return a pointer to a stack variable - your program will at best crash (or worse, have a non-deterministic bug depending on how your function gets called and uses the return value). Subsequent function calls after BufferPar returns will trash the memory held by "String"

不不不!!!不要返回指向堆栈变量的指针 - 您的程序充其量只会崩溃(或者更糟的是,根据您的函数如何被调用和使用返回值,有一个不确定的错误)。BufferPar 返回后的后续函数调用将清除“String”持有的内存