Assigning Values to Arrays

You can initialize arrays (assign values to them) in one of two ways. First, you can do it one element at a time. Second, you can assign all of the values when declaring the array (just as you can assign values to any variable when you declare it). Let's go over both approaches in more detail.

Assigning values to one element

Assigning values to array elements is similar to assigning values to any other variable: you use the assignment operator. The one distinct difference is that you must indicate the index to which the assigned value should apply. But first, a quick word about indexes in C.

Strange as it may seem, arrays in C are indexed beginning at 0. So the first element in an array called answers is answers[0] ...

Get C Programming: Visual Quickstart Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.