/*
 * Demonstrates a few features of arrays in C.
 *
 * Author: Zach Tomaszewski.
 */

#include <stdio.h>
#include <stdlib.h>

void passArray(char[]);

/*
 * Creates a couple arrays and demonstrates the different syntax they require.
 */
void main() {
  int i;
  char* onheap;
  
  //array constructed with array initializer
  char onstack[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
  printf("onstack is at: %p\n", onstack);
  printf("size of onstack in main: %d\n", sizeof onstack);
  printf("onstack[6]: %c\n", onstack[6]);
  passArray(onstack);
    
  //equivalence of array indexing and pointer arithmetic
  for (i = 0; i < sizeof onstack; i++) {
    printf("%c ", onstack[i]);
  }
  puts("");
  
  //the following prints the same as above
  for (i = 0; i < sizeof onstack; i++) {
    printf("%c ", *(onstack + i));
  }
  puts("");  
  
  //creating an array in the heap
  onheap = malloc(sizeof(char[7]));
  //initialize it
  for (i = 0; i < 7; i++) {
    onheap[i] = 'A' + i;  //uppercase letters
  }
  //print it
  for (i = 0; i < 7; i++) {
    printf("%c ", onheap[i]);
  }  
  puts("");
  free(onheap);
}

/*
 * Prints the address and attempts to print the size of the passed array.
 */
void passArray(char from[]) {
  printf("from array is at: %p\n", from);
  //lose the size of an array when passed to a different fn
  printf("size of from in passArray: %d\n", sizeof from);  
}
