C language learned by suffering
C language learned by suffering
How to use arrays
Assignment of initial values
Arrays, like all variables up to this point, can be initialized at the same time they are declared.
Arrays are initialized as follows
The numbers are placed in order, separated by , inside the {}.
The number of numbers should be less than or equal to the number of elements in the array, since it is not necessary to specify all of them.
If there are fewer than the number of elements in the array, all the rest will be assigned 0.
The following program is an example of initializing and displaying an array.
The result of executing this program will be as follows
When declared this way, the number of elements can be omitted.
In that case, the number of elements is allocated as many times as the number specified.
The following program is an example of declaring an array without the number of elements.
This saves you the trouble of specifying the number of elements and also eliminates miscounting.
It is better to omit it if an initial value can be assigned.
Assigning values to an array at one time can only be done at the time of declaration.
For example, the following assignments are not allowed
These writings will result in a compile error.
If you want to assign values after the declaration, assign them one by one, even if it is tedious.
Arrays are initialized as follows
Array initialization
Type name Array name[number of elements]={0th number,1st number,2nd number,...};
The numbers are placed in order, separated by , inside the {}.
The number of numbers should be less than or equal to the number of elements in the array, since it is not necessary to specify all of them.
If there are fewer than the number of elements in the array, all the rest will be assigned 0.
The following program is an example of initializing and displaying an array.
Source Code
#include <stdio.h>
int main(void)
{
int array[10] = { 42, 79, 13 }
printf("array[0] = %d\n", array[0]);
printf("array[1] = %d\n", array[1]);
printf("array[2] = %d\n", array[2]);
printf("array[3] = %d\n", array[3]);
printf("array[4] = %d\n", array[4]);
return 0;
}
The result of executing this program will be as follows
Execution Result
array[0] = 42
array[1] = 79
array[2] = 13
array [3] = 0
array [4] = 0
array[1] = 79
array[2] = 13
array [3] = 0
array [4] = 0
When declared this way, the number of elements can be omitted.
In that case, the number of elements is allocated as many times as the number specified.
The following program is an example of declaring an array without the number of elements.
Source code
#include <stdio.h>
int main(void)
{
int array[] = { 42, 79, 13 }; /* number of elements omitted */
printf("array[0] = %d\n", array[0]);
printf("array[1] = %d\n", array[1]);
printf("array[2] = %d\n", array[2]);
return 0;
}
This saves you the trouble of specifying the number of elements and also eliminates miscounting.
It is better to omit it if an initial value can be assigned.
Assigning values to an array at one time can only be done at the time of declaration.
For example, the following assignments are not allowed
Assignments that cannot be made
array = { 42, 79, 13 };
array[10] = { 42, 79, 13 }
These writings will result in a compile error.
If you want to assign values after the declaration, assign them one by one, even if it is tedious.
Assignments that can be made
array[0] = 42;
array[1] = 79;
array[2] = 13;
Display of all elements
A loop of for statements can be used to display all the elements of an array.
Such usage is the greatest advantage of arrays.
The following program is an example of displaying the full number of elements in an array.
The result of executing this program is as follows
The point of this program is that variables can also be used for array element numbers.
Varying the value of the variable allows direct access to the various numbered elements.
With a normal variable that is not an array, it would take five printf functions to display five elements, and with a variable that is not an array, it would take five printf functions to display five elements, and
To display 100 pieces, you need 100 pieces, but with arrays, all you have to do is change the number in the for statement.
With this, it is easy to write a program to display data for 10,000 people.
You can also change the statements in the loop to assign the same value to all elements or to
It is also easy to find the average of the values assigned to all elements.
Such usage is the greatest advantage of arrays.
The following program is an example of displaying the full number of elements in an array.
source code
#include <stdio.h>
int main(void)
{
int array[] = { 42, 79, 13, 75, 19 };
int i;
for (i = 0; i < 5; i++) {
printf("array[%d] = %d\n", i, array[i]);
}
return 0;
}
The result of executing this program is as follows
Execution Result
array[0] = 42
array[1] = 79
array[2] = 13
array [3] = 75
array [4] = 19
array[1] = 79
array[2] = 13
array [3] = 75
array [4] = 19
The point of this program is that variables can also be used for array element numbers.
Varying the value of the variable allows direct access to the various numbered elements.
With a normal variable that is not an array, it would take five printf functions to display five elements, and with a variable that is not an array, it would take five printf functions to display five elements, and
To display 100 pieces, you need 100 pieces, but with arrays, all you have to do is change the number in the for statement.
With this, it is easy to write a program to display data for 10,000 people.
You can also change the statements in the loop to assign the same value to all elements or to
It is also easy to find the average of the values assigned to all elements.
Find the number of elements
In the previous section, we used a for statement loop to display all the elements of an array.
The number of iterations, 5, was specified directly, but this requires counting the number of elements.
With this method, the for statement must be rewritten each time the number of elements increases.
That would be tedious, so we decided to have it automatically find the number of elements and repeat the process.
There is no direct method provided for determining the number of elements, but it can be calculated.
The C language has a sizeof (size-of) operator that finds the size of a variable or array.
The sizeof operator is used as follows.
The sizeof operator does not have to be followed by a (), but it is usually followed by a () because it is more readable.
To calculate the number of elements in an array array using this operator, do the following
array[0] because there will always be an element of array number 0, even if the length of the array is 1.
If you set array[1], you will get an error when the length of the array is 1.
The following program is an example of rewriting the previous section in this manner.
The execution result will be the same as the previous one.
If you change the number of elements in the array, it automatically displays that number.
The number of iterations, 5, was specified directly, but this requires counting the number of elements.
With this method, the for statement must be rewritten each time the number of elements increases.
That would be tedious, so we decided to have it automatically find the number of elements and repeat the process.
There is no direct method provided for determining the number of elements, but it can be calculated.
Find the size of the entire array and divide it by the size of a single element to find the number of elements.
The C language has a sizeof (size-of) operator that finds the size of a variable or array.
The sizeof operator is used as follows.
sizeof operator
sizeof(variable or array name)
The sizeof operator does not have to be followed by a (), but it is usually followed by a () because it is more readable.
To calculate the number of elements in an array array using this operator, do the following
Find the number of elements in array array
sizeof(array) / sizeof(array[0])
array[0] because there will always be an element of array number 0, even if the length of the array is 1.
If you set array[1], you will get an error when the length of the array is 1.
The following program is an example of rewriting the previous section in this manner.
Source Code
#include <stdio.h>
int main(void)
{
int array[] = { 42, 79, 13, 75, 19 };
int i;
for (i = 0; i < sizeof(array) / sizeof(array[0]); i++) {
printf("array[%d] = %d\n", i, array[i]);
}
return 0;
}
The execution result will be the same as the previous one.
If you change the number of elements in the array, it automatically displays that number.
Storing the number of elements in a variable to eliminate waste
Actually, the above program is wasteful.
This is because <0>it calculates "sizeof(array) / sizeof(array <0>)" for each loop. <0>
int array_count = sizeof(array) / sizeof(array <0>);
for (i = 0;i < array_count;i++) {
as element By storing the number in a variable, the waste of calculating each time can be eliminated.
Well, nowadays computers are so powerful that it doesn't make much difference...
This is because <0>it calculates "sizeof(array) / sizeof(array <0>)" for each loop. <0>
int array_count = sizeof(array) / sizeof(array <0>);
for (i = 0;i < array_count;i++) {
as element By storing the number in a variable, the waste of calculating each time can be eliminated.
Well, nowadays computers are so powerful that it doesn't make much difference...
Array Copy
To assign the values of all elements of one array to another array, use the for statement.
The result of executing this program is as follows
The result shows that the value of array1 is copied to array2.
However, you can use the memcpy function without using a for statement.
Note that you must #include the memory.h file to use the memcpy function.
The overall array size depends on the array type and number of elements.
In this function, the size is based on the size obtained by the sizeof operator.
To copy all elements of an array, specify the array name with the sizeof operator.
The following program is an example of copying an array with the memcpy function.
The result of the execution will be exactly the same as before.
source code
#include <stdio.h>
int main(void)
{
int array1[] = { 42, 79, 13, 19, 41 }
int array2[] = { 1, 2, 3, 4, 5 }
int i;
for (i = 0; i < sizeof(array2) / sizeof(array2[0]); i++) {
printf("array2[%d] = %d\n", i, array2[i]);
}
/* copy all elements of array1 to array2 */
for (i = 0; i < sizeof(array2) / sizeof(array2[0]); i++) {
array2[i] = array1[i];
}
for (i = 0; i < sizeof(array2) / sizeof(array2[0]); i++) {
printf("array2[%d] = %d\n", i, array2[i]);
}
return 0;
}
The result of executing this program is as follows
Execution Result
array2[0] = 1
array2[1] = 2
array2[2] = 3
array2[3] = 4
array2[4] = 5
array2[0] = 42
array2[1] = 79
array2[2] = 13
array2[3] = 19
array2[4] = 41
array2[1] = 2
array2[2] = 3
array2[3] = 4
array2[4] = 5
array2[0] = 42
array2[1] = 79
array2[2] = 13
array2[3] = 19
array2[4] = 41
The result shows that the value of array1 is copied to array2.
However, you can use the memcpy function without using a for statement.
Note that you must #include the memory.h file to use the memcpy function.
memcpy function
memcpy(destination array name, source array name, total array size)
The overall array size depends on the array type and number of elements.
In this function, the size is based on the size obtained by the sizeof operator.
To copy all elements of an array, specify the array name with the sizeof operator.
The following program is an example of copying an array with the memcpy function.
Source Code
#include <memory.h>
#include <stdio.h>
int main(void)
{
int array1[] = { 42, 79, 13, 19, 41 }
int array2[] = { 1, 2, 3, 4, 5 }
int i;
for (i = 0; i < sizeof(array2) / sizeof(array2[0]); i++) {
printf("array2[%d] = %d\n", i, array2[i]);
}
memcpy(array2, array1, sizeof(array1)); /* copy all elements of array1 to array2 */
for (i = 0; i < sizeof(array2) / sizeof(array2[0]); i++) {
printf("array2[%d] = %d\n", i, array2[i]);
}
return 0;
}
The result of the execution will be exactly the same as before.
buffer overrun
In the above, the source array (array1) and the destination array (array2) are the same length.
Therefore, there is no overflow and they are copied exactly.
However, the trouble is that the memcpy function does not take into account the length of each array.
Even if the source array is longer than the destination array, it will be copied in its entirety.
As a result, the long part will be copied overflowing.
As a result, other unrelated variables and arrays are overwritten, causing serious calculation errors.
This is the infamous "buffer overrun" bug.
This buffer overrun is the cause of many of the sudden stoppages of applications and computers.
Well, technically, it is a buffer overflow if it is just copied overflowing...
C is a very bug-prone language.
If you make a slight programming mistake, the application will stop immediately.
Newer programming languages such as Java, C#, Rust, etc., are much more bug-prone.
Buffer overruns are designed to occur as little as possible.
Therefore, there is no overflow and they are copied exactly.
However, the trouble is that the memcpy function does not take into account the length of each array.
Even if the source array is longer than the destination array, it will be copied in its entirety.
As a result, the long part will be copied overflowing.
As a result, other unrelated variables and arrays are overwritten, causing serious calculation errors.
This is the infamous "buffer overrun" bug.
This buffer overrun is the cause of many of the sudden stoppages of applications and computers.
Well, technically, it is a buffer overflow if it is just copied overflowing...
C is a very bug-prone language.
If you make a slight programming mistake, the application will stop immediately.
Newer programming languages such as Java, C#, Rust, etc., are much more bug-prone.
Buffer overruns are designed to occur as little as possible.
About this Site
The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or more complete than any book on the market.