error: array subscript has type 'char' when using isdigit()

zazas321
Posts: 231
Joined: Mon Feb 01, 2021 9:41 am

error: array subscript has type 'char' when using isdigit()

Postby zazas321 » Wed Jun 07, 2023 10:49 am

Hello. In my UART0 parser I have the following function to parse the incoming data:

Code: Select all

bool ParseSystemCmd(char *line, uint16_t cmd_size)
{

    if (!strncmp("ping", line,4))
    {	
            printf("pong\n");
            return true;
    }

    if (!strncmp("red:", line,4))
    {	
        if (!isdigit(line[4]))
		{
			ESP_LOGW(TAG,"Entered input is not a number");
			return 0;
		}
		char temp_buf[4];
		uint16_t data_to_write=0;
		for(int i = 0;i <=(strlen(line)-4);i++){
			temp_buf[i] = line[4+i];
		}
		data_to_write = atoi(temp_buf);
        RGB_set_red(data_to_write);
        return true;
    }

    return 0;

}


This function is being called when I read incomming UART data:

Code: Select all


void UART0_task(void *argument)
{
	UART0_setup();
  	unsigned int char_received=EOF;
  	unsigned int char_counter=0;
  	char command_line[UART0_COMMAND_LINE_MAX_SIZE];
  	for (;;)
	{	
		int len = uart_read_bytes(UART_NUM_0, command_line, (UART0_COMMAND_LINE_MAX_SIZE - 1), 20 / portTICK_PERIOD_MS);
        if (len) {
            command_line[len] = 0;
			ParseSystemCmd(command_line, len); // Line is complete. Execute it!
			memset(&command_line, 0, sizeof(command_line));
        }
		vTaskDelay(10/portTICK_PERIOD_MS);

  }
  
I think I didint have any problems with the esp-idf v4.4 but now I ported my code to esp-idf v5.0.1 and noticed this weird error:

Code: Select all

error: array subscript has type 'char' [-Werror=char-subscripts]
I dont really understand what is the issue:

Code: Select all

        if (!isdigit(line[4]))
		{
			ESP_LOGW(TAG,"Entered input is not a number");
			return 0;
		}
		
All I am doing is checking if the 4th byte in the char array is digit or no. For example:
red:50 is valid
red:x1 is not valid

Craige Hales
Posts: 94
Joined: Tue Sep 07, 2021 12:07 pm

Re: error: array subscript has type 'char' when using isdigit()

Postby Craige Hales » Wed Jun 07, 2023 11:43 am

use

Code: Select all

isdigit((unsigned char)(line[4]))
The problem is not the 4, it is because char is really signed char and isdigit is warning about numbers > 127 will be negative indexes without the cast. Something changed, probably the isdigit definition. (good, it is safer now.)
The cast does not generate code; it just means the byte will be treated as a 0..255 value rather than -128..127

edit https://stackoverflow.com/questions/101 ... -type-char
casting to int may make sense too.
Craige

zazas321
Posts: 231
Joined: Mon Feb 01, 2021 9:41 am

Re: error: array subscript has type 'char' when using isdigit()

Postby zazas321 » Wed Jun 07, 2023 12:19 pm

Craige Hales wrote:
Wed Jun 07, 2023 11:43 am
use

Code: Select all

isdigit((unsigned char)(line[4]))
The problem is not the 4, it is because char is really signed char and isdigit is warning about numbers > 127 will be negative indexes without the cast. Something changed, probably the isdigit definition. (good, it is safer now.)
The cast does not generate code; it just means the byte will be treated as a 0..255 value rather than -128..127

edit https://stackoverflow.com/questions/101 ... -type-char
casting to int may make sense too.
Thanks for the response. What about casting to unsigned char?:

Code: Select all

if (!isdigit( (unsigned char)(line[4]) ))

Craige Hales
Posts: 94
Joined: Tue Sep 07, 2021 12:07 pm

Re: error: array subscript has type 'char' when using isdigit()

Postby Craige Hales » Wed Jun 07, 2023 1:45 pm

If the line[] data might have characters beyond 0x7F then I think you have to cast to unsigned char before any conversion to int. Otherwise they become negative numbers. The cast to unsigned char does not generate code, it just interprets 8-bit patterns with the high bit set as numbers > 0x007F rather than negative numbers with the sign bit set.
Craige

Who is online

Users browsing this forum: Bing [Bot] and 141 guests