C error, " 'expected '=' ',' ';' , 'asm' or '__attribute__' before 'Bufferpar' "
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.
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 pInit开发者_运维技巧char, 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]='\0';
strcpy(String, vPointer);
pNchar=[0]=pFinalchar=[0];
return String;
}
You need a #define
, typedef
or appropriate header to declare/define PCHAR
.
If you're at a loss, try adding the following after the #include <string.h>
:
typedef char CHAR;
typedef char* PCHAR;
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 if
clauses.
I'd go so far as to suggest you change your code that follows this pattern:
if((vPointer=strstr(pPagebuffer, pInitchar) == NULL){ ... }
to:
vPointer=strstr(pPagebuffer, pInitchar);
if(vPointer == NULL){ ... }
I think you'll save yourself a bit of grief in the long run by avoiding the needless complexity in the if
statement's conditional expression.
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.
#include <windows.h>
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((vPointer=strstr(pPagebuffer, pInitchar)) == NULL){
Same thing here:
if((pNchar=strstr(vPointer, pFinalchar) == NULL)){
Should be:
if((pNchar=strstr(vPointer, pFinalchar)) == NULL)){
(also, the else clause after pNchar is broken)
And this line makes absolutely no sense:
pNchar=[0];=pFinalchar=[0];
This line...
strcpy(String, vPointer);
...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.
And most important:
return String;
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"
精彩评论