How to Use C Arrays in C++

A C array is a data structure that stores many elements of the same data type. C arrays, when you use them in C++, are called "dumb" arrays. Arrays store default data types or user-defined ones in a contiguous area of memory. You can access an element via indexing, where the index is a positive integer that indicates the element's position, counting from zero.

Things You'll Need

  • Introductory C++
  • C++ compiler with an IDE
Show More

Instructions

    • 1

      Use the following convention to declare an array. Write the data type, use a name to designate the array and indicate the number of the elements inside square brackets. End the line with a semicolon.

      int arr1[5];

      int arr2[5] = {0}; // arr2 has all zeros.

    • 2

      Populate the array with data or initialize the array. Use indexing to assign elements to the various locations inside the array. Access the first location using an index of zero. Access the last location using an index of n minus one, where n is the length of the array:

      arr1[0] = 4; // puts 4 in the first location

      arr1[2] = 32; // puts 32 in the middle location

      arr1[4] = 17; // puts 17 in the last location

    • 3

      Retrieve an element from the array. This is the reverse of Step 3:

      int elem = arr1[4]; // Now elem has the value of 17.

    • 4

      Iterate across an array and print its elements on the console, also known as the standard output. Place the following sample inside the main() function and compile the program:

      for( int i = 0; i < 5; i++ ) {

      std::cout << "Elem " << i << ":" << arr1[i] << std::cout;

      }

    • 5

      Observe the output:

      Elem 1: 4

      Elem 2: 0

      Elem 3: 32

      Elem 4: 0

      Elem 5: 17

Tips & Warnings

  • This tutorial demonstrated one-dimensional arrays. You could also make arrays that have two or more dimensions.

  • In C, strings are implemented as arrays of type char.

  • C arrays have a fixed size and can't be resized.

  • Proper indexing is the programmer's responsibility. The value of the index must always be zero or positive and can never exceed the n minus 1 limit. When this rule is violated, compiler errors or run-time exceptions occur.

Related Searches:

Resources

Comments

You May Also Like

  • How to Create an Array in C

    Arrays offer the most efficient method for storing lists of data in C. They're very easy for the programmer to create and...

  • How to Use Arrays in Turbo C++

    In Turbo C++, arrays are used to hold collections of data that are unlikely to change in size very often. They are...

  • How to Initialize an Empty Array in C

    In programming, coders use variables to store pieces of data to access and manipulate throughout the course of the program. While regular...

  • How to Add Elements to an Array in C-#

    C# is a programming language created and maintained by Microsoft. It is used to create computer programs for Microsoft's .NET runtime environment,...

  • How to Reverse Elements of an Array in C

    C is a programming language for writing computer programs of any level of complexity. An array is the data structure denoted in...

  • How to Use the Array Command in PHP

    The array command in PHP creates and returns an array with the listed parameters. Index values are optional and may be provided...

  • How to Reverse an Array

    Virtually all programming languages use an array structure to store a list of data. For example, "array(76, 3, 5)." The reversed array...

  • How to Store a Sentence in an Array in C++

    Learning how to manipulate character arrays is a fundamental step in learning how to program in a language. A character array is...

  • How to Create an Array in PHP

    Arrays are data structures, and they are frequently used in all programming languages, including PHP. In PHP, an array is actually a...

  • About Arrays

    One of the fundamental constructs of computer science and mathematics is the array. It simply represents a collection of elements. A list...

  • How to Use Arrays in Microsoft Excel

    The array is a familiar construct to all computer programmers, but to the average Excel user, this tool often remains unknown and...

  • How to Use the Memset Function in C++

    Look at the following complete program for some simple examples of how to use memset. Note the use of memset to initialize...

  • How to Use Turbo C

    Turbo C is the Integrated Development Environment (IDE) for the C programming language created and sold by the Borland Corporation. Originally founded...

  • How to Use the Map STL Container Class

    The C++ map container class implements one-to-one mapping between a unique key and a value. The key sorts the key value pairs...

  • How to Use Pointers in C++

    Often, programmers prefer to deal with chunks of data through their memory location instead of directly. C++ pointers are ideal tools for...

  • How to Use an Array in Mathematics to Find Factors of a Number

    An array shows multiplication tables using objects. This is an easier approach for younger elementary students to visualize, rather than memorize, multiplication...

  • How to Calculate SHA1 for a String in C#

    The cryptographic hash function, SHA1, which stands for Secure Hash Algorithm, was designed by the National Security Agency. A hash function is...

Related Ads

Featured