pointer to an enum?

hey gang,

i'm new here, and new to c++, although i've done lots of programming in other (simpler?) languages.

anyway, i'm trying to use a library on a mobile device to query the status of the wifi device.

in the header for the library, i have a typedef enum:

1
2
3
4
5
typedef enum {
    WIFI_STATUS_RADIO_OFF = 0,
    WIFI_STATUS_RADIO_ON = 1,
    WIFI_STATUS_BUSY = 2,
} wifi_status_t;


and a declaration:

WIFI_API int wifi_get_status(wifi_status_t *status);

I'm writing my own "wrapper" for it as:

1
2
3
4
5
6
7
8
9
10
int getWifiStatus();

int getWifiStatus() {
  wifi_status_t *status = 0;
  int result = 0
  result = wifi_get_status(*status);
  if (result == 0) {
  // i'd like to switch case on the various WIFI_STATUS_* states
  }
}


but i'm stuck on how to do a switch - if it's even possible, and how to handle/deal with the *status pointer (i admit, pointers still confuse the crap out of me!).

can anyone help? i'm kinda stuck.

thank you!

J
When you want to get the value the pointer is pointing to, you need to dereference.
wifi_get_status actually expects a pointer, so the dereferencing in line 6 is wrong.

This would be correct:
1
2
3
4
5
6
7
8
9
int getWifiStatus() {
  wifi_status_t status;
  int result = wifi_get_status(&status);
  if (result == 0) {
    switch(status) {
      ...
    }
  }
}
Last edited on
The function wfif_get_status() asks for a address to write the status to.. So give it the address of status:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
wifi_status_t status = 0;
Int result =0;
result = wifi_get_status( &status);
//status now holds the wifi status.

// now check state
If(result == 0)
{
  switch(status)
  {
  case WIFI_STATUS_RADIO_OFF:
    //handle off status
  break;
  case WIFI_STATUS_RADIO_ON:
    // handle on status
  break;
  case WIFI_STATUS_BUSY:
    // handle busy state
  break;
  default:
    //error
  break;
}
thanks! this is helpful!

i'm still not clear on what the variable status is though. i can't seem to print it with

fprintf(stdout, "%n\n", status);

since it's declared as a wifi_status_t, i'm just not clear on how i can use it in the switch/case.



to use %n in a printf you need to give the address of the variable:

fprintf(stdout, "%n\n", &status);

and why not just use the standard printf() ?
It works the same...

wifi_status_t is an enum type. and all values of a enum must be int .. so you can handle a enum 'almost' like a int..
Topic archived. No new replies allowed.