Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

243
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda