Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

244
Visualizações
Why is does char from an array give subscript warning in gcc for isspace in newlib

I´m having troubles understand a compiler warning that we have in our code. The code we have are similar to the one in this example that give the warning (-Wchar-subscripts). We are using ARM gcc 9.2.1 embedded and newlib as standard library.

#include <ctype.h>

int main ()
{
  char str[]="Example string\n";

  int i = isspace(str[1]); // Warning

  char c=str[1];
  i = isspace(c); // No warning

  i = isspace((unsigned char)str[1]); // No warning
}

Example in godbolt

From what I have understood the implementation of isspace can be via array indexing. Hence the warning. But in that case shouldn't both 1 and 2 give the warning? Why is it only the first one that gives the warning?

We solved our code by adding a cast but I'm not really satisfied until I have understood why it helps.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

This appears to be the very same bug as discussed in Bugzilla here Bug 95177 - error: array subscript has type char. Basically gcc is inconsistent in its diagnostics and this behavior only appeared in later versions.

As discussed in that thread, passing char to the ctype.h functions could in theory be a problem in case the char would contain anything unknown. These functions are defined as expecting the input to be something representable as unsigned char, see C17 7.4:

In all cases the argument is an int, the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF

Therefore int i = isspace((unsigned char)str[1]); makes the warning go away.

But the warning is as you can tell inconsistent. Which could possibly be explained by when you are using ctype.h, the compiler could sometimes be picking a macro and sometimes a function.

Just disable it and regard it as a false positive. According to the Bugzilla log above, this should now have been fixed very recently. gcc (trunk) and gcc >10.3 doesn't give this warning any longer.

over 4 years ago · Santiago Trujillo Relatório

0

The difference in warning messages between isspace(str[1]) and isspace(c) is caused by a bug in GCC’s feature to suppress warning messages in system headers.

Consider this code:

#include <ctype.h>

int foo(char c)
{
    return isspace(c);
}

int bar(char c)
{
    return 
          ((((_ctype_)+sizeof(""[
          c
          ]))[(int)(
          c
          )])&010);
}

The code in bar is the result of applying macro replacement to the code in foo (it was obtained by compiling with -E to show the result of preprocessing). So these two functions have identical code after macro replacement and should have identical semantics. Yet the second code gets a warning and the first code does not. Therefore, GCC’s reporting is not based solely on the C semantics of the code.

GCC has a feature to suppress warnings in system header files. When this feature is disabled (by using -Wsystem-headers), both functions get a warning message. Therefore, the warning that occurs with isspace(str[1]) but not with isspace(c) is due to a failure of the feature to suppress warning messages in system headers. That is, it is a bug in GCC.

For confirmation, using -Wsystem-headers with the original code in the question yields warning messages for both isspace(str[1]) and isspace(c).

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda