Contents


On-line Guides
All Guides
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems

How To Guides
Xen Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions

 

 

6.3. Declaring and Accessing Arrays

Declaring an array is much the same as declaring any other variable except that you must specify the array size. The size (or number of elements) is an integer value placed in square brackets after the arrays identifier.

Example 6-1. first_arrays.c

int
main()
{
  int person[10];
  float hourly_wage[4] = {2, 4.9, 10, 123.456};
  int index;

  index = 4;
  person[index] = 56;

  printf("the %dth person is number %d and earns $%f an hour\n",
         (index + 1), person[index], hourly_wage[index]);

  return 0;
}
      

NOTE: it is up to you to make sure you don't try to access an element that is not in the array such as the eleventh element of a ten element array. Attempting to access a value past the end of an array will either crash your program or worse, it could retrieve garbage data without telling you that an error occurred.

 
 
  Published under the terms of the GNU General Public License Design by Interspire