/* 
* 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. 
*/

/* to declare a string 
* static memory allocaiton - at compile time 
*/
const int MAX_STRING_SIZE = 10;

/* char stringName [ size ] */
char myString[MAX_STRING_SIZE];

/* This can hold a string MAX_STRING_SIZE - 1 long 
 * ( we need one character for the NULL terminator \0 )
 */

/* Alternativly 
 * Note: that this string cannot be changed, as it is statically declared
 */
char myString[] = "A static string";

/* String Handling functions 
 * Functions for handling strings are found in the string.h header file, be sure to include this 
 */

#include <string.h>

/* Working with strings - short example */

#include <stdio.h>
#include <string.h>

int main(char* argv, int argc)
{
    /* Declare string size*/
    const int MAX_STRING_SIZE = 10;

    /* first string  */
    char aString[MAX_STRING_SIZE];

    /* another stirng */
    char anotherString[MAX_STRING_SIZE];

    /* Reading from keyboard 
     * Note: We don't need the & the string is an array, and therefore 
     * already a pointer
     */
    scanf("%s",aString); 

    /* Setting the string to some constant value */
    strcpy(anotherString, "bob");

    /* outputing strings */
    printf("%s\n", aString);
    printf("hey look %s\n", anotherString);

    /* adding to a string */ 
    concat(anotherString, ".txt");

    printf("%s\n", anotherString);
    
    exit(0);
}

/* Array alloation - dynamic 
* Use this when you don't know in adavance how bit the string will be, 
* and there is no safe maximum.  
*/

/* Must include stdlib for malloc/free/new/delete */
#include <stdlib.h>

/* take a non-constant from somewhere, e.g. keyboard, file etc. */
int size = 10;  

/* from the example above we might calculate it based on some other strings */
size = strlen(aString) + strlen(anotherString);

/* using C */

/* v pointer          v cast             v memory required for one */
char* dynamicCString = (int*)malloc(size * sizeof(char));

/* use the string   .... */

/* free at the end and set pointer to NULL to avoid accidental re-use */
free(dynamicCString);
dynamicCString = NULL;

/* using C++ */
int *dynamicCppString = new char[size];

/* use the string   .... */

/* delete at the end and set pointer to NULL to avoid accidental re-use */
delete [] dynamicCppString;
dynamicCppString = NULL;

/* Array of Arrays 
 * To create an array of strings, we need a pointer to a pointer. 
 */

char** listOfNames;

/* Allocate the list */
listOfNames = (char**)malloc(numberOfNames * sizeof(char*));

/* Allocate memory for ONE of the names 
 * would probably need to do this in a loop or after reading a name to a buffer 
 */

listOfNames[0] = (char*)malloc(sizeOfName * sizeof(char));

/* if we use a buffer, we could read into the buffer and set the size of the name based on this */ 

const int MAX_STRING_SIZE = 256;
char buffer[MAX_STRING_SIZE];

scanf("%s",buffer);

int lenght = strlen(sizeOfBuffer) + 1;

listOfNames[1] = (char*)malloc(lenght * sizeof(char));


