C / C + + Programming Notes: array part! Understand the key knowledge of C language in three minutes

Time:2021-4-24

An array in C or C + + is a collection of items stored in a continuous memory location. You can use the index of the array to access elements randomly. They are used to store elements of similar types because all elements must have the same data type. They can be used to store a collection of original data types, such as any specific type of int, float, double, char, etc.

C / C + + Programming Notes: array part! Understand the key knowledge of C language in three minutes

 

In addition, using C or C + + array can store derived data types, such as structure, pointer, etc. The following is a picturesque representation of an array.

C / C + + Programming Notes: array part! Understand the key knowledge of C language in three minutes

 

Why do we need arrays?

Ordinary variables (V1, V2, V3,…) can be used when there are few objects, but it is difficult to manage them with ordinary variables if a large number of instances are to be stored. The idea of arrays is to represent many instances in one variable.

Array declaration in C / C + +

C / C + + Programming Notes: array part! Understand the key knowledge of C language in three minutes

 

be careful:In the figure above, int a [3] = {[0… 1] = 3};   Since GCC 2.5, this statement has been obsolete

We can declare arrays in many ways. It can be initialized by specifying its type and size, or both.

Declare an array by specifying the size

int arr1[10];

int n = 10;

int arr2[n];

Array declaration by initializing elements

int arr[] = { 10, 20, 30, 40 }

Array declaration by specifying size and initialization elements

int arr[6] = { 10, 20, 30, 40 }

 

The advantages of array in C / C + +

Use the array index to access elements randomly.

Use fewer lines of code because it creates a single array of multiple elements.

Easy access to all elements.

With a single loop, it’s easy to traverse the array.

Sorting becomes easy because it can be done by writing fewer lines of code.

Disadvantages of array in C / C + +

Allows you to enter a fixed number of elements determined at the time of declaration. Unlike linked lists, arrays in C are not dynamic.

The insertion and deletion of elements can be expensive because they need to be managed according to the new memory allocation.

Facts about arrays in C / C + +

To access array elements:

Use integer indexAccessing array elements. The array index starts at 0 and continues until the array size minus 1.

C / C + + Programming Notes: array part! Understand the key knowledge of C language in three minutes

 

 

Example:

C language:

C / C + + Programming Notes: array part! Understand the key knowledge of C language in three minutes

 

C++:

C / C + + Programming Notes: array part! Understand the key knowledge of C language in three minutes

 

There is no index

Out of bounds check: for example, if there is no index out of bounds check in C / C + +, the following program can compile normally, but it may produce unexpected output at runtime.

C language:

C / C + + Programming Notes: array part! Understand the key knowledge of C language in three minutes

 

C++:

C / C + + Programming Notes: array part! Understand the key knowledge of C language in three minutes

 

Elements are stored in contiguous memory locations

C language:

C / C + + Programming Notes: array part! Understand the key knowledge of C language in three minutes

 

C++:

C / C + + Programming Notes: array part! Understand the key knowledge of C language in three minutes

 

Another method of traversing array

C ++:

C / C + + Programming Notes: array part! Understand the key knowledge of C language in three minutes

 

Arrays and pointers

Arrays and pointers are two different things (we can check by applying sizeof). The confusion occurs because the array name indicates the address of the first element, and the array is always passed as a pointer (even if we use square brackets).

What are vectors in C + +?

The vector in C + + is a class representing array in STL. The advantage of vectors over ordinary arrays is that,

When we declare a vector, we don’t need to pass the size as an extra parameter, that is, the vector support dynamic size (we don’t have to specify the size of the vector at the beginning). We can also adjust the size of the vector.

Vectors have many built-in functions, such as deleting elements.

In addition, if you want to better improve your programming ability, learn C language c + + programming! Overtaking on the curve, one step faster! I may be able to help you here~

C language c + + programming learning circle,QQ group 1090842465Click to enterWeChat official account: C language programming learning base

Share (source code, project video, project notes, basic introductory course)

Welcome to change and learn programming partners, use more information to learn and grow faster than their own thinking Oh!

Programming learning book sharing:

Programming learning video sharing:

Recommended Today

Notes on basic learning of ruby metaprogramming

Note 1:The code contains variables, classes and methods, which are collectively referred to as language construct. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 # test.rb class Greeting  def initialize(text)   @text = text  end    def welcome   @text  end end my_obj = Greeting.new(“hello”) […]