Code examples


// Convert a binary number of value 0-99 into its ASCII equivalent characters.

unsigned char str[2];

void convertTwoDigBinToASCII(uint8_t bin, uint8_t* str)
{
    str[0] = '0';
    if (bin >= 10)
    {
        str[0] = (bin / 10) + 48;
    }
    str[1] = (bin % 10) + 48;
}
//Usage:
convertDateBinToASCII(str);	
	

Character pointer to keep track of a string

char label[] = "John";       // Initialize label array with John string
char label2[10] = "Oscar";   // Initialize label array with Oscar string

char *labelPtr;
labelPtr = label;	
	

Suppose that the array label, that stores "John," starts at memory address 2000 - label @2000.
The string is stored in memory as follows:

label @2000

|2000|2001|2002|2003|2004|
|----|----|----|----|----|
| J  | o  | h  | n  | \0 |
|--- |----|----|----|----|

label2 @3000

|3000|3001|3002|3003|3004|3005|
|----|----|----|----|----|----|---|---|---|---|
| O  | s  | c  | a  | r  | \0 |   |   |   |   |
|----|----|----|----|----|----|---|---|---|---|

One character is needed to store the nul character (\0), which indicates the end of the string.

A disadvantage of creating strings using the character array syntax is that you must say ahead of time how many characters the array may hold. For example, in the following array definitions, we state the number of characters (either implicitly or explicitly) to be allocated for the array.

char label[] = "Single"; // 7 characters
char label[10] = "Single";

Thus, you must specify the maximum number of characters you will ever need to store in an array. This type of array allocation, where the size of the array is determined at compile-time, is called static allocation.

Strings as pointers

Another way of accessing a contiguous chunk of memory, instead of with an array, is with a pointer.
Since we are talking about strings, which are made up of characters, we'll be using pointers to characters, or rather, char * 's.

However, pointers only hold an address, they cannot hold all the characters in a character array. This means that when we use a char * to keep track of a string, the character array containing the string must already exist (having been either statically- or dynamically-allocated).

char label[] = "Single";
char label2[10] = "Married";
char *labelPtr;
labelPtr = label;

We would have something like the following in memory

label @2000

|2000|2001|2002|2003|2004|2005|2006|
|----|----|----|----|----|----|----|
| S  | i  | n  | g  | l  | e  | \0 |
|--- |----|----|----|----|----|----|

label2 @3000

|3000|3001|3002|3003|3004|3005|3006|3007|
|----|----|----|----|----|----|----|----|---|---|
| M  | a  | r  | r  | i  | e  | d  | \0 |   |   |
|----|----|----|----|----|----|----|----|---|---|

labelPtr @4000

|2000|
|----|
| S  |
|--- |

Since we assigned the pointer the address of an array of characters, the pointer must be a character pointer--the types must match.
Also, to assign the address of an array to a pointer, we do not use the addressof (&) operator since the name of an array (like label) behaves like the address of that array in this context.