/* 
* Code Snippets for this week
* Small programs to show bits of C and bits of C. 
* >> I've tried to put * at the start of all the comment lines. 
*/

/* 
* Passing by reference
*
*/

/* 
* Decleration must state that we're passing a pointer i.e. type* (e.g. int*)
*/ 

/* prototype - top of file, before main */
int myFunctionName(int);

/* funciton body, note must match its prototype in terms of retrun type and paramters */
int myFunctionName(int *myParamter)
{
    /* Local variable */
    int x;

    /* To get the value pointed to by the pointer use * to dereference */
    x = *myParamter;

    /* same to change */
    *myParamter = x * 2;

    /*must end with a return - of the correct type */
    return x;
}

/*
* Calling a  function with a paramter passed by reference. 
* - use function name followed by a paramter list
*/
int myIntCalledA = 6;

int variableForTheReturnValue = myFunctionName(&myIntCalledA);

*/
* Arrays/strings
* - These are already pointers so no need for &
*/

int stringLenght = strlen(myString);

/* Working with functions - short example */

#include <stdio.h>

/* count string chars */
int countString(char*);

int main(char* argv, int argc)
{
    const int STRING_MAX = 32;
    char myString[STRING_MAX];    
    int result = countString(myString);

    printf("%d\n", result);
    exit(0);
}

int countString(char* aString)
{
    int localCount = 0;
    while(aString[localCount] != '\0')
    {
        localCount++;
    }
    return localCount;
}

