Detailed explanation of one-dimensional array, two-bit array and string array

Keywords: C string array

catalogue

Array description

Definition of one-dimensional array

Initialization of one-dimensional array

Definition of two-dimensional array

Reference to two-dimensional array elements

Multidimensional array

Character arrays and strings

Character array

character string

2D string array

Array description

  • Array is one of the construction types
  • An array is a collection of several variables with a certain order relationship. Each variable constituting the array is called the element of the array.
  • The data types of elements in the array are required to be the same. It is determined by the array name and subscript. The array can be one-dimensional or multi-dimensional.

Definition of one-dimensional array

  • The so-called one-dimensional array refers to an array with only one subscript, which is continuously stored in the memory of the computer.
  • In C language, the general form of the name of one-dimensional array is as follows:
    < storage type > < data type > < array name > [< expression >] for example: int a[6];
    • The array name represents the first address of memory. The address is always on, and sizeof (array name) is the memory space of the array.

Example:

#include<stdio.h>
int main (void)
{
        int a[10];
        int i ;
        for(i = 0;i<10;i++)
                printf("%p\n",&a[i]);
        return 0;
}

Operation results:

0x7ffd148f8a20
0x7ffd148f8a24
0x7ffd148f8a28
0x7ffd148f8a2c
0x7ffd148f8a30
0x7ffd148f8a34
0x7ffd148f8a38
0x7ffd148f8a3c
0x7ffd148f8a40
0x7ffd148f8a44
  •   Note: the program operation is the result obtained by running under linux system. The results obtained by everyone may be inconsistent.

    %p is the format output controller for printing address information

Initialization of one-dimensional array

  • Initialization method: assign initial values to array elements when defining an array
  •  int a[5] = {1,2,3,4,5};
  • explain
  •   The array is not initialized and its element value is a random number
  •   If no initial value is assigned to the static array, the system will automatically assign a value of 0
  •   Only some array elements are given initial values

For Article 2:

static int a[5];
Equivalent to:
a[0];a[1] = 0;a[2] = 0;a[3] = 0;a[4] = 0;

Example: the array only assigns initial values to some elements, and the remaining elements are replaced by 0

#include<stdio.h>
int main (void)
{
        int a[10] = {1,5,6,8};//Array initialization partial elements
        int i ;
        for(i = 0;i<10;i++)
                printf("%p,%d\n",&a[i],a[i]);//Print array values
        return 0;
}

Operation results:

0x7ffc991407e0,1
0x7ffc991407e4,5
0x7ffc991407e8,6
0x7ffc991407ec,8
0x7ffc991407f0,0
0x7ffc991407f4,0
0x7ffc991407f8,0
0x7ffc991407fc,0
0x7ffc99140800,0
0x7ffc99140804,0

Definition of two-dimensional array

  • Definition method: (when declaring, the number of columns cannot be omitted, and the number of rows can be omitted)
  • Data type array name [constant expression] [constant expression]

For example:

int a[3][3];
float b[5][3];
  • Storage order of array elements
  • Two dimensional arrays are stored in row order first. Because the memory is one-dimensional, two-dimensional arrays need to be stored in order.

For example: int a[2][3]

a[0][1]a[0][2]a[0][3]
a[1][1]a[1][2]a[1][3]

Store a[0][1] before a[0][2]

Example:

#include<stdio.h>
int main(void)
{
	int a[2][3];
	int i,j;
	for(i= 0;i<2;i++)
	{
	    for(j = 0;j<3;j++)
	        printf("%p ",&a[i][j]);//Print array address
	    printf("\n");   
	}
	/*Print number, address and array length*/
	printf("%p %ld\n",a,sizeof(a));
	/*Print the array address of the first line and the length of the data array*/
	printf("%p %ld\n",a[0],sizeof(a[0]));
	/*Print the array address of the second line and the length of the data array*/
	printf("%p %ld\n",a[1],sizeof(a[1]));
	return 0;
}

Operation results:

0x7fff263db870 0x7fff263db874 0x7fff263db878 
0x7fff263db87c 0x7fff263db880 0x7fff263db884 
0x7fff263db870 24
0x7fff263db870 12
0x7fff263db87c 12

Reference to two-dimensional array elements

  • Form: array name [subscript] [subscript]
  • Initialization of two-dimensional array elements
    • Branch initialization
    • Initialize in element order

Branch initialization
Example: int a [2] [3] = {1,2,3}, {4,5,6};

Initialize in element order
Example: int a[2][3]={1,2,3,4,5,6};

Multidimensional array

An array with two or more subscripts is called a multidimensional array.

Example: int a[2][3][4]// Is a three-dimensional array

Three dimensional arrays are rarely used in practical applications, so I won't introduce them here.

Character arrays and strings

Objectives:

  • Master the usage of characters and numbers
  • Master the usage of string

Character array

Character array is an array whose data type is character type, char c[10],ch[3][4];

Initialization of character array

  • Assign char ch[5] = {'B','o','y'} character by character;
  • Use the string constant char ch[6] = {"hello"};char ch[] = "hello";

Example:

#include<stdio.h>
int main()
{
    int i,n,n1;
    char ch[] = {'a','b','c'}; //character
    char ch1[6] = {'a','b','c'};//String, the number of empty bits is represented by '\ 0'
    
    n = sizeof(ch)/sizeof(char) //String length
    for(i = 0;i < n;i++)
    {
        putchar(ch[i]);//Cyclic printing character
    }
    putchar('\n');
    n1 = sizeof(ch)/sizeof(char)  //String length
    for(i = 0;i < n1;i++)
    {
        putchar(ch[i]);//Print string
    }
    putchar('\n');
    return 0;
}

Operation results:

abc
abc

character string

  • There is no string variable in C language. Use character array to process string

  • String end flag: '\ 0'.

For example, char str[] = "hello";

Indicates that the character length is 5 and ends with '\ 0'.

2D string array

Character array initialization

char fruit[][10] = {"apple","orange","pear","peach","banana"};
  • Indicates that the two-dimensional array has 5 rows and 10 columns.

example

#include<stdio.h>
int main(void)
{
    char fruit[][10] = {"apple","orange","pear","peach","banana"};
    int i,k;
    
    k = sizeof(fruit)/sizeof(fruit[0]);//Indicates how many lines there are
    
    for(i = 0;i < k;i++)
    {
        printf("%s\n",fruit[i]);
    }
    return 0;
}

Operation results:

apple
orange
pear
peach
banana

Program example: string character reverse order

#include<stdio.h>
#include<string.h>
#define N 20
int main(void)
{
	char arr[N] = {0};
	char temp;
	int i = 0,j,n,k;
	
	printf("Plaese input a string!\n");
	gets(arr);
	n = strlen(arr);
	j = n-1;
	while(i<j)
	{
		temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
		i++;
		j--;
	}		
	puts(arr);
	return 0;
}

Operation results:

Plaese input a string!
apple
elppa

Posted by kbc1 on Tue, 19 Oct 2021 13:18:35 -0700