Arrays.

Arrays can be created from any of the C data types int, float, char. I start with int and float as these are the easiest to understand. Then move onto int and float multi dimentional arrays and finally char arrays


int and float arrays

To define an integer or floating point variable you would say.

	main()
	{
	  int count;		/* interger variable 		*/
	  float miles;		/* floating point variable	*/
	}

The syntax for an array is:

	main()
	{
	  int count[5];		/* interger 5 element array	*/
	  float miles[10];	/* floating point 10 element array */
	}

Now, the important fact is that the elements start at 0 (ZERO), so, 'count' above has elements 0, 1, 2, 3, 4.

To change the value of the 5th element we would code:

	main()
	{
	  int count[5];
	  count[4] =index.html 20;		/* code 4 to update the 5th element */
	}

If we want to initalise 'count' at definition we can say:

	main()
	{
	  int i;
	  int count[5]={10, 20, 30};
	  for (i=0; i

The result will be:
	10 20 30 0 0 
We can see from the example above that the elements that have NOT been initalised have been set to zero. One last thing to show you. If all the elements are being initialized when the variable is being declared, the compiler can work out the number of elements for you. So this example will create an array with 3 elements.
	main()
	{
	  int i;
	  int count[]={10, 20, 30};
	}
Dont forget all the stuff so far also applies to float.

Two dimensional int and float arrays.

C does not actually support multi-dimensional arrays, but you can emulate them.
	main()
	{
	  int count[5];
	  int matrix[10][4];
	}
count has been seen before, it defines an array of 5 elements. matrix is defined as 10 arrays, all with 4 elements. To initalise matrix at definition, you would code the following.
	main()
	{
	  int thingy[4][2]={{1, 2},
	  		    {3, 4},
			    {5, 6},
			    {7, 8}};
	}
Dont forget the last element will be thingy[3][1]

char arrays.

char arrays work in the same way as int and float
	main()
	{
	  char letter;
	  char letters[10];
	}
'letter' can only hold one character but 'the 'letters' array could hold 10. It is important to think of 'char' as an array and NOT a string. To initalise 'char' variables you can use the following syntax.
	main()
	{
	  char letter='x';
	  char letters[10]='f','a','x',' ','m','o','d','e','m','\0';
	  char text[10]="fax modem";
	}
Note that the double quotes mean 'text string' and so they will add the NULL automatically. This is the only time that text can be loaded into an array in this way. If you want to change the contents of the array you should use the function strcpy.

Two dimensional char arrays.

Two dimensional arrays are a different kettle of fish! We can follow the rules above to define the array as below but it does not appear to be much use!
	main()
	{
	  char colours[][6]={"red","green","blue"};
	}
Note we have specified 6 characters as a NULL will be added to the end of each string. The code above works, but you dont seem to be able to access any of the strings! characters can be extracted as below:
	main()
	{
	  char colours[][6]={"red","green","blue"};
	  printf ("%c \n", colours[0][0]);
	}
We don't have any pointers to the strings. To see how to get around this problem click here for chat on pointers.

Top

Master Index

Keywords

Functions


Martin Leslie 02-Apr-94