- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi everyone,
at the moment I program a central from scratch.
To provide an interface I added a UART with stdio functionality.
Althouth it should not expect any other expression, the compiler expects a semicolon.
For a better understanding see the picture attached to this case.
The project is also attached to this case.
What could be the problem?
Best regards
Philipp
Solved! Go to Solution.
- Tags:
- compiler error
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
A good explanation of the issue can be found here:
c++ - Why can't variables be declared in a switch statement? - Stack Overflow
Although it is labeled as C++ it also talks about C specification.
Looks like this is normal behavior.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Your project compiles error-free.
I do not know what or how you produced the error in your picture, but you are trying to allocate a variable with the same name a second time.
I get (beside 12 notes) a warning for an unused variable.
There are component updates available (BLE PDL), please check.
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Bob,
I hoped that one of you could tell me why this occures.
I know that it should compile without errors, but it does not.
I updated the Component PDL and did a clean build but nothing changed.
Which variable do you mean?
Thanks
Philipp
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I can reproduce the issue and it looks like it is caused because the enum has a value assigned to it.
Same error is triggered when using CY_BLE_EVT_SET_SLAVE_LATENCY_MODE_COMPLETE which has also a value assigned.
That is strange, I will do some more debugging...
@Bob: you have to remove the semicolon in the line to trigger the error.
regards,
Achim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Achim,
have you tried to do a clean build on your side?
Maybe it is a failure with my creator 4.2 installation or the PDL 3.04.
I will uninstall and install everything on my side.
Best regards,
Philipp
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Probably it is the software combination, I will continue debugging it and create an internal ticket.
My assumption above was wrong, the issue is triggered when a variable is declared as first instruction after the case.
To solve the issue I found two workarounds:
- Declare the variable outside the switch/case. Assigning the value as first instruction is no problem.
- Use __NOP(); as first instruction after the case
Will keep you updated, but digging deep into it might take a while.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
A good explanation of the issue can be found here:
c++ - Why can't variables be declared in a switch statement? - Stack Overflow
Although it is labeled as C++ it also talks about C specification.
Looks like this is normal behavior.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Achim,
thanks for the answer. You safed my day because it caused other failures elsewhere.
The confusing thing is that it works in most cases
I will try it out and will avoid declaring variables in switch statements.
Best regards
Philipp
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Achim,
I found the same proceeding in several example projects.
For example in CE215118_BLE_Multi_Master_Single_Slave with the following code declaring a variable in a case:
case CY_BLE_EVT_GAPC_SCAN_PROGRESS_RESULT:
{
cy_stc_ble_gapc_adv_report_param_t *advReport = (cy_stc_ble_gapc_adv_report_param_t *)eventParam;
bool printHeader = false;
bool isNewAddress = true;
...
}
break;
The difference to my code is, that they use curley brackets.
Regards
Philipp
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Philipp,
As explained in the stack overflow post pointed by Achim, the use of curly braces restrict the scope of the variable to within the {} and thus make the code build/compile. If you do not use {}, the case statement (which is a jump label) cannot have a declaration as the first statement i.e. a Label cannot have declaration as its first statement in C - so compilation error.
In addition, it is always a good practice to have variable declarations at the beginning of the block in which the variable's scope is required. And if you are defining a variable in the middle of a function/block, then enclose the section in {} to define the scope in which the variable needs to be visible and declare the variable at the beginning of the block {}.
Regards,
Meenakshi Sundaram R
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It first occured when I added the client role in the ble component.
Working on a peripheral project does not cause this issue.