#include <stdio.h>
#include <string.h>
//------------------------------------
// A Standard Hello World Function
//------------------------------------
void HelloWorld1()
{
//Here we simply use printf to display the text "1 - Hello World!"
printf("1 - Hello World!\n");
}
//------------------------------------
// Displaying Hello World from a String
//------------------------------------
void HelloWorld2()
{
//First we make an array of type char, 14 elements
//long. Why 14? Since the \n is only a single
//character (a carriage return character) shouldn't
//we only need 13? Nope. Strings are terminated by
//a NULL character. Without this we would run into
//problems when manipulating them (such as reading
//past the end into memory we dont own... Very bad.
char String[14];
//Now we use strcpy to copy the text to String
strcpy(String, "Hello World!\n");
//And print away
printf("2 - %s", String);
}
//------------------------------------
// Allocating Memory for Hello World
//------------------------------------
void HelloWorld3()
{
//Our variable is a pointer of type char. This means
//it will store a value that points to a location in
//memory. We assign pString to NULL by default even
//though we dont really need to. Since we'll be
//assigning pString a new value on the next line,
//this is pointless, but its a good habit to always
//set pointers to NULL before possible use.
char * pString = NULL;
//Now a pointer is useless unless it points to
//something (other than NULL that is). So we want
//it to point to a section of memory that we can
//use to store our Hello World string. What we need
//is 14 elements the size of a char. We use the 'new'
//operator to acomplish this. Notice that 'new' returns
//a pointer to the memory its giving us, so we say
//say 'pString = new...' so that pString points there.
pString = new char[14];
//Now check this out. We can copy text to our new
//memory using the pointer. Looks a lot like the
//previous functions use doesnt it?
strcpy(pString, "Hello World!\n");
//Yup, its easy to use.
printf("3 - %s", pString);
//Now comes a very important part. We need to give back
//the memory that we got by calling 'new'. If we forget
//to delete allocated memory, we get whats called a
//memory leak. If this function was called lots of times,
//or it had allocated a lot of memory each time, our
//program would quickly slow down and eventually crash.
//Memory keeps getting allocated, and your system isnt
//allowed to use it for anything else until its deleted.
delete [] pString;
}
//------------------------------------
// Passing Hello World to a function
//------------------------------------
void HelloWorld4(char * pString)
{
//This demonstrates passing a pointer to a function as an
//argument. If you look in main() you will notice that this
//function is called differently from the others:
//HelloWorld4("4 - Hello World!\n");
//Notice that we have to pass in something for pString to
//point to. If you dont pass anything to this function when
//its called, you will recieve an error.
//Now we can just use this variable like it was declared here
printf("4 - %s", pString);
}
//------------------------------------
// Combining some previous concepts and more
//------------------------------------
void HelloWorld5(char * pString)
{
//We passed in a pointer to a string just like the last
//example. But we also need a pointer to a string that
//is going to contain everything we put together in a
//minute.
char * pAlteredString = NULL;
//Allocate memory for pAlteredString. Its going to hold
//the contents of what pString points to as well as 8 more
//characters. But we dont know how big pString is! Thats
//where we use strlen. It gets the length of a string.
//strlen doesnt count the terminating NULL character so we
//have to make room for that in our calculations. Hence the
//+9 instead of the expected 8.
pAlteredString = new char[strlen(pString) + 9];
//Copy what pString points to over to the memory that
//pAlteredString points to.
strcpy(pAlteredString, pString);
//Very simular to strcpy, we now use strcat to copy some
//more text on the end of pAlteredString.
strcat(pAlteredString, " World!\n");
//Now print.
printf("5 - %s", pAlteredString);
//Of course, since we allocated memory, we have to delete it,
//just like before.
delete [] pAlteredString;
}
//------------------------------------
// Returning Hello World
//------------------------------------
char * HelloWorld6()
{
//Notice that this function isnt of type void. Its return
//value is a pointer of type char. In main we see this:
//printf( "6 - %s", HelloWorld6() );
//Since HelloWorld6 returns a pointer to char back to the
//caller, we can use the function just like a char * variable
//in this instance.
//Return this text
return "Hello World!\n";
}
//------------------------------------
// Error checking Hello World
//------------------------------------
int HelloWorld7(char * pString)
{
//Here, we see a lot of the same things we saw before, used
//in a slightly different manner. We're passed in a pointer
//to char, and the return type is of type int. Previous
//examples showed us how that works.
//Here we check to make sure that something actually got
//passed in. Remember that pString is just a pointer and in
//a more complicated program, its possible that pString might
//not be pointing to anything yet, which would make it very
//evil for us to be using. This displays one important reason
//for setting pointers to NULL when they're declared. It makes
//it easy to check if theres any memory where its pointing.
if (pString != NULL)
{
//If pString does point to something, then we print it
printf("7 - %s", pString);
}
else
{
//If pString doesnt point to anything then we print this
//error message.
printf("Invalid parameter! pString is not an initialised pointer!\n");
//Then we exit the function by calling return. We're going
//to return the value 1. Go have a look in main to see how
//we're going to handle what happens to this value.
return 1;
}
//Just for kicks we're going to check to see if the text is
//what we expect it to be. We use strcmp to compare pString
//to what we're expecting.
if (strcmp(pString, "Hello World!\n") != 0)
{
//If the text wasn't exactly "Hello World!\n", then we're
//going to print an error about it and return 1 to the
//calling function to report our error.
printf("Invalid parameter! pString contains invalid data!\n");
return 1;
}
//It looks like everything worked out fine. We're going to
//return 0 to tell the calling function that everything is
//going smoothly.
return 0;
}
//------------------------------------
// Disecting Hello World
//------------------------------------
void HelloWorld8(char * pString, unsigned int len)
{
//We pass two arguments to this function. The first is a
//pointer to type char as we've seem a million times before.
//The second is an integer (int) which is going to tell us
//the length of the text that pString points to.
//Looking in main we see:
//HelloWorld8("Hello World! - With Garbage!!!\n", 12);
//Notice that we're passing in a really long string, but that
//only the "Hello World!" part gets displayed. That because
//we're passing in the len value as 12, and this function will
//only display that many characters of the string. Try changing
//that value to see it in action...
//We need to keep track of what element we're printing so we
//need a variable for that.
unsigned int i;
//By default display which function number we're on first:
printf("8 - ");
//If someone set len up to be longer than the string they passed
//in, that would get messy. We would be accessing memory that we
//dont own, and thats something we never want to do. So we truncate
//len if its longer than the total character in pString.
if (len > strlen(pString))
len = strlen(pString);
//Now we loop through the text displaying 'len' # of characters.
for(i = 0; i < len; i ++)
{
//Since pString is a pointer to n elements of type char, we
//can use the [] operator to move through the elements in
//memory. Its exactly like the elements in an array, we're
//just seeing it at a more naked level.
printf("%c", pString[i]);
}
//Put a carriage return at the end of everything.
printf("\n");
}
//------------------------------------
// Figuring out Hello World
//------------------------------------
void HelloWorld9(char * pString)
{
//This is a lot like the previous function, but notice that
//we have no idea how much we're supposed to display. We're
//going to keep reading the memory pointed to by pString
//until we reach the end. But how do we know when we get to
//the end? NULL terminating character to the resuce!
//We need to set our counting variable to 0 here since we're
//not using a for loop to do it.
unsigned int i = 0;
//By default display which function number we're on first:
printf("9 - ");
//A do...while loop. This will keep looping until the
//condition in the 'while' enclosing brackets is true.
//In this case that means when the element of memory we
//just moved to is NULL. Everything else works just like
//in the previous example. Print each character then move
//to the next element and repeat.
do
{
printf("%c", pString[i]);
i ++;
}while(pString[i] != NULL);
}
//------------------------------------
// MAIN
//------------------------------------
int main (void)
{
HelloWorld1();
HelloWorld2();
HelloWorld3();
HelloWorld4("Hello World!\n");
HelloWorld5("Hello");
printf( "6 - %s", HelloWorld6() );
if (HelloWorld7("Hello World!\n") != 0)
{
printf("HelloWorld7 function failed!\n");
return 1;
}
HelloWorld8("Hello World! - With Garbage!!!\n", 12);
HelloWorld9("Hello World!\n");
return 0;
}