Announcements

Help us improve the Power & Sensing Selection Guide. Share feedback

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
PhDi_1589426
Level 3
Level 3
First like received First like given

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

0 Likes
1 Solution

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.

View solution in original post

0 Likes
10 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes

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

0 Likes

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

0 Likes

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

0 Likes

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:

  1. Declare the variable outside the switch/case. Assigning the value as first instruction is no problem.
  2. Use __NOP(); as first instruction after the case

Will keep you updated, but digging deep into it might take a while.

0 Likes

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.

0 Likes

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

0 Likes

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

0 Likes

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

It first occured when I added the client role in the ble component.

Working on a peripheral project does not cause this issue.

0 Likes