www.easyCPlusPlus.com

Easy C++

www.easyCPlusPlus.com

C Tutorial - Lesson 9: Arrays

Introduction to Arrays

by John Kopp

Support this site at no cost to you

Welcome to easyCPlusPlus.com's C tutorial. This lesson introduces arrays. Arrays are a data structure that is used to store a group of objects of the same type sequentially in memory. All the elements of an array must be the same data type, for example float, char, int, pointer to float, pointer to int, a structure or function. Structures provide a way to organize related data and will be studied in a later lesson. Functions provide a way to define a new operation. They are used to calculate a result or update parameters. Functions will be covered in a later lesson. The elements of an array are stored sequentially in memory. This allows convenient and powerful manipulation of array elements using pointers.

Defining Arrays
An array is defined with this syntax.

datatype arrayName[size];

Examples:

int ID[30];
              /* Could be used to store the ID numbers of students in a class */

float temperatures[31];
              /* Could be used to store the daily temperatures in a month */

char name[20];
              /* Could be used to store a character string.
              Character strings in C are terminated by the null character, '\0'.
              This will be discussed later in the this lesson. */

int *ptrs[10];
              /* An array holding 10 pointers to integer data */

unsigned short int[52];
              /* Holds 52 unsigned short integer values */

Previous Page       Next Page