Global char array or pointer for functions.

I am working on a program that reads a serial input message from a sensor console.
Then:

  • in a subroutine trailing spaces are removed.
  • another subroutine changes the message to one more understandable.
    all working on/with the same char array with each sub making its required changes.

This approach seems a bit crude (i.e. relying on a global to deal with "passing" to the subs).

My question is:
Should I be using pointers to "pass" the array to the subroutines? Or is it just as good simply using the global?

For me this is both a functional and learning experience so I tend to recreate some wheels by using my own subroutines.

Thanks
John

char Inputbuffer[40] = ""

// if there is data available from the sensor console, process it
   if (Serial.available() > 0)
     {
    getConsoleMessage();    // Fills Inputbuffer with alarm message and then trims whitespaces
    }  //  .... if Serial.available

     trimEndSpaces();
     reFormatMessage();


//*** subroutines *************************************************************
//*****************************************************************************

void trimEndSpaces(){

int8_t i;
i=strlen(Inputbuffer)-2;    // the -2 to shift to 0 based and so we don't test the \0
while (Inputbuffer[i] = " ")
  {
  i--;
  if(i=3) break;
  }
Inputbuffer[i] = \0
}
  if(i=3) break;

I think you meant:

  if(i==3) break;

Also...

Missing semicolon...

char Inputbuffer[40] = ""

Single equals is assignment. You want double equals for equality test. Also double quotes are strings, you want single quotes for comparing a char type...

while (Inputbuffer[i] = " ")

Missing a semicolon. Also, what's the backslash all about? That needs to be inside single quotes if your trying for an escaped ASCIII NUL...

nputbuffer[i] = \0

Thanks for the pointers.

So should I assume using a global array to allow my subroutines to manipulate the contests is no worse or better than passing pointers?

JohnRob:
So should I assume using a global array to allow my subroutines to manipulate the contests is no worse or better than passing pointers?

If the functions only ever work on one string, using a global is fine. If you have a function that you want to use on various strings, passing a pointer is better than having multiple functions that do the same operation on different global variables.