Ich habe ein Makro wie folgt aus:Erwartete Ausdruck vor 'typeof' OR erwartet Primärausdruck vor 'typeof'
#include <stdio.h>
#include <stddef.h>
#define m_test_type(e) \
do { \
if (typeof(e) == typeof(char [])) { \
printf("type is char []\n"); \
} else \
if (typeof(e) == typeof(int)) { \
printf("type is int\n"); \
} else { \
printf("type is unknown\n"); \
} \
} while (0)
int main() {
char s[] = "hello";
m_test_type(s);
return 0;
}
Beim Übersetzen mit gcc-I-Fehler erhalten folgende:
prog.cpp: In function 'int main()':
prog.cpp:6:14: error: expected primary-expression before 'typeof'
if (typeof(e) == typeof(char *)) { \
^
prog.cpp:19:2: note: in expansion of macro 'm_test_type'
m_test_type(s);
^
prog.cpp:6:14: error: expected ')' before 'typeof'
if (typeof(e) == typeof(char *)) { \
^
prog.cpp:19:2: note: in expansion of macro 'm_test_type'
m_test_type(s);
^
prog.cpp:9:14: error: expected primary-expression before 'typeof'
if (typeof(e) == typeof(int)) { \
^
prog.cpp:19:2: note: in expansion of macro 'm_test_type'
m_test_type(s);
^
prog.cpp:9:14: error: expected ')' before 'typeof'
if (typeof(e) == typeof(int)) { \
^
prog.cpp:19:2: note: in expansion of macro 'm_test_type'
m_test_type(s);
^
'typeof' ist nicht Standard-C, es ist ein [GNU-Erweiterung] (https://gcc.gnu.org/onlinedocs/gcc/Typeof .html) in GCC. Welchen Compiler benutzen Sie? – unwind
Ich benutze GCC-Compiler –
Code muss ein SWAG nehmen, was "e" ist. Es hat keine Möglichkeit zu wissen und auch nicht "typeof". –