I am using Microsoft Visual Studio 2010 to write a C code. This is the piece of code that I defined :
#define setImagVal_Matrix(matrix,type,x,y,val) \
(getImagVal_Matrix(matrix,type,x,y) = (val))
Then I am using it inside this function :
for(bands=0; bands < no_of_bands; bands++) {
outputmatrix[bands] = new_Matrix(yrange,xrange,getDataType_Image(inputImage),getDataFormat_Image(inputImage));
for(r=0; r < no_of_rows; r++) {
for(c=0; c < no_of_cols; c++) {
if(c<x1 || c>x2 || r<y1 || r>y2)
{
continue;
}
else
{
setImagVal_Matrix(outputmatrix[bands],getDataType_Matrix(outputmatrix[bands]),c-x1,r-y1,123);
}
}
}
}
However, it shows me this error on setImagVal_Matrix function call:
"Error:expected an expression"
And when I build the solution , here is the output which shows a syntax error on the same line:
1>c:\cviplab-net-2010\cviplab\crop.c(50): error C2059: syntax error : ')'
After spending couple of hours, I still cannot find the cause of the error . Any idea how to fix it?
EDIT:
I analyzed the pre-processed file and found the syntax error but still I don't know how to fix it. Here is the line which makes the error :
((((((outputmatrix[bands])->data_type) **)((outputmatrix[bands])->iptr))[r-y1][c-x1]) = (123));
the error is for the ) after **
Just stop using macros as functions. Try this:
inline void setImagVal_Matrix(int matrix, int type, int x, int y, int val) {
getImagVal_Matrix(matrix, type, x, y) = val;
}
Change the int types as appropriate, and you'll get the compiler helping you with useful error messages instead of cryptic ones. Heck, it'll even help you figure out the argument types.
Within your macro, you do an assignment to something that does not look like an lvalue. Do you mean ==?
I would like to propose at least to use brackets for the macro arguments:
#define setImagVal_Matrix((matrix),(type),(x),(y),(val)) \
(getImagVal_Matrix((matrix),(type),(x),(y)) = (val))
Also, to see if it is really bracket issue, you can try to look at preprocessed file.
But, actually using macro for this purpose is not a good one practice. If you care about function call you can make the function inline:
inline void setImagVal_Matrix(matrix,type,x,y,val) {
getImagVal_Matrix(matrix,type,x,y) = val;
}
In spite of the fact that inline is only recommendation for the compiler, this will allow to avoid a lot of compilation errors and hardly debugging run-time errors.
Related
I'm writing a simple code in C language, and this works.
Which compiles and excutes with no errors, gives the expected output.
#include <stdio.h>
int main(void) {
struct SiteTemplate {
int views;
};
int visit(struct SiteTemplate *site) {
site -> views++;
return 0;
}
struct SiteTemplate site;
site.views = 0;
visit(&site);
printf("%d\n", site.views);
return 0;
}
But in my VS Code, with C_Cpp linting is on, my IDE shows the following error and other problems with it.
declaration is incompatible with previous "visit" (declared at line 8)
Having a screenshot of it:
This error linting is really confusing me since my code works with gcc, it doesn't show any error when compiling.
And also, if I move my struct and function definition to the global level instead of inside main(), then the errors don't exist anymore... But what's the error declaration is incompatible? Or is there any problem with my code?
Click here to view the another screenshot to save whitespaces of this page.
By the way, the version of my VS Code is 1.52.0, with default C_Cpp linting.
Nested function definition is not standard C, it's supported by compiler extensions. According to C standard, any function definition needs to appear outside of any other function definition.
I am following along a tutorial for C programming in 6502 Assembly and am having a great deal of difficulty on the 3rd step. When compiling my code in the command line, i get these errors:
test4.c(8): Error: '{' expected
test4.c(9): Warning: Function must be extern
test4.c(9): Error: ';' expected
test4.c(13): Error: '}' expected
I am using a program to compile .c files made in code::blocks to .nes files. The current tutorial is having me also make .s assembly file when compiling in the cl65 command line in Windows from the program that is compiling it. Here is the link to the tutorial page i am on:
https://helloacm.com/tutorial-3-c-programming-in-6502-using-assembly/
I have tried many different combinations of code that i can think of to try and get rid of a least some of the problems, but alas to no avail. I am an amateur in C, usually use C++ but i cannot and still trying to figure this out. I was not able to find the "Function must be extern" error anywhere with a quick google search either. Anyone have an idea what's going on?
Here is how i wrote the code in code::blocks:
void main()
{
asm("nop");
}
void testasm()
void main()
{
while(1) {
testasm(); // call the assembled function
}
}
Also, had a really difficult time following along on this particular tutorial part.
Thanks in advance, any help is appreciated on diagnosing the problem!
Perhaps this is what you're after?
void testasm()
{
asm("nop");
}
void main()
{
while(1) {
testasm(); // call the assembled function
}
}
Your code had two main() functions, and a prototype void testasm() with no terminating semicolon.
Alternatively, if testasm is written in assembly, your code should look like this (removing the extra main function):
extern void testasm(); // `extern` not specifically required, but may be for
// your particular compiler
void main()
{
while(1) {
testasm(); // call the assembled function
}
}
You need to be much more careful writing code.
Hi I have been trying to port LWIP to a new arm device. When compiling the code i get the error message:
"lwip/lwip-1.4.0/src/include/lwip/memp_std.h:35:23: error: expected ')' before numeric constant"
When I go to this file this and below this several similar macros is what I find on that line:
LWIP_MEMPOOL(RAW_PCB, MEMP_NUM_RAW_PCB, sizeof(struct raw_pcb), "RAW_PCB")
If I remove the need for this macro with a define to deactivate the RAW functionality the error moves to the next LWIP_MEMPOL() macro.
The define it seems to want to put a ')' in front of is defined as this:
#define MEMP_NUM_RAW_PCB 1
The RAW_PCB is not defined but is "combined with MEMP_" to create an element in an enum.
I have tried to complie the whole ting with the -E option to get human redable object files and see if i can find any open '(' around the areas where MEMP_RAW_PCB apears and the substitution of MEMP_NUM_RAW_PCB to 1 but I have not found any by manual inspection yet.
Are there any suggestions on what can be going on here or what more I can do or look for to find the cause of the error?
I should maybe add that so far I don't call on any of the LWIP code from main() or any of the functions used in main().
I solved it with:
#ifndef MEMP_STD_H_
#define MEMP_STD_H_
... // memp_std.h codes ...
#endif //#ifndef MEMP_STD_H_
The error suggests you have unbalanced parentheses. The code you have provided thus far does not indicate where this problem is, but since ) is expected, it probably means the error is actually in the lines of code preceding the one you have shown.
Examine the code preceding the line you have shown (perhaps after using gcc -E) to check to see if all the parentheses are balanced.
If you're defining it with the dash-D option, it will generate the 1 by default, e.g.:
gcc -D 'MAX(A,B) ((A) < (B)? (B) : (A))' ...
Generates:
#define MAX(A,B) ((A) < (B)? (B) : (A)) 1
And you get the error: expected ‘)’ before numeric constant message at the line where the substitution occurs because of that trailing 1, e.g.:
int maxval = MAX(i,j);
// generates: int maxval = ((i) < (j)? (j) : (i)) 1;
Conversely, if you use the assignment operator to explicitly define the value, it will generate it the way you expected. E.g.:
int maxval = MAX(i,j);
// generates: int maxval = ((i) < (j)? (j) : (i));
I am getting a couple of weird compilation errors. This is for a homework assignment (help is ok). The idea is to implement a program that tests how well the user can hit "enter" once a second. I'm supposed to use gettimeofday to get some time values for each "enter" and then find out what the average time is and standard deviation... I'm trying to do this by checking stdin for '\n' and then if true, using gettimeofday to populate a timeval struct, then store said struct in an array for later use...
The errors I'm getting when compiling (gcc -Wextra homework1.c) are:
homework1.c: In function ‘main’:
homework1.c:19:29: error: expected ‘]’ before ‘;’ token
homework1.c:27:17: error: expected ‘)’ before ‘;’ token
homework1.c:32:4: error: ‘entry_array’ undeclared (first use in this function)
homework1.c:32:4: note: each undeclared identifier is reported only once for each function it appears in
I can't see why I'm getting those first two syntax errors and then I can't understand why "entry_array" is undeclared when I am clearly declaring it in the beginning of "main." Suggestions?
I feel like I'm getting burned by not knowing how to use the timeval struct... Initially I tried to define the struct timeval globally like you would with any other struct but was getting an error about overwriting the definition for struct timeval... Is this because it is defined in the "sys/time.h" library?
Here's the code:
GNU nano 2.2.6 File: homework1.c
//prototypes
int GetAverage(long array[]);
int GetStdDev(long array[]);
//# of keystrokes tracked by user
#define MAX_STROKES 1;
int main(int argv, char ** argc) {
struct timeval entry_array[MAX_STROKES]; //container for tv_usec fields from timeval struct
double average = 0;
double std_deviation = 0;
int count = 0;
printf("This program will test your ability to hit enter every 1 second, for 10 seconds. Ready when yo$
//loop to build array of timeval's
while (count < MAX_STROKES) {
struct timeval time_val;
int input = getc(stdin);
if (input == '\n') {
gettimeofday(&time_val, NULL);
entry_array[count] = time_val;
++count;
}
}
return 0;
}
The problem is the MAX_STROKES macro. As this is homework, I won't tell you exactly what the problem with it is.
This:
#define MAX_STROKES 1; could become a syntax error wherever you use "MAX_STROKES" (it's your job to figure out "why" ;)).
I hope you have this commented out: GNU nano 2.2.6 File: homework1.c
I'm not sure whether your "printf()" is OK: in your cut/paste, it's cut off here: Ready when yo$
I hope you're #includ'ing all the files you need, like "stdio.h" and "time.h"
Did a little research and decided that the MAX_STROKES macro idea I had wasn't quite right. Thanks guys. My guess is that it wasn't standing for what I wanted. I was looking for "int MAX_STROKES = 1"... Was 1 a char by default as it was? I can't quite tell what exactly it was. After reading I decided to use "static const int MAX_STROKES = 1;" instead and it compiled fine.
Since yesterday, I've been facing a compiling error for my C project. The project itself consists on creating a service that will make some tasks.
I don't what has changed since yesterday, but this morning, my code can't compile anymore.
Here are the errors I have :
c:\path\main.c(56): error C2275: 'SERVICE_TABLE_ENTRY' : illegal use of this type as an expression
c:\program files\microsoft sdks\windows\v7.0a\include\winsvc.h(773) : see declaration of 'SERVICE_TABLE_ENTRY'
c:\path\main.c(56): error C2146: syntax error : missing ';' before identifier 'DispatchTable'
c:\path\main.c(56): error C2065: 'DispatchTable' : undeclared identifier
c:\path\main.c(56): error C2059: syntax error : ']'
c:\path\main.c(57): error C2065: 'DispatchTable' : undeclared identifier
c:\path\main.c(57): warning C4047: 'function' : 'const SERVICE_TABLE_ENTRYA *' differs in levels of indirection from 'int'
c:\path\main.c(57): warning C4024: 'StartServiceCtrlDispatcherA' : different types for formal and actual parameter 1
Here's the code concerned by these errors (from lines 45 to 58) :
int main(int ac, char *av[])
{
if (ac > 1)
{
if (!parse_args(ac, av))
{
aff_error(ARGUMENTS);
return EXIT_FAILURE;
}
}
SERVICE_TABLE_ENTRY DispatchTable[] = {{MY_SERVICE_NAME, ServiceMain}, {NULL, NULL}};
StartServiceCtrlDispatcher(DispatchTable);
return EXIT_SUCCESS;
}
And here's the code of my ServiceMain function :
void WINAPI ServiceMain(DWORD ac, LPTSTR *av)
{
gl_ServiceStatus.dwServiceType = SERVICE_WIN32;
gl_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
gl_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
gl_ServiceStatus.dwWin32ExitCode = 0;
gl_ServiceStatus.dwServiceSpecificExitCode = 0;
gl_ServiceStatus.dwCheckPoint = 0;
gl_ServiceStatus.dwWaitHint = 0;
gl_ServiceStatusHandle = RegisterServiceCtrlHandler(MY_SERVICE_NAME, ServiceCtrlHandler);
if (gl_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
return;
gl_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
gl_ServiceStatus.dwCheckPoint = 0;
gl_ServiceStatus.dwWaitHint = 0;
SetServiceStatus(gl_ServiceStatusHandle, &gl_ServiceStatus);
}
I couldn't manage to find some answers that fit my problem, could anyone helps ? Thanks !
When you name your source files *.c, MSVC assumes it's compiling C, which means C89. All block-local variables need to be declared at the beginning of the block.
Workarounds include:
declaring/initializing all local variables at the beginning of a code block (directly after an opening brace {)
rename the source files to *.cpp or equivalent and compile as C++.
upgrading to VS 2013, which relaxes this restriction.
You might be using a version of C that doesn't allow variables to be declared in the middle of a block. C used to require that variables be declared at the top of a block, after the opening { and before executable statements.
Put braces around the code where the variable is used.
In your case that means:
if (ac > 1)
{
if (!parse_args(ac, av))
{
aff_error(ARGUMENTS);
return EXIT_FAILURE;
}
}
{
SERVICE_TABLE_ENTRY DispatchTable[] = {{MY_SERVICE_NAME, ServiceMain}, {NULL, NULL}};
StartServiceCtrlDispatcher(DispatchTable);
}
This error occurred when transferring a project from one installation to another (VS2015 => VS2010).
The C code was actually compiled as C++ on the original machine, on the target machine the "Default" setting in Project Properties\C/C++\Advanced\Compile as was somehow pointing to C even though the source file was of type *.cpp.
In my small program, errors popped up regarding the placement in code of certain types e.g. HWND and HRESULT as well as on the different format of for loops , and C++ constructs like LPCTSTR, size_t, StringCbPrintf and BOOL. Comparison.
Changing the "Compile as" from Default to Compile as C++ Code (/TP) resolved it.
This will also give you "illegal use of this type as an expression".
WRONG:
MyClass::MyClass()
{
*MyClass _self = this;
}
CORRECT:
MyClass::MyClass()
{
MyClass* _self = this;
}
You might be wonder the point of that code. By explicitly casting to the type I thought it was, when the compiler threw an error, I realized I was ignoring some hungarian notation in front of the class name when trying to send "this"
to the constructor for another object. When bug hunting, best to test all of your assumptions.