dereferencing array of pointer to struct pass to a function

Hi. I have trouble to use an array of pointer that refer to a struct and access data. it compile but when a function try to access data of the struct, the arduino crash and the sketch restart from the beginning.

  struct uHTTP_request {
  uHTTP_request( String uId, uint8_t uMethod, void (*uCallback)() );
  String id;
  uint8_t method;
  void (*callback)();
};

uint8_t uHTTP_request_REQUEST_COUNTER = 0;

uHTTP_request::uHTTP_request(String uId, uint8_t uMethod, void (*uCallback)() ){
  id = uId;
  method = uMethod;
  callback = uCallback;
  uHTTP_request_REQUEST_COUNTER++;
}

instance example and array declaration:

uHTTP_request switchAlarms_PUT("switchAlarms", uHTTP_METHOD_PUT, parseJSONswitchAlarms );
uHTTP_request configs_PUT("configs", uHTTP_METHOD_PUT, parseJSONConfigs );



uHTTP_request *requestIndex[] = {&ajax_inputs_GET, &ajax_alarms_GET, &configs_GET , &channels_PUT, &alarms_PUT, &switch_PUT, &switchAlarms_PUT, &configs_PUT};

then, I call a function that access the info inside the struc, but it crash.

void uHTTP::requestHandler(struct uHTTP_request *requestIndex){
   Serial.println(requestIndex[i].id);
}

uHTTPserver->requestHandler(*requestIndex);

I tried a print statement for debugging but have the same bug. If I mute it, it run normally.

I believe it have to do with the deference I've use. At first I thought I have to use arrow operator, but it didn't compile.

nitrof:

void uHTTP::requestHandler(struct uHTTP_request *requestIndex){

Serial.println(requestIndex[i].id);
}

Usually you would not access a structure pointer like an array of structure pointers.

Try:

void uHTTP::requestHandler(struct uHTTP_request *requestIndex){
   Serial.println(requestIndex->id);
}

The '->' operator means that the thing on the left is a structure (or object) pointer and the thing on the right is a member of that structure (or object).

Don't use String. Use a char with a #defined size.

Call it a class if it's a class. Usually structs don't have constructor functions.

void uHTTP::requestHandler(struct uHTTP_request *requestIndex){
Serial.println(requestIndex[i].id);

You should merge with your other thread… Would bebetter

thank all. i’m in debug mode. I think I found my mistake. It was in the argument I want to pass.

I wanted to pass an array of struct pointer… so this is my function:

void uHTTP::requestHandler(uHTTP_request *requestIndex[])

the the object I want to pass add the calling function;

uHTTP_request *requestIndex[] = {&ajax_inputs_GET, &ajax_alarms_GET, &configs_GET , &channels_PUT, &alarms_PUT, &switch_PUT, &switchAlarms_PUT, &configs_PUT};


uHTTPserver->requestHandler(requestIndex);

then, when I try to acces data in each struct pass to the function I use arrow operator as expected at first… :stuck_out_tongue:

Serial.println(requestIndex[i]->id);

at least I can now print the id… the rest still a bit buggy but I working on it :stuck_out_tongue:

@MorganS the difference between struct and class still not clear, I though it was only that class have private member by default… but I take the advise ( and maybe some of you godfather can do a little topic on this… :P)

at J-M-L … will add the like to ma other topic…

keep you posted !!!

thak allz !!

YES !! confirm.

after a little glitch correction. It work weel.

so this is what the final function look like. but it missing a 404 error that I have not yet find how I will do it…

void uHTTP::requestHandler(uHTTP_request *requestIndex[]){

  char url[uHTTP_URI_SIZE];
  if (*response = available()) {

    if (uri("/") ) {
      if (method(uHTTP_METHOD_GET)) {
        strcpy(url, home_page); // if nothing requested, send default page
        send_headers(200);
        webFile_Post(url); //send default page
      }
      else if (method(uHTTP_METHOD_OPTIONS)) {
        send_headers(200);
      }
    }
    else {
      //scan request enumerator
      bool requestFound = false;
      for (int i = 0; i < uHTTP_request_REQUEST_COUNTER; i++){
        if( (method(requestIndex[i]->method)) && (strcmp(this->uri(1), requestIndex[i]->id) == 0) ){
          requestIndex[i]->callback();
          requestFound = true;
          break;
        }
      }

      if(!requestFound){
        strcpy(url, uri());
        webFile_Post(url); //send other file if they exist on sd card
      }    
    }
    response->stop();
  }
}