Function crushes when run on 32Bit MPU [SOLVED]


I have recompiled my sketch that normally runs on a MEGA for arduino zero.
Had to make several changes mainly in valriables to make it work and I am now stuck to the following function which crushes on Arduino Zero.

void processReceivedResponse(){

int16_t comnd=0;
char *pch;

    if (strstr(HTTP_req, "&St")) {
      pch=strstr(HTTP_req, "&St");
       sscanf(pch,"%*3c%d",&comnd);     // <---- crushes on this line
        #ifdef debugWeb
         PORT_S.print(F("HTTP RCVD Command :"));
         PORT_S.println (comnd);
        if (comnd>0 ) { executeCommand(comnd);}
    if (strstr(HTTP_req, "&UtdButtons")) {  updateDeviceStatus();} 
    if (strstr(HTTP_req, "&Wl")) {
      pch=strstr(HTTP_req, "&Wl");
        #ifdef debugWeb
         PORT_S.print(F("HTTP RCVD Command :Water Level Margin Set"));
         PORT_S.println (comnd);
        if (comnd>0 && comnd<13) {sendUniversalSwitch(liquidID[1], comnd);}

I know that this is not a complete sketch and its probably hard to pinpoint the problem from just one function, but it would be impossible for anyone toto go through 100s of lines of code.
I have managed to narrow down the line were the code above crushes which is :


I suspect that this has something to do with the difference of length of variables in ARM architecture when compared to the AVR.

Can anyone tell whats wrong from just this snippet?

What size variable does the %d sscanf specifier expect it will be writing to on a 32-bit processor?

I think you're right about different size types. An int is 16-bits on AVR, so %d wants to write 16-bits. On ARM, an int is 32-bits and doesn't fit into comnd. Perhaps scanning for a half-word might help: %dh?

Easier to make 'comnd' an int32_t?

That processor is 32 bits so a native 'int' will also be 32 bits which is what the "%d" is expecting but you have provided the address of a 16 bit variable.
Either declare comnd as 'int' or adjust your format to use a length modifier

You could change the format to "%*3c%hd" for 'short int'. That might even work for int16_t on both AVR and 32-bit processors.

Thanks John! Good idea.

Will try it out

It worked! Thanks for your help!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.