【问题标题】:Errors on MikroCMikroC 上的错误
【发布时间】:2017-05-25 13:22:55
【问题描述】:

我正在使用 mikroC 对 pic16f1823 进行编程,但遇到了一些非常奇怪的错误。

当我声明一个整数时,会出现一个错误,当我尝试声明一个 char* 数组时,我会收到一个错误,上面写着Too many chars。最奇怪的错误之一是 '}' 预期的 '}'。

我的代码如下,如果您发现任何会导致此类错误的错误,请通知我。

#include <stdio.h>

char *filter;
char *filters[25] = {'1','6','11','2','7','12','3','8','13','4','9','14','5','10','15'};//error: TOO many chars
int PositionOfFilters[] = {1,6,11,2,7,12,3,8,13,4,9,14,5,10,15};
int EEPROM_READ(void);
void main() {
     TRISA = 0;//all port A are set as outputs
     TRISC = 0;//all port C are set as outputs
     while(1){
              int prevState = RA2;//store the previous state of RA2 to see if the button was every pressed or not
              int i;
              unsigned int *address[15];
              i2c_Start();
              for(i = 0; i < 16; i++){
                    address[i] = i2c_read(0);//should return what pin is set high;
              }
              i2c_Stop();
           if(RA2 != prevState){ //if the state of RA2 is different than the previous state that means the button was pressed
                  LCD_Cmd(_LCD_CLEAR);
                  int position;//error invalid experison
                  int on;
                  Lcd_Out(2,14,'LOC'); //prints out the we are on local mode
                  for(i = 0; i<16;i++){
                        if(address[i]!=0x00){ //checking to see which pin is high as well as where it's corresponding position is
                            on = address[i]; //should work because only one number shouldn't be 0x00
                            position = i; //gets the corresponding position
                            break;
                        }
                  }
                  if(on == 0x06){
                        Lcd_Out(1,3,'filter 15');//must be filter 15 because there is no other filter that has 0x06;
                  }
                  else{
                       int j;
                       for(j = 0; j < 15; j++){
                             if(PositionOfFilters[j] == position){
                                   //we know what filter we are at
                                   filter = filters[j];
                                   break;
                             }
                     } '}' expected '}' found MyProject.c
                       char *finalFilter = 'filter ' + filter;
                       Lcd_Out(1,3,finalFilter);//Prints out which filter is on
                  }
                  //for the eeprom
                  EEPROM_READ();

           }
           else{
                Lcd_Out(2,14,'REM'); //will print out REM for remote mode
           }
     }
}

int EEPROM_READ(void){
      //figure out how long the eeprom data is
      //start storing the data in a array and then return that array
      //do this by using i2c_start() then eeprom_read(address) then i2c_restart
}

【问题讨论】:

  • 您确定{} 的总数相等吗?我更喜欢在forif 下方的新行上使用{ 格式化 - 在同一列位置,如}。它提高了此类情况的可读性。
  • 刚刚检查,所有的 { 和 } 都相等

标签: c debugging compiler-errors pic mikroc


【解决方案1】:

过滤器声明错误。您声明了一个 char 指针数组并使用 char 文字对其进行初始化。

你需要指向一个字符的字符串

char *filters[25] = { "1", "6", "11", "2", "7", "12", "3", "8", "13", "4", "9", "14", "5", "10", "15" }; 

这取决于filters 的用途。


第二个问题是

Lcd_Out(1, 3, 'filter 15');

这个代码肯定是 eb

Lcd_Out(1, 3, "filter 15");

C 字符串字面量必须由 " 而非 ' 环绕


第三个问题

char *finalFilter = 'filter ' + filter;

这是错误的。如前所述,字符串必须是 "filter" 并且使用 您不能以这种方式连接字符串。 例如,您可以这样做

char finalFilter[32];
sprintf(finalFilter, "%s%s", "filter", filter);

【讨论】:

  • 对于您发送的第一次编辑以将 char * 更改为仍然给我错误的 char
  • @MOHAMMEDSALEH 不。答案的第一个版本我给了你 2 个选择。更好地查看您的代码,您需要我现在在答案中留下的选项:char * with strings as initizalizer...
  • 谢谢你我想通了,感谢你指出的其他错误
  • @MOHAMMEDSALEH 不客气。如果足够,请关闭问题并标记正确答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-12
  • 1970-01-01
  • 2015-12-23
  • 2021-09-07
相关资源
最近更新 更多