For Loop Question, syntax questions and fix needed

I received help on another topic and this included the for loop in the first function below. I tried using the same technique for unsigned long in the second function below.

The comments in the code are mine, just trying to understand what is going on. Also I'm a hardware guy, no formal programming education.

The below print function prints three values not 2. This seems to imply this code syntax is meant for character arrays and is expected a null character at the end? Will this for loop style work with long type as well as char types?

How can this be fixed.

// this is the code example followed to try coding the print() function below

void copyArrayAtoO(char a[], char out[]) {
	char *q;		// char input arrays pointer
	char *p = out;	// assign output array pointer
	
	// assign char a[] to q, *q ptr for char a[], inc char a[] ptr, inc out ptr
	for (q = a; *q; q++, p++)
		*p = *q; 
	
	*p = '\0'; // append null character
}
// code that needs help

unsigned long A[] = { 655390, 1310780 };

// call like this
print(A);

void print( unsigned long A[] )  {
	unsigned long *a; // pointer 
	
	for (a = A; *a; a++)  {
		Serial.println(*a);
	}		
}

You're relying on a char string being automatically terminated with a zero byte.
There is no such termination for other arrays.

To fix this, you need to pass the size of the array to the function.

What is c?

Note also that even a simple char array would fail your first function...

char array [] { 'O', 'o', 'p', 's'  };

You guessed right. The only way the first function can know the length of the array is that the for loop tests for a terminating null. You have to pass the length of the array to the second function.

To trick it into working just for a demo:

unsigned long A[] = { 655390, 1310780, 0 };

... but such a print function couldn't print zeroes.

c is a typo, should be a. I simplified the code to bare issue

Your program stopped printing for the same reason the example you worked from.

It printed until it was pointing to some memory that appeared to be 0 when interpreted as a long integer.

So you could do the same thing, put a zero at the end of the kist of values you want to print.

In the case of characters, no one wants or needs to print a character whose value is zero, that’s why it can enjoy special use as a terminator.

You might miss not being able to use 0 when dealing with integers, however.

Sometimes another special value is named, like 9999 as a value never expected to be needed, therefor usable as a flag to mean something different, in this case like you have reached the end of “real” values in a list.

@aarg gmta

a7

You're relying on a char string being automatically terminated with a zero byte.
There is no such termination for other arrays.

I see, so length is needed unless 0 could be used as a termination value. I'll pass the length.

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