///////////////////////////////////////////////////////////////////
//  Some examples with arrays, pointers and functions
///////////////////////////////////////////////////////////////////

#define SIZE 20

int x[SIZE], y[SIZE];
int *px = x, *py = y;           // Second variable also needs a "*" if it is also a pointer
int *px = &x[0], *py = &y[0];   // Same thing
     px = x;        // two examples of initializing px outside the declaration
     px = &x[0];

// Initialize all array elements to a value -- indexing version
void initArray(int size, int x[], int value) {
	int i;
	for (i=0; i<size; i++) {
		x[i] = value;
	}
}

// initArray using pointer:
void initArray(int size, int *px, int value) {
	int *p = 0;
	for (p=px; p - px < size; p++) {
		*p = value;
	}
}

// Calling either of the above functions
initArray(SIZE, y, 7);		// These will have the same effect with the above
initArray(SIZE, &y[0], 7);	// Any of these can be used with either version of initArray
initArray(SIZE, py, 7);

**********************************************************************************


// Add one to all elements in array:
void plusOne(int size, int x[]) {
	int i;
	for (i=0; i<size; i++) {
		x[i]++;
	}
}

// Add one to all elements in array, pointer version
void plusOne(int size, int *px) {
	int *p = 0;
	for (p=px; p - px < size; p++) {
		(*p)++;			// parentheses are required (otherwise p, not *p, gets incremented)
	}
}

**********************************************************************************

// Add array "a" to array "b" element by element
void addArrays(int size, const int a[], int b[]) {		// Note const
	int i;
	for (i=0; i<size; i++) {
		b[i] += a[i];
	}
}

// Add array "a" to array "b" element by element - pointer version
void addArrays(int size, const int *pa, int *pb) {		// Note const
	int *ptr_a = 0, *ptr_b = 0;
	for (ptr_a = pa, ptr_b = pb; ptr_a - pa < size; ptr_a++, ptr_b++) {
		*ptr_b += *ptr_a;
	}
}

// Add array "a" to array "b" element by element - alternate pointer version
void addArrays(int size, const int *pa, int *pb) {		// Note const - first array won't change
	int *ptr_a = pa, *ptr_b = pb;
	while (ptr_a-pa < size) {			// Could instead say while (ptr_a < pa + size)
		*ptr_b++ += *ptr_a++;		// Note precedence -- the pointer gets incremented
	}
}

const int x[] = {2, 3, 5, 7, 11, 13, 17, 19};
int y[8] = {0};

addArrays(8, x, y);			// This works - adding a constant array x to array y
addArrays(8, y, x);			// This doesn't work - trying to add to a const array

**********************************************************************************

// toUpper();
// Replace all 'a' - 'z' with 'A' -'Z' in a NULL terminated string
// Return the index of the last 'a' - 'z' (lowercase) found;

char toUpper(char str[]) {
	char i, lasti = 0;
	for (i=0; str[i] != '\0'; i++) {
		if (str[i] >= 'a' && str[i] <= 'z') {
			str[i] -= ('a' - 'A');
			lasti = i;
		}
	}
	return lasti;
}

char toUpper(char *str) {
	char *ptr, *ptr_last = str;	// 
	for (ptr=str; *ptr != '\0'; ptr++) {
		if (*ptr >= 'a' && *ptr <= 'z') {
			*ptr -= ('a' - 'A');
			ptr_last = ptr;
		}
	}
	return ptr_last - str;
}

char mystring[] = "Here is a string!!!";	// note automatically NULL terminated
int lastLowerLetter = toUpper(mystring);

