Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Arrays

C Arrays

C language supportarraysdata structure, which can store a sequential collection of elements of a fixed size and the same type. An array is used to store a series of data, but it is often thought of as a sequence of variables of the same type.

Declaring an array does not declare individual variables one by one, such as runoob0, runoob1, ..., runoob99; instead, it declares a single array variable, such as runoob, and then uses runoob[0], runoob[1], ..., runoob[99] to represent the individual variables.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element, and the highest address corresponds to the last element.

Arrays in C

A specific element in an array can be accessed by index, with the first index having a value of 0.

The C language also allows us to use pointers to work with arrays, making array operations more flexible and efficient.

Declaring Arrays

To declare an array in C, you need to specify the type of the elements and the number of elements, as shown below:



type arrayName [ arraySize ];

This is called a one-dimensional array.arraySize must be an integer constant greater than zero,type It can be any valid C data type. For example, to declare an array of 10 elements of type double balance, the declaration statement is as follows:



double balance[10];

Now balance is a usable array that can hold 10 numbers of type double.

Initializing Arrays

In C, you can initialize an array element by element, or use a single initialization statement, as shown below:



double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};

The number of values between the braces { } cannot be greater than the number of elements we specified in the square brackets [ ] during the array declaration.

If you omit the size of the array, the size of the array becomes the number of elements at initialization. Therefore, if:



double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};

you will create an array that is exactly the same as the one created in the previous example. The following is an example of assigning a value to an element in an array:



balance[4] = 50.0;

The above statement assigns the value 50.0 to the fifth element of the array. All arrays use 0 as the index of their first element, also known as the base index; the last index of the array is the total size of the array minus 1. The following is a graphical representation of the array discussed above:

Array Representation

The figure below shows an array of length 10 , in which the first element has an index value of 0, and the ninth element runoob has an index value of 8:

Accessing Array Elements

Array elements can be accessed by the array name plus an index. The element's index is placed in square brackets, following the array name. For example:



double salary = balance[9];

The statement above will assign the value of the 10th element in the array to the salary variable. The following example uses the three concepts above: declaring an array, assigning to an array, and accessing an array:

Example

#include

<

stdio.h

int

main

(

)

{

int

n

[

10

]

;

/*

n is an array containing 10 integers

*/

int

i

,

j

;

/*

Initialize array elements

*/

for

(

i

=

0

;

i

<

10

;

i

++

)

{

n

[

i

]

=

i

100

;

/*

Set element i to i + 100

*/

}

/*

Print the value of each element in the array

*/

for

(

j

=

0

;

j

<

10

;

j

++

)

{

printf

(

"

Element[%d] = %d

\

n

"

,

j

,

n

[

j

]

)

;

}

return

0

;

}

When the code above is compiled and executed, it produces the following result:



Element[0] = 100

Element[1] = 101

Element[2] = 102

Element[3] = 103

Element[4] = 104

Element[5] = 105

Element[6] = 106

Element[7] = 107

Element[8] = 108

Element[9] = 109

Get the array length

The array length can be obtained using sizeof the operator to get the length of the array, for example:

int numbers[] = {1, 2, 3, 4, 5};

int length = sizeof(numbers) / sizeof(numbers[0]);

Example

#include

int

main

(

)

{

   

int

array

[

]

=

{

1

,

2

,

3

,

4

,

5

}

;

   

int

length

=

sizeof

(

array

)

/

sizeof

(

array

[

0

]

)

;

   

printf

(

"Array length is: %d\n"

, length

)

;

   

return

0

;

}

Use a macro definition:

Example

#include

#define LENGTH(array) (sizeof(array) / sizeof(array[0]))

int

main

(

)

{

   

int

array

[

]

=

{

1

,

2

,

3

,

4

,

5

}

;

   

int

length

=

LENGTH

(

array

)

;

   

printf

(

"Array length is: %d\n"

, length

)

;

   

return

0

;

}

The output of the above example is:

数组长度为: 5

Array name

In C, the array name represents the address of the array, that is, the address of the first element. When we declare and define an array, the array name represents the address of the array.

For example, in the following code:



int myArray[5] = {10, 20, 30, 40, 50};

Here, myArray is the array name, representing an array of type int containing 5 elements. myArray also represents the address of the array, that is, the address of the first element.

The array name itself is a constant pointer, meaning its value cannot be changed; once determined, it cannot point elsewhere.

We can use the & operator to get the address of the array, as shown below:



int myArray[5] = {10, 20, 30, 40, 50};

int *ptr = &myArray[0]; // 或者直接写作 int *ptr = myArray;

In the example above, the ptr pointer variable is initialized to the address of myArray, that is, the address of the first element of the array.

It should be noted that although the array name represents the address of the array, in most cases the array name is automatically converted to a pointer to the first element. This means we can use the array name directly in pointer arithmetic, such as when passing arguments to functions or traversing arrays:

Example

void

printArray

(

int

arr

[

]

,

int

size

)

{

   

for

(

int

i

=

0

;

i

<

size

;

i

++

)

{

       

printf

(

"%d "

,

arr

[

i

]

)

;

// The array name arr is used as a pointer

   

}

}

int

main

(

)

{

   

int

myArray

[

5

]

=

{

10

,

20

,

30

,

40

,

50

}

;

    printArray

(

myArray

,

5

)

;

// Pass the array name to a function

   

return

0

;

}

In the code above, the printArray function takes an integer array and the array size as parameters. We pass the myArray array name to the function, and inside the function the arr array name can be used like a pointer.

Detailed Explanation of Arrays in C

In C, arrays are very important, and we need to learn more details about them. The following lists some important array-related concepts that C programmers must be clear about:

Concept Description
Multidimensional Arrays C supports multidimensional arrays. The simplest form of a multidimensional array is a two-dimensional array.
Passing Arrays to Functions You can pass a pointer to an array to a function by specifying the array name without an index.
Returning Arrays from Functions C allows functions to return arrays.
Pointers to Arrays You can generate a pointer to the first element in the array by specifying the array name without an index.
Static Arrays and Dynamic Arrays Static arrays allocate memory at compile time with a fixed size, while dynamic arrays allocate memory manually at runtime with a variable size.