Error when compiling C code with CreateEvent call - c

I get several compile errors if I compile the following function in Visual Studio 2005:
void search()
{
deviceEventHandle = CreateEvent(NULL, FALSE, FALSE, "foundDevice");
BTUINT32 deviceClass = 0; // 0 represents all classes
BTUINT16 maxDevices = 200; // 0 represents an unlimited number of responses
BTUINT16 maxDuration = 45; // maxDuration * 1.28 = number of seconds
Btsdk_StartDeviceDiscovery(deviceClass, maxDevices, maxDuration);
WaitForSingleObject(deviceEventHandle, INFINITE);
if (deviceEventHandle != NULL) {
CloseHandle(deviceEventHandle);
deviceEventHandle = NULL;
}
}
These are the errors I get:
error C2275: 'BTUINT32' : illegal use of this type as an expression
error C2146: syntax error : missing ';' before identifier 'deviceClass'
error C2065: 'deviceClass' : undeclared identifier
error C2275: 'BTUINT16' : illegal use of this type as an expression
error C2146: syntax error : missing ';' before identifier 'maxDevices'
error C2065: 'maxDevices' : undeclared identifier
error C2275: 'BTUINT16' : illegal use of this type as an expression
error C2146: syntax error : missing ';' before identifier 'maxDuration'
error C2065: 'maxDuration' : undeclared identifier
If I comment out the line containing the CreateEvent call the code compiles without errors:
void search()
{
//deviceEventHandle = CreateEvent(NULL, FALSE, FALSE, "foundDevice");
BTUINT32 deviceClass = 0; // 0 represents all classes
BTUINT16 maxDevices = 200; // 0 represents an unlimited number of responses
BTUINT16 maxDuration = 45; // maxDuration * 1.28 = number of seconds
Btsdk_StartDeviceDiscovery(deviceClass, maxDevices, maxDuration);
WaitForSingleObject(deviceEventHandle, INFINITE);
if (deviceEventHandle != NULL) {
CloseHandle(deviceEventHandle);
deviceEventHandle = NULL;
}
}
These are the headers I use:
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include "Btsdk_ui.h"
I compile the code as C code (/TC) in Visual Studio 2005. The "Btsdk_ui.h" file is part of the BlueSoleil bluetooth stack and inlcudes another file that contains definitions of BTUINT32 and BTUINT16.
All suggestions are welcome.

In C, you declare all your variables at the beginning of the block.
Move your deviceEventHandle = CreateEvent(NULL, FALSE, FALSE, "foundDevice"); to after your block of BTUINT variables.

When compiling C code, MSVC doesn't allow declarations to be mixed with statements - declarations can only be at the start of a block (MSVC follows more closely to the ANSI/ISO C90 standard than the C99 standard).
Try:
void search()
{
BTUINT32 deviceClass = 0; // 0 represents all classes
BTUINT16 maxDevices = 200; // 0 represents an unlimited number of responses
BTUINT16 maxDuration = 45; // maxDuration * 1.28 = number of seconds
deviceEventHandle = CreateEvent(NULL, FALSE, FALSE, "foundDevice");
Btsdk_StartDeviceDiscovery(deviceClass, maxDevices, maxDuration);
WaitForSingleObject(deviceEventHandle, INFINITE);
if (deviceEventHandle != NULL) {
CloseHandle(deviceEventHandle);
deviceEventHandle = NULL;
}
}

Related

How to skip parts of bison if yyparse fails

So basically, in my bison file if yyparse fails (i.e there is syntax error) I want to print 'ERROR' statement and not print anything else that I do in the above part of bison file in fac the stmt part. if yyparse returns 1 is there any way to skip the content of middle part of bison file? such as idk maybe writing if statement above the stmt part etc? I would appreciate any help! Thanks in advance.
Such as :
%{
#include "header.h"
#include <stdio.h>
void yyerror (const char *s)
{}
extern int line;
%}
%token ...///
%// some tokens types etc...
%union
{
St class;
int value;
char* str;
int line_num;
float float_value;
}
%start prog
%%
prog: '[' stmtlst ']'
;
stmtlst: stmtlst stmt |
;
stmt: setStmt | if | print | unaryOperation | expr
{
if ($1.type==flt && $1.line_num!=0) {
printf("Result of expression on %d is (",$1.line_num);
printf( "%0.1f)\n", $1.float_value);
$$.type=flt;
}
else if ($1.type==integer && $1.line_num!=0){
$$.type=integer;
printf("Result of expression on %d is (%d)\n",$1.line_num,$1.value);
}
else if ($1.type==string && $1.line_num!=0) {
$$.type=string;
printf("Result of expression on %d is (%s)\n",$1.line_num,$1.str);
}
else if ($1.type==mismatch && $1.line_num!=0)
{
$$.type=mismatch;
printf("Type mismatch on %d \n",$1.line_num);
}
else{ }
}
%%
int main ()
{
if (yyparse()) {
// if parse error happens only print this printf and not above stmt part
printf("ERROR\n");
return 1;
}
else {
// successful parsing
return 0;
}
}
Unfortunately, time travel is not an option. By the time the error is detected, the printf calls have already happened. You cannot make them unhappen.
What you can so (and must do if you are writing a compiler rather than a calculator) is create some data structure which represents the parsed program, instead of trying to execute it immediately. That data structure -- which could be an abstract syntax tree (AST) or a list of three-address instructions, or whatever, will be used to produce the compiled program. Only when the compiled program is run will the statements be evaluated.

VisualStudio 2017: Syntax error missing ':' before '^'

I have the following:
FileReader.cpp
#include "FileReader.h"
#include "FileData.h"
using namespace System;
using namespace System::IO;
FileReader::FileReader(){}
System::Void FileReader::readFile(System::String^ fileName) {
this->_fileName = fileName;
Console::WriteLine("Opening: " + _fileName);
try {
StreamReader^ fileIn = File::OpenText(_fileName);
System::String^ input;
int count = 0;
while ((count < 10 && (input = fileIn->ReadLine()) != nullptr)) {
Console::WriteLine(input);
count++;
}
}
catch (FileNotFoundException^ e) {
Console::WriteLine("FileNotFound");
}
}
FileReader.h
#pragma once
ref class FileReader
{
public:
FileReader();
FileData^ fData = gcnew FileData(); // This is whats causing the errors.
System::String^ _fileName;
System::Void readFile(System::String^ fileName);
};
FileData.cpp
#include "FileData.h"
FileData::FileData(){}
System::Void FileData::Set_DataName(System::String^ dName)
{
this->dataName= dName;
}
FileData.h
#pragma once
using namespace System;
ref class FileData
{
public:
FileData();
System::String^ dataName;
System::Void Set_PlayerName(System::String^ dName);
};
Problem I'm having is the compiler returns big list of errors when trying to create the FileData object in FileReader.h
Error C2143 syntax error: missing ';' before '^'
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2238 unexpected token(s) preceding ';'
Error C2143 syntax error: missing ';' before '^'
Error C4430 missing type specifier - int assumed.
Error C2238 unexpected token(s) preceding ';'
Error C2143 syntax error: missing ';' before '^'
Error C4430 missing type specifier - int assumed.
Error C2238 unexpected token(s) preceding ';'
I know its probably something simple I've missed but I can't figure out what I've done wrong. Anyone able to help?

Weird Syntax Error In C in If-else

I was programming an apache module. In the middle of the programming, I was opening a file, but I got an error while compiling.
32. static int wqb_handler(request_rec* req){
33. // Open and read our requested file
34. const char* p_file = req->filename;
35.
36. FILE* req_file;
37. if((req_file = fopen(p_file,"r"))==NULL){
38. return HTTP_NOT_FOUND;
39. }else{
40. fclose(req_file);
41. }
42. // Required variables
43. const char* content_type_a = "text/html";
44.
45. // Set Headers
46. ap_set_content_type(req,content_type_a);
47. if(req->header_only){
48. return OK;
49. }
50.
51.
52. return OK;
53. }
The problem is in that function, I was checking that was the problem, and I think the problem is the if-else statement, the code is written in C, not in C++.
These are the errors:
C:/wqb/wqb1_apache2.c(43) : error C2143: syntax error : missing ';' in front of 'const'
C:/wqb/wqb1_apache2.c(46) : error C2065: 'content_type_a' : undeclarated identifier
If this is C, and you're not compiling in C99 mode (i.e. with a C89 compiler), remember that all declarations must be directly following the start of a block.
Mixing declarations and code is a C99 feature imported from C++.
It appears you are compiling with a Micrososft Visual Studio Compiler in C mode. Note that William H. Gates III chose to ignore C99 entirely and refuses to update the C implemenation for the third millennium. :-)
Share with the solution of your problem. It will help the others to understand it more quickly.
Improve your knowledge about operators and comments.
You're writing too many unnecessary { and } in the operators.
For example, your code:
for( i = 0; i < N; i++ )
{
printf("Hello");
}
More simple/clear code:
for( i = 0; i < N; i++ )
printf("Hello");
...........................................................................
Your code (original) may look so (It's easier to read and understand.):
static int wqb_handler(request_rec* req)
{
/* Open and read our requested file */
const char* p_file = req -> filename;
FILE* req_file;
if((req_file = fopen(p_file,"r"))==NULL)
return HTTP_NOT_FOUND;
else
fclose(req_file);
/* Required variables */
const char* content_type_a = "text/html";
/* Set Headers */
ap_set_content_type(req,content_type_a);
if(req->header_only)
return 0;
return OK;
}

Don't know if I'm copying these integers the wrong way, but I'm getting some weird errors

I'm getting the following errors, hopefully someone can explain these errors for me as I'm not very good in C at the moment.
case ' ':
This is the error here. shellNew.c:57: error: a label can only be part of a statement and a declaration is not a statement
int rCheck = 0;
shellNew.c:58: error: expected expression before â
int foundPos = 0;
while(rCheck < 10)
{
if(inputBuffer[2] == historyBuffer[rCheck][0])
{
shellNew.c:63: error: â undeclared (first use in this function)
shellNew.c:63: error: (Each undeclared identifier is reported only once
shellNew.c:63: error: for each function it appears in.)
foundPos = rCheck;
}
rCheck++;
}
if(rCheck == 0)
{
printf("There was no command matching your criteria\n");
}
else
{
strcpy(inputBuffer, historyBuffer[rCheck]);
}
break;
Assuming that there was a switch in the previous 56 lines of code, then the compiler is complaining that you can't do:
switch (variable)
{
case ' ':
int var = 23;
because the declaration doesn't count as a statement and labels must be attached to statements. Converted into a minuscule function, this code gave me the error you report with GCC 4.7.1 on Mac OS X 10.7.5. The subsequent errors are probably because your variable rCheck was not declared because of the misplaced label, leading to problems where you try to use it.
You can't skip over variable declarations, so you'll need to use a statement block inside the switch statement block:
switch (variable)
{
case ' ':
{
int var = 23;
...
}
break;
}
This code compiled cleanly. It's a moot point whether it's better to put the break inside the statement block or outside; they're equivalent.

Failed Return Type In C With Visual Studio 2010

I'm trying to write a simple stack code and I get this code from a data structure book but it fails when i try to compile.
bool pushStack (STACK* stack, void* dataInPtr)
{
STACK_NODE* newPtr;
newPtr = (STACK_NODE*) malloc(sizeof(STACK_NODE));
if(!newPtr)
return FALSE;
newPtr->dataPtr = dataInPtr;
newPtr->link = stack->top;
stack->top = newPtr;
(stack->count)++;
return TRUE;
}
For example for this code, compiler says
Error 1 error C2061: syntax error : identifier 'pushStack'
Error 2 error C2059: syntax error : ';'
Error 3 error C2059: syntax error : 'type'
How can we resolve this? I tried to change TRUE to true, but it's not worked.
C doesn't have a bool data type (C++ does, though). Have the function return an int, and return 1 for TRUE and 0 for FALSE. Alternatively, #DEFINE TRUE 1 and #DEFINE FALSE 0.

Resources