Monthly Archives: November 2014

char arrays and null terminated strings

Character arrays are designated by

Hence, we make an array of characters called “myName” with 10 elements:


Any left over elements that are not taken up by the initialization will have ” or NULL filled in.

Using pointer to display string

myName is actually a const pointer to the first element of the array. When we use cout to display char arrays, it displays all the data from the pointer up until the terminating NULL. Since our pointer is currently at the beginning of the array (R), cout will display the full name Ricky.

Adding a byte (1) to the pointer will display one character further down the array. Note that a char is equal a byte.
Adding n bytes will display characters further down the array.

my name is: Ricky
my name is: icky
my name is: cky
my name is: ky
my name is: y

Using address of element to display string

myName[0] gets you the first element ‘R’.

If you use &myName[0], it gets you the ADDRESS of the first element. Hence, the compiler will display all data from that address up to a NULL. Hence &myName[0] will display ‘Ricky’ also.

&myName[0] displays: Ricky, myName[0] is: R
&myName[1] displays: icky, myName[1] is: i
&myName[2] displays: cky, myName[2] is: c
&myName[3] displays: ky, myName[3] is: k
&myName[4] displays: y, myName[4] is: y

What if the element is NULL?

A better way to display an element is to check for NULL

In our case index 5 and on would give us a NULL.

Getting the address

Use void pointer to get the memory location.
std::cout << (void*)&myName[0] << std::endl; std::cout << (void*)&myName[1] << std::endl; std::cout << (void*)&myName[2] << std::endl; std::cout << (void*)&myName[3] << std::endl; std::cout << (void*)&myName[4] << std::endl; std::cout << (void*)&myName[5] << std::endl; Result: 0x7fff5fbff86e 0x7fff5fbff86f 0x7fff5fbff870 0x7fff5fbff871 0x7fff5fbff872 0x7fff5fbff873 Remember that hex is 0 - 9, a - f and each increment is a byte. Hence R is at address 0x7fff5fbff86e
i is at address 0x7fff5fbff86f
c is at address 0x7fff5fbff860
k is at address 0x7fff5fbff861

where each char is a byte. Also notice that the string terminating NULL also has a address at 0x7fff5fbff873.

void pointer

A void pointer, (void *) is a raw pointer to some memory location.
The type void * simply means “a pointer into memory; I don’t know what sort of data is there”.

When you stream the address of a char to an ostream, it interprets that as being the address of the first character of an ASCIIZ “C-style” string, and tries to print the presumed string. You don’t have a NUL terminator, so the output will keep trying to read from memory until it happens to find one or the OS shuts it down for trying to read from an invalid address. All the garbage it scans over will be sent to your output.

When you are taking the address of i, you get char *.
operator<< interprets that as a C string, and tries to print a character sequence instead of its address.


An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.

That means that, for example, five values of type int can be declared as an array without having to declare 5 different variables (each with its own identifier). Instead, using an array, the five int values are stored in contiguous memory locations, and all five can be accessed using the same identifier, with the proper index.

For example, an array containing 5 integer values of type int called foo could be represented as:

where each blank panel represents an element of the array. In this case, these are values of type int. These elements are numbered from 0 to 4, being 0 the first and 4 the last; In C++, the first element in an array is always numbered with a zero (not a one), no matter its length.

Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is:

type name [elements];

where type is a valid type (such as int, float…), name is a valid identifier and the elements field (which is always enclosed in square brackets []), specifies the length of the array in terms of the number of elements.

Therefore, the foo array, with five elements of type int, can be declared as:

Initializing Arrays

By default, regular arrays of local scope (for example, those declared within a function) are left uninitialized. This means that none of its elements are set to any particular value; their contents are undetermined at the point the array is declared.

But the elements in an array can be explicitly initialized to specific values when it is declared, by enclosing those initial values in braces {}. For example:

int foo [5] = { 16, 2, 77, 40, 12071 };

This statement declares an array that can be represented like this:

The number of values between braces {} shall not be greater than the number of elements in the array. For example, in the example above, foo was declared having 5 elements (as specified by the number enclosed in square brackets, []), and the braces {} contained exactly 5 values, one for each element. If declared with less, the remaining elements are set to their default values (which for fundamental types, means they are filled with zeroes). For example:

int bar [5] = { 10, 20, 30 };

Will create an array like this:

async to true/false

You set async to false, when you need that ajax request to be completed before the browser passes to other codes:

Does it have something to do with preventing other events on the page from firing?


Setting async to false means that the statement you are calling has to complete before the next statement in your function can be called. If you set async: true then that statement will begin it’s execution and the next statement will be called regardless of whether the async statement has completed yet.

Start a Simple HTTP Server in Any Folder

Start a Simple HTTP Server in Any Folder

If you’re needing to quickly test some HTML that you’re working on, start a simple web server within any folder on your Mac. Navigate to the folder to use and enter:

$ python -m SimpleHTTPServer 8000

The number at the end is the port to use, open your browser and visit http://localhost:8000 You can use the default of port 80 if you wish and remove the port number entirely.

When you’re finished, simply press Ctrl-C.