解决 array subscript has type char 错误

原来的代码如下,编译器报 

error: array subscript has type 'char' [-Werror=char-subscripts]    gettaskinfo((char *)Tname[i]);

    int cnt=0;
    char i=0;
    char *Tname[3]={"display_task","http_get_task","task_sendclk"};
	while(1)
	{		
		cnt++;
		if(cnt>1000){
			cnt=0;
			gettaskinfo((char *)Tname[i]);			
			i++;
			if(i>=3)
				i=0;
		}
		vTaskDelay(10/portTICK_RATE_MS);
	}

起初怀疑自己使用指针数组出现错误,多番修改还是一样,查阅资料

GCC手册说:

-Wchar-subscripts
Warn if an array subscript has type “char”. This is a common cause of error,as programmers often forget that this type is signed on some
machines. This warning is enabled by -Wall.

因此,此错误警告应防止使用负数组索引。

解决如下:

char i=0;

修改为 无符号的变量类型

uint16_t i=0;

经测试 uint8_t 也是可以编译成功的


版权声明:本文为sjygqz原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。