How to Use Arrays in C Programming

In the real world, information comes individually or in groups. You may find a penny on the road and then a nickel and maybe a quarter! To handle such fortunes in the C language, you need a way to gather variables of a similar type into groups. A row of variables would be nice, as would a queue. The word used in C is array.

How to initialize an array

As with any variable in C, you can initialize an array when it’s declared. The initialization requires a special format, similar to this statement:

int highscore[] = { 750, 699, 675 };

The number in the square brackets isn’t necessary when you initialize an array, as shown in the preceding example. That’s because the compiler is smart enough to count the elements and configure the array automatically.

Exercise 1: Write a program that displays the stock market closing numbers for the past five days. Use an initialized array, marketclose[], to hold the values. The output should look something like this:

Stock Market Close
Day 1: 14450.06
Day 2: 14458.62
Day 3: 14539.14
Day 4: 14514.11
Day 5: 14452.06

Exercise 2: Write a program that uses two arrays. The first array is initialized to the values 10, 12, 14, 15, 16, 18, and 20. The second array is the same size but not initialized. In the code, fill the second array with the square root of each of the values from the first array. Display the results.

How to use character arrays (strings)

You can create an array using any of the C language’s standard variable types. A char array, however, is a little different: It’s a string.

As with any array, you can declare a char array initialized or not. The format for an initialized char array can look like this:

char text[] = "A lovely array";

The array size is calculated by the compiler, so you don’t need to set a value in the square brackets. Also — and most importantly — the compiler adds the final character in the string, a null character: \0.

You can also declare the array as you would declare an array of values, though it’s kind of an insane format:

char text[] = { 'A', ' ', 'l', 'o', 'v', 'e', 'l', 'y', ' ', 'a', 'r', 'r', 'a', 'y', '\0' };

Each array element in the preceding line is defined as its own char value, including the \0 character that terminates the string. No, you’ll find the double quote method far more effective at declaring strings.

The code in Displaying a Char Array plods through the char array one character at a time. The index variable is used as, well, the index. The while loop spins until the \0 character at the end of the string is encountered. A final putchar() function (in Line 14) kicks in a newline.

DISPLAYING A CHAR ARRAY

#include <stdio.h>
int main()
{
 char sentence[] = "Random text";
 int index;
 index = 0;
 while(sentence[index] != '\0')
 {
 putchar(sentence[index]);
 index++;
 }
 putchar('\n');
 return(0);
}

Exercise 3: Type the source code from Displaying a Char Array into your editor. Build and run the program.

The while loop in Displaying a Char Array is quite similar to most string display routines found in the C library. These functions probably use pointers instead of arrays. Beyond that bit o’ trivia, you could replace Lines 8 through 14 in the code with the line

puts(sentence);

or even with this one:

printf("%s\n",sentence);

When the char array is used in a function, as shown in the preceding line, the square brackets aren’t necessary. If you include them, the compiler believes that you screwed up.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com