C language: intensive review of the final array

Regarding the resolution of the array description and its initial value:
Example: (1)
int a[ ][4]={0,0};
Example: (2)
int a[3][4]={0};

Similar to the description of the two, each element of the array a can get the initial value 0. Derived from
this:
Example (three): int a[ ][3]={1,2,3,4,5,6,7};
Its array a contains 9 elements, and the first dimension is 3 .
in this regard the int a [] [3] = {1,2,3,4,5,6,7}; make a comparison - the first dimension of the two-dimensional array of size 3.

It is worth noting that:
int a[ ][4]={0,0}; The size of the first dimension of this two-dimensional array a is 1.

Edge knowledge (just understand):
If the two-dimensional array a has m columns, the formula for calculating the position of any element a[i][j] in the array is i*m+j+1.

Personally, character arrays are more difficult than numeric arrays.
The first is to figure out strings and characters.

Initialize two character arrays:

char a[ ]="ABCDEF";

char b[ ]={
    
    'A', 'B', 'C', 'D', 'E', 'F'};

The length of a is longer than that of b. Strings and characters.

scanf("%s%s", a, b);
is the correct input format of character array a, b (mentioned in my previous blog). This does not require the command character &.

The correct statement about the character array:
any character in the ASCII character set can be stored in the
character array. The character string of the character array can be input and output as a whole. It is
not possible to compare the character strings in the character array with relational operators.

Character array output will automatically wrap and output when encountering'\0'.

Error-prone hints
Relational operators can be used to compare single characters;
but for strings, this requires the #include<string.h> header file before calling
functions such as strcat, strcmp, strcpy, strlen, etc., to apply between strings.

Detailed error prompt (a common error for most beginners)
char s[5]="abcde";
This type of initialization of the array s is wrong. As a beginner, I did a comparison and confirmed that
e was not removed :

#include<stdio.h>
int main()
{
    
    
	char s[5]="abcde";
	puts(s);
}

Running result:

after removing e:

#include<stdio.h>
int main()
{
    
    
	char s[5]="abcd";
	puts(s);
}

operation result:

The reason is that'\0' in the character array also occupies a storage unit. Failure to notice this can lead to garbled characters.

To determine whether the two strings are equal, the correct statement
if (!strcmp(a,b))
specifies that this statement is because if(strcmp(a,b)>0) is commonly used
if(strcmp(a,b) <0) if(strcmp(a,b)==0)
is a knowledge blind zone.

Then there will be interesting programming questions to share.

Guess you like

Origin blog.csdn.net/yooppa/article/details/112296620