I believe the explicit null character \0 at the end of your string is redundant. The C compiler appends a null character when the string is stored in memory.

It is the null character that terminates the processing of the string. However, I think that a better means of coding the FOR loop would be to use the LEN() function to define the length of your string.

Example: for(a=pString;strlen(pString);a++)

You will need to include the header file "string.h" to make the function call to strlen() available.

I believe the explicit null character \0 at the end of your string is redundant. The C compiler appends a null character when the string is stored in memory.

It is the null character that terminates the processing of the string. However, I think that a better means of coding the FOR loop would be to use the LEN() function to define the length of your string.

Example: for(a=pString;strlen(pString);a++)

You will need to include the header file "string.h" to make strlen avaliable.

hgmjr

Click to expand...

As written this loop would be infinite since tha value of pString is not changed therefore strlen(pString) would always evaluate to a non zero constant for any non-null string pointed at by pString. You could fix the problem by saying

Code ( (Unknown Language)):

for(a=pString; strlen(a); a++)

This requires a function call and some considerable processing at each iteration of the loop. In the evaluation of expressions there is an implicit test for zero (FALSE) or non-zero (TRUE), but you can always improve the clarity of your code by making the comparison explicit as in

Code ( (Unknown Language)):

for(a=pString; *a != '\0'; a++)

This method has the advantage of being both clear and effcient.

Many people actually use the macro preprocessor to #define the following:

That's clumsy for a heap of reasons. pString is already a temporary variable that has cost RAM so there no need to copy it to another temp var. You could do this;

Code ( (Unknown Language)):

//*******************************

// RS232 Send string (unsigned)

//*******************************

void SendString(unsigned char* pString)

{

while(*pString)

{

SerialOut_To_PC(*pString);

pString++;

}

}

That saves 2 RAM (by removing the need for a 16bit pointer) but the compiler will still be doing the indirect addressing twice so it will still be slow like the example above it.

Code ( (Unknown Language)):

//*******************************

// RS232 Send string (unsigned)

//*******************************

void SendString(unsigned char* pString)

{

unsigned char temp;

while(1)

{

temp = *pString;

if(temp) SerialOut_To_PC(temp);

else break;

pString++;

}

}

This costs 1 RAM but only needs to do the indirect addressing once and then tests and passes only a char var so it will be much faster executing and smaller code than either example especially on a microcontroller (which is probable considering the "Serial_out_to_PC").

Given that any half-decent compiler will perform extensive optimisation you may as well write your source as clearly and concisely as possible and leave it up to the compiler of turn it into efficient code.
You will probably find that even though your source uses pointers the compiler will translate these to direct addressing in the final assembly version of the code so personally I would go with you #2 option.
I.E.