twitter
    Find out what I'm doing, Follow Me :)

What Is An Array?

An array is a series of homogeneous pieces of data that are all identical in type, but the type can be quite complex as we will see when we get to the chapter of this tutorial discussing structures. A string is simply a special case of an array.

The best way to see these principles is by use of an example, so load the program chrstrg.c and display it on your monitor.

main( )
{
char name[5]; /* define a string of characters */
name[0] = ’D’;
name[1] = ’a’;
name[2] = ’v’;
name[3] = ’e’;
name[4] = 0; /* Null character - end of text */
printf("The name is %s\n",name);
printf("One letter is %c\n",name[2]);
printf("Part of the name is %s\n",&name[1]);
}



The first thing new is the line that defined a "char" type of data entity. The square brackets define an array subscript in C, and in the case of the data definition statement, the 5 in the brackets defines 5 data fields of type "char" all defined as the variable "name". In theC language, all subscripts start at 0 and increase by 1 each step up to the maximum which in this case is 4. Wetherefore have 5 "char" type variables named, "name[0]", "name[1]", "name[2]", "name[3]", and "name[4]". You must keep in mind that in C, the subscripts actually go from 0 to one less than the number defined in the definition statement.