How should I play with ``arrays'', one-dimensional arrays, two-dimensional arrays, character arrays, string processing functions

One-dimensional array

Two-dimensional array

Character array

Functions for processing strings

1. One-dimensional array:

Type symbol array name [constant expression];

*** The array name can represent the address of the array or the address of the first element of the array. The two are the same in value, but have different meanings.

int arry[10];

The number of elements in this array:
int n=sizeof(arry)/sizeof(arry[0]);//Number of elements = total byte size of the array/byte size occupied by the first element
means that this is an integer (int) array with 10 integer elements, respectively Is a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9] . (" A[10] does not exist ");

Use a for function to print out the following table corresponding to 10 elements;
eg:

#include<stdio.h>
void main()
{
    
    
	int i, a[10] = {
    
    1,2,3,4,5,6,7,8,9,10 };
	for (i = 0; i<10; i++)
	{
    
    
		printf("第%d个元素即a[%d]对应的值为:>", i+1,i);
		printf("%d\n", a[i]);
	}
	system("pause");
}

The print result is:

1个元素即a[0]对应的值为:>1
2个元素即a[1]对应的值为:>2
3个元素即a[2]对应的值为:>3
4个元素即a[3]对应的值为:>4
5个元素即a[4]对应的值为:>5
6个元素即a[5]对应的值为:>6
7个元素即a[6]对应的值为:>7
8个元素即a[7]对应的值为:>8
9个元素即a[8]对应的值为:>9
10个元素即a[9]对应的值为:>10
请按任意键继续. . .

The constant expression in "[]" can only be a constant or a constant expression (its value is fixed).
eg: int a[2+3]; is possible, because its value is fixed as a constant.
eg: int a[n]; is illegal, that is, the c language does not allow dynamic adjustment of the array size.

Note: The array name represents the entire array only after sizeof and &, and the others represent the address of the first element ***

int arr1[10] = {
    
    1,2,3};//10个元素,其余未定义为0
int arr2[] = {
    
    1,2,3,4};//四个元素
int arr3[5] = {
    
    12345}
char arr4[3] = {
    
    'a',98, 'c'};
char arr5[] = {
    
    'a','b','c'};
char arr6[] = "abcdef";
这些数组都是正确的

Two, two-dimensional array:

Type specifier array name [constant expression 1] [constant expression 2]

(Constant expression 1: There are several lines) (Constant expression 2: There are several elements in each line)
eg: i

int ewarry[3][4]={
    
    {
    
    12}{
    
    5,67}{
    
    }}
注:未赋值的为0
既:
1  2  0  0
5  6  7  0
0  0  0  0

正确的表达式:
int arr[3][5] = {
    
     1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };//系统自行填充0
int arr[][5]={
    
    1,2,3,4,6,4,7,8,5};//常量表达式1可以不写,由系统自行填充0,常量表达式2不能少

Three, character array:

Definition: The array used to store the character data is when scanf is used for the character array
. scanf("%d",srry); No &, arry has the first address for the array name
eg:

char c[10]={
    
    'i','s','e','e','y','o','u'}
注:\0既是字符串结束标志,系统自行填充\0

Fourth, the function of processing strings:

1. puts

puts(字符数组)
作用:将一个字符串(以\0结束)输出到终端。

2.gets

gets(字符数组)
作用:从终端输入一个字符串到字符数组,并且得到一个函数值。该函数值是字符串的起始地址。

3.strcat

strcat(字符数组1,字符数组2
作用:将两个字符数组中的字符串连接起来,吧第二个字符串接到第一个字符串后面,结果放在字符数组1中,函数调用得到一个函数值——字符数组1的地址。
char arry1[50]={
    
    "abcde "};
char arry2[]={
    
    doushini"};
printf("%d",strcat(arry1,arry2));
输出:>
abcd doushini

4.strcpy和strncpy

strcpy(字符数组1,字符数组2)
作用:将字符串2复制到字符串1中(包含2中末尾的\0,但1后面任为原来字符元素)。加n表示将数组2中前面几个字符复制过来。
char str1[10],str2[]="abcd";
strcpy(str1,str2);

5.strcmp

strcmp(字符串1,字符串2
作用:比较字符串1和字符串2.
strcmp("str1,str2");
strcmp("abcde","nishishei");
strcmp(str1,"abcde");
比较结果由函数值带回。

(1) If the strings are equal, the function value is 0;
(2) If the front string> the back string, the function value is a positive integer;
(3) If the front string <the back string, the function value is A negative integer;

注:两字符串比较不能用:
if(str1>str2) 
printf("yes");
只能用:
if(strcmp(str1,str2)>0)
printf("yes");

6.strlen

strlen(字符数组)
作用:测试字符串长度的函数,函数的值为字符串中的实际长度(不包括'\0')。
char str[10]="abcede";
printf("%d",strlen(str));
输出结果为:>5.
也可以直接粗粮长度,如:
strlen("abcde");

7.stress

strlwr(字符串)
作用:将字符串中大写字母换成小写字母。

8.strupr

strupr(字符串)
作用:将字符串中小写字母换成大写字母。

Guess you like

Origin blog.csdn.net/weixin_52270223/article/details/109658353