Computer Science

Array C

Array C is a data structure in the C programming language that stores a collection of elements of the same data type. It is a contiguous block of memory that can be accessed using an index. Arrays in C are static, meaning their size is fixed at compile time.

Written by Perlego with AI-assistance

5 Key excerpts on "Array C"

  • C Programming
    eBook - ePub

    C Programming

    Learn to Code

    10 Arrays and Strings
    DOI: 10.1201/9781003188254-10

    10.1 Introduction

    Many times, we come across a situation where we use a set of data rather than a single datum. For example, assume that you are an instructor and you teach C programming. You want to store the marks of your students and later perform different types of operations on them, such as finding the top performer or knowing how many students secure less than 50 marks. In that case usage of a single variable is not enough: you need multiple variables to store the data of your students. Again, if you use many variables in your program then remembering those variable names will become difficult. So, the solution is the array – a concept provided by C that handles large numbers of items simultaneously.
    In our day-to-day life we also came across situations where we need to group items and keep them in a sequential manner for easy access. For example, Figure 10.1 shows a toy train built to store painting items such as watercolors, sketch pens, brushes, pencils, and oil pastels. We name this train the Painter’s Train. We number the boxes from 1 to 5 and store many items in them. The concept of arranging similar data and calling them using a common name is sometimes known as an array of items.
    Figure 10.1 Introducing the concept of an array.
    An array is a series of elements of the same type, placed in contiguous memory locations that can be individually referenced by adding an index number to each location. Other definitions are:
    • An array is a single programming variable with multiple "compartments." Each compartment can hold a value.
    • A collection of data items, all of the same type, in which the position of each item is uniquely designated by a discrete type.
    This chapter is dedicated to a discussion of arrays. After completion of this chapter, readers will have learnt the following:
  • Ivor Horton's Beginning Visual C++ 2008
    • Ivor Horton(Author)
    • 2011(Publication Date)
    • Wrox
      (Publisher)
    tax , and so on. Of course, you would also need some means of picking out a particular employee from the whole bunch, together with the data from the generic variables associated with them. This kind of requirement arises with any collection of like entities that you want to handle in your program, whether they're baseball players or battleships. Naturally, C++ provides you with a way to deal with this.
    Arrays
    The basis for the solution to all of these problems is provided by the array in ISO/ANSI C++. An array is simply a number of memory locations called array elements or simply elements , each of which can store an item of data of the same given data type and which are all referenced through the same variable name. The employee names in a payroll program could be stored in one array, the pay for each employee in another, and the tax due for each employee could be stored in a third array.
    Individual items in an array are specified by an index value which is simply an integer representing the sequence number of the elements in the array, the first having the sequence number 0 , the second 1 , and so on. You can also envisage the index value of an array element as being an offset from the first element in an array. The first element has an offset of 0 and therefore an index of 0 , and an index value of 3 will refer to the fourth element of an array. For the payroll, you could arrange the arrays so that if an employee's name was stored in the employeeName array at a given index value, then the arrays pay and tax would store the associated data on pay and tax for the same employee in the array positions referenced by the same index value.
    The basic structure of an array is illustrated in Figure 4-1 .
    Figure 4-1
    Figure 4-1 shows an array. The name height has six elements, each storing a different value. These might be the heights of the members of a family, for instance, recorded to the nearest inch. Because there are six elements, the index values run from 0 through 5 . To refer to a particular element, you write the array name, followed by the index value of the particular element between square brackets. The third element is referred to as height[2]
  • Learn C Programming
    Chapter 11 : Working with Arrays
    We have already seen how a structure is a grouping of one or more components that can each be of different data types. Often, we need a grouping that consists of the same type; this is called an array . An array is a collection of multiple occurrences of the same data type grouped together under a single name. Each element of the array is accessed via its base name and an offset of that base. Arrays have many uses, from organizing homogenous data types to providing the basis for strings, or arrays of characters.
    Before we can learn about some of the wide uses of arrays, we need to explore the basics of declaring and manipulating arrays. The following topics will be covered in this chapter:
    • Declaring an array of values
    • Initializing an array in several ways
    • Understanding variable-length arrays
    • Accessing each element of an array
    • Understanding zero-based array indexing
    • Assigning and manipulating elements of an array
    • Using looping statements to access all elements of an array
    • Using array references as function parameters

    Technical requirements

    Continue to use the tools chosen from the Technical requirements section of Chapter 1 , Running Hello, World! .
    The source code for this chapter can be found at https://github.com/PacktPublishing/Learn-C-Programming-Second-Edition/tree/main/Chapter11 .

    Declaring and initializing arrays

    An array is a collection of two or more values, all of which have the same type and share a single common base name. It makes no sense to have an array of just one value; that would simply be a variable. An array definition has the following syntax: dataType arrayIdentifier[ numberOfElements ]; Here, dataType is any intrinsic or custom type, arrayIdentifier is the base name of the array, and numberOfElements specifies how many values of dataType are in the array. numberOfElements , for whatever type and values are given, is a literal constant or expression that will be converted to an integer. Most commonly, numberOfElements is an integer constant expression. An array whose size is defined by a constant expression we call a constant-length array
  • Ivor Horton's Beginning Visual C++ 2012
    • Ivor Horton(Author)
    • 2012(Publication Date)
    • Wrox
      (Publisher)
    tax , and so on. Of course, you would also need some means of picking out a particular employee from the whole bunch, together with the data from the generic variables associated with them. This kind of requirement arises with any collection of like entities that you want to handle in your program, whether they’re baseball players or battleships. Naturally, C++ provides you with a way to deal with this.

    Arrays

    The basis for the solution to all of these problems is provided by the array. An array is simply a number of memory locations called array elements or simply elements, each of which can store an item of data of the same given data type, and which are all referenced through the same variable name. The employee names in a payroll program could be stored in one array, the pay for each employee in another, and the tax due for each employee could be stored in a third array.
    You select a particular element in an array using an index value, which is simply an integer representing the sequence number of the element in the array. The first element has the sequence number 0 , the second 1 , and so on. You can also envisage the index value of an array element as being an offset from the first element in an array. The first element has an offset of 0 and therefore an index of 0 , and an index value of 3 will refer to the fourth element of an array.
    The basic structure of an array is illustrated in Figure 4-1 .
    FIGURE 4-1
    Figure 4-1 shows an array with the name height that has six elements, each storing a different value. These might be the heights of the members of a family, for instance, recorded to the nearest inch. Because there are six elements, the index values run from 0 through 5 . To refer to a particular element, you write the array name, followed by the index value of the particular element between square brackets. The third element is referred to as height[2]
  • Microcontroller Programming
    eBook - ePub
    • Syed R. Rizvi(Author)
    • 2016(Publication Date)
    • CRC Press
      (Publisher)
    10.  An operator is a symbol which helps the user to command the computer to do a certain mathematical or logical manipulations. C has many operators which can be classified as: mathematical operators, relational operators, logical operators, and assignment operators.
    11.  C uses the term functions for subroutines. Judicious use of subroutines often significantly reduces the cost of developing and maintaining a large program. It also increases its quality and reliability.
    12.  Subroutines are an important mechanism for sharing and trading software. 13.  The variable that stores the reference to another variable is called a pointer. 14.  Pointers are a very powerful feature of the C language that has many uses in advanced programming. 15.  The C language provides a capability that enables the user to define a set of ordered data items known as an array.

    Glossary

    Arithmetic: Traditional operations such as addition, subtraction, multiplication, and division.
    Array: A variable that holds multiple elements which has the same data type.
    Assembler: A software package that is used to convert assembly language into machine language.
    Assembly Language:
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.