C language: array

Array:
An array composed of several related data items of the same type, which are stored together in order; An array is actually a collection of ordered data of the same type.
Array name:
Use a unified name to identify this group of data. This name is the array name.
Array elements:
Each data item that constitutes an array is called an element of the array
Format:
Type array name [subscript] [subscript]; Where: type is the base type of array elements, that is, the type of each element.
Array classification:
Commonly used one-dimensional array, two-dimensional array
One dimensional array:
Format:
Type array name [constant expression]; int a[10];// All elements are integers
The elements are a[0], a[1], a[2],... a[9].
**Note: * * the arrangement order of one-dimensional array elements in memory is linear, that is, continuous storage.
quote:
C language stipulates that only array elements can be referenced one by one, not the whole array at a time.
Reference form:
Array name [subscript]
When the array is referenced, its subscript must fall within the range of 0 to n-1, otherwise it will cross the boundary. The system does not check the cross-border, which needs to be checked by the programmer. In fact, an array element is a variable name, which is used as a variable.
One dimensional array initialization:
When defining an array, you can initialize the array:
Can be fully initialized
It can also be partially initialized

When assigning values to all array elements, you can not specify the array length

When the array is described as a static storage type or an external storage type (defined outside all functions), the array elements will be automatically initialized to 0 at the program compilation stage without displaying the given initial value. Figure below
2D array:
Format:
Type array name [constant expression] [constant expression]; Figure below
Elements are stored in rows

**Reference: * * array name [subscript] [subscript], subscript can be integer or constant expression,
Each array element is also a variable, which can be used as a variable.
If initial values are assigned to all elements, the first dimension length can not be specified when defining data, but the second dimension length cannot be written.
One dimensional array name as function parameter:
Using a simple variable as a function parameter, you can pass a data from an actual parameter to a formal parameter. However, using the array name as the function parameter belongs to "address call", which can transfer the first address of the array from the calling function to the called function, and transfer multiple data from the called function to the calling function.
Delivery method: it is an address assignment call, which passes the first address of the real parameter group to the virtual parameter, which is also an array name.
Virtual parameter requirement: must be an array name
Argument requirement: can be an array name
Virtual real combination: the real parameter and the virtual parameter have the same first address. After the virtual parameter changes the content of the whole address, the content of the real parameter also changes.
Function transfer diagram:
However, from the perspective of value transfer, it is still passed in one direction. It can only be passed from the actual parameter to the virtual parameter, and the virtual parameter can not be passed to the actual parameter again.


Two dimensional array name as function parameter

#include <stdio.h>
 
#define ROW 2 / / the number of rows in the two-dimensional array
#define COL 2 / / the number of columns in a two-dimensional array
 
//4 versions of summation function
//Method 1: array form
int TwoDimArraySum1(int twoDimAr[][COL], int row, int col);
 
//Mode 2: pointer form. prArray is a pointer to an array containing COL ints
int TwoDimArraySum2(int (*prArray)[COL], int row, int col);
 
//Mode 3: pointer form. pr is a pointer to int
int TwoDimArraySum3(int *pr, int row, int col);
 
//Mode 4: variable length array (supported from C99)
int TwoDimArraySum4(int row, int col, int twoDimAr[row][col]);
 
int main(void)
{
    int twoDimArray[ROW][COL] = {{-2, 5}, {4, 9}};
    int result;
 
    //Mode 1
    result = TwoDimArraySum1(twoDimArray, ROW, COL);
    printf("Sum1 Function result:%d\n", result);
 
    //Mode 2
    result = TwoDimArraySum2(twoDimArray, ROW, COL);
    printf("Sum2 Function result:%d\n", result);
 
    //Mode 3
    result = TwoDimArraySum3(twoDimArray[0], ROW, COL);
    printf("Sum3 Function result:%d\n", result);
 
    //Mode 4
    result = TwoDimArraySum4(ROW, COL, twoDimArray);
    printf("Sum4 Function result:%d\n", result);
 
    return 0;
}
 
int TwoDimArraySum1(int twoDimAr[][COL], int row, int col)
{
    int i, j;
    int result = 0;
 
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            //The following two addressing methods are OK
            result += twoDimAr[i][j];
            // result += *(*(twoDimAr + i) + j);
        }
    }
    return result;
}
 
int TwoDimArraySum2(int (*prArray)[COL], int row, int col)
{
    int i, j;
    int result = 0;
 
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            //The following two addressing methods are OK
            result += prArray[i][j];
            // result += *(*(prArray + i) + j);
        }
    }
    return result;
}
 
int TwoDimArraySum3(int *pr, int row, int col)
{
    int i, j;
    int result = 0;
 
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            //The following two addressing methods are OK
            result += pr[i*col + j];
            // result += *(Pr + i*col + j);
        }
    }
    return result;
}
 
int TwoDimArraySum4(int row, int col, int twoDimAr[row][col])
{
   int i, j;
   int result = 0;
 
   for (int i = 0; i < row; i++)
   {
       for (int j = 0; j < col; j++)
       {
           //The following two addressing methods are OK
           result += twoDimAr[i][j];
           // result += *(*(prArray + i) + j);
       }
   }
   return result;
}

Definition of character array:
**Format: * * char array name [constant] [constant]; Character array initialization:



Reference to character array:
To reference an element in a character array is to get a character.
There is no string data type in C language. C language treats strings as character arrays.

Relationship between character array and string:

String input:



If scanf encounters a space here, it will be regarded as the end. Later data is lost

String output:
Here, the length of the string is 6, not 5, and there is a '\ 0', which is automatically added by the system at the end, because it is the sign of the end of the string.

String handler:
The following functions have standard library functions, which are all in < string h> Inside.



String connection function --------- strcat


String copy function ---------- strcpy()


String comparison function ------ strcmp
Note: the comparison here is the comparison of corresponding elements. If one party is greater than the other, the comparison is completed, not by length.

String length function ------ strlen
Unlike siaeof operator, it does not calculate '\ 0' and sixeof calculates' \ 0 '

String lowercase function ----- strlwr

String uppercase function ----- strupr

Keywords: C Back-end

Added by owaring on Wed, 26 Jan 2022 08:40:12 +0200