Forum: Mikrocontroller und Digitale Elektronik Keine Warnung bei Vergleich von enum und Pointer


von Simon (Gast)


Lesenswert?

Moin und Hallo,
ich nutze den ARM/GNU C Compiler : 6.3.1.
Bei der Umstellung eines Funktionsparamters von enum auf den Pointer auf 
diesen Typ ist mir aufgefallen, dass bei dem folgenden Code keine 
Warnung ausgegeben wird.
-Wall und -Wextra sind gesetzt.
Kann mir dies bitte jemand erklären?
1
typedef enum
2
{
3
  STATUS_OK = 0,
4
  STATUS_COLLISION = 1,
5
  STATUS_BAD_ADDRESS = 2,
6
  I2C_STATUS_TIMEOUT = 4,
7
  I2C_STATUS_OVERFLOW = 8,
8
} STATUS_CODE_t;
9
10
static void test(volatile STATUS_CODE_t *status)
11
{
12
  if (status == STATUS_OK)
13
  {
14
    nop();
15
  }
16
}
17
int main()
18
{
19
  volatile I2C_STATUS_CODE_t status;
20
  status = I2C_STATUS_OK;
21
  test(&status);
22
}

Vielen Dank,
Simon

von Rolf M. (rmagnus)


Lesenswert?

Simon schrieb:
> Kann mir dies bitte jemand erklären?

STATUS_OK ist 0. Damit wird aus dem hier ein Nullpointer-Vergleich:

>   if (status == STATUS_OK)

von EAF (Gast)


Lesenswert?

Mein C sagt:
1
E:\Programme\arduino\portable\sketchbook\sketch_feb01d\t.c: In function 'test':
2
E:\Programme\arduino\portable\sketchbook\sketch_feb01d\t.c:13:5: warning: implicit declaration of function 'nop' [-Wimplicit-function-declaration]
3
   13 |     nop();
4
      |     ^~~
5
E:\Programme\arduino\portable\sketchbook\sketch_feb01d\t.c: In function 'main':
6
t.c:18:12: error: unknown type name 'I2C_STATUS_CODE_t'
7
   18 |   volatile I2C_STATUS_CODE_t status;
8
      |            ^~~~~~~~~~~~~~~~~
9
t.c:19:12: error: 'I2C_STATUS_OK' undeclared (first use in this function); did you mean 'STATUS_OK'?
10
   19 |   status = I2C_STATUS_OK;
11
      |            ^~~~~~~~~~~~~
12
      |            STATUS_OK
13
E:\Programme\arduino\portable\sketchbook\sketch_feb01d\t.c:19:12: note: each undeclared identifier is reported only once for each function it appears in
14
E:\Programme\arduino\portable\sketchbook\sketch_feb01d\t.c:20:8: warning: passing argument 1 of 'test' from incompatible pointer type [-Wincompatible-pointer-types]
15
   20 |   test(&status);
16
      |        ^~~~~~~
17
      |        |
18
      |        volatile int *
19
E:\Programme\arduino\portable\sketchbook\sketch_feb01d\t.c:9:42: note: expected 'volatile STATUS_CODE_t *' but argument is of type 'volatile int *'
20
    9 | static void test(volatile STATUS_CODE_t *status)
21
      |                  ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
22
exit status 1
23
unknown type name 'I2C_STATUS_CODE_t'

Und mein C++ sagt:
1
E:\Programme\arduino\portable\sketchbook\sketch_feb01d\sketch_feb01d.ino: In function 'void test(volatile STATUS_CODE_t*)':
2
sketch_feb01d:11:14: error: no match for 'operator==' (operand types are 'volatile STATUS_CODE_t*' and 'STATUS_CODE_t')
3
   11 |   if (status == STATUS_OK)
4
      |       ~~~~~~ ^~ ~~~~~~~~~
5
      |       |         |
6
      |       |         STATUS_CODE_t
7
      |       volatile STATUS_CODE_t*
8
sketch_feb01d:13:5: error: 'nop' was not declared in this scope
9
   13 |     nop();
10
      |     ^~~
11
E:\Programme\arduino\portable\sketchbook\sketch_feb01d\sketch_feb01d.ino: In function 'int main()':
12
sketch_feb01d:18:12: error: 'I2C_STATUS_CODE_t' does not name a type; did you mean 'STATUS_CODE_t'?
13
   18 |   volatile I2C_STATUS_CODE_t status;
14
      |            ^~~~~~~~~~~~~~~~~
15
      |            STATUS_CODE_t
16
sketch_feb01d:19:3: error: 'status' was not declared in this scope; did you mean 'static'?
17
   19 |   status = I2C_STATUS_OK;
18
      |   ^~~~~~
19
      |   static
20
sketch_feb01d:19:12: error: 'I2C_STATUS_OK' was not declared in this scope; did you mean 'STATUS_OK'?
21
   19 |   status = I2C_STATUS_OK;
22
      |            ^~~~~~~~~~~~~
23
      |            STATUS_OK
24
E:\Programme\arduino\portable\sketchbook\sketch_feb01d\sketch_feb01d.ino: At global scope:
25
E:\Programme\arduino\portable\sketchbook\sketch_feb01d\sketch_feb01d.ino:9:13: warning: 'void test(volatile STATUS_CODE_t*)' defined but not used [-Wunused-function]
26
    9 | static void test(volatile STATUS_CODE_t *status)
27
      |             ^~~~
28
exit status 1
29
no match for 'operator==' (operand types are 'volatile STATUS_CODE_t*' and 'STATUS_CODE_t')


Natürlich darf man einen Pointer prüfen, ob er Null ist.
Aber das will man hier ja wohl nicht, einen Pointer auf Null prüfen, 
sondern das worauf er zeigt.


C ist da weniger pingelig, als C++.

von Simon (Gast)


Lesenswert?

Rolf M. schrieb:
> STATUS_OK ist 0. Damit wird aus dem hier ein Nullpointer-Vergleich:

Super! Das verstehe sogar ich. :-D

EAF schrieb:
> Mein C sagt:

Vielen Dank,
da habe ich wohl beim Vereinfachen etwas zu viel bzw. zu wenig weg 
genommen.

Liebe Grüße,
Simon

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.