Помощь - Поиск - Пользователи - Календарь
Полная версия этой страницы: warning от Keil
Форум разработчиков электроники ELECTRONIX.ru > Сайт и форум > В помощь начинающему > Программирование
Метценгерштейн
Не вижу чего- то некорректного на картинке.

а такой код работает без варнингов.

Код
    case CMD_GET_FW_INFO:
            frame->data_1 = 0;            
        
            //uint8_t * ptr = &frame->data_2;
            //int size = sizeof(hex); // вернула 82 (0х52) байт
        
            memcpy (&frame->data_2, &hex, sizeof(hex));            
            uart_responce(length_unstuffed + sizeof(hex));
            break;


Первый вариант нельзя применять? В чем суть варнинга была?

Картинка- первый код. Споллер- второй код.
Палыч
Транслятор не может определить область видимости объявленных переменных. В Вашем случае, наверное, лучше всего взять в скобки { ... } весь текст от case до break.
Метценгерштейн
это только кусок скрина. Там есть скобки и case несколько штук.
aaarrr
Цитата(Метценгерштейн @ Mar 24 2017, 18:31) *
это только кусок скрина. Там есть скобки и case несколько штук.

Скобки там, а видимость надо ограничить тут. Прям вот на этом куске.
Метценгерштейн
т.е. эти переменные еще где-то видны за пределами этого case?
Из-за этого ругается?
aaarrr
Да, видны. Из-за этого и ругается.
Палыч
Цитата(Метценгерштейн @ Mar 24 2017, 21:28) *
Из-за этого ругается?

Example:
Код
int main(void){
   int choice = 1;
   int z =1;
   switch(choice)
   {
     case 1:
       int y = 1;
       z = y + z;
       break;
     case 2:
     break;
   }
   return 0;

In the example, y is an initialized variable that is in scope (but unused) in the other cases.
The C++ Standard says in section 6.7:
Цитата
It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps from a point where a local variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless the variable has POD type (3.9) and is declared without an initializer (8.5).

Note
The transfer from the condition of a switch statement to a case label is considered a jump in this respect.
The usual way to fix this is to enclose the case that declares y in braces:
Код
case 1:   {
  int y = 1;
  z = y + z;
}
break;

Because y is a POD (Plain Old Data) type, so an alternative is to not use initialization:
Код
case 1:
int y;
y = 1;
z = y + z;
break;
Метценгерштейн
спасибо всем.
Для просмотра полной версии этой страницы, пожалуйста, пройдите по ссылке.
Invision Power Board © 2001-2025 Invision Power Services, Inc.