overview
All members of the same array are of the same data type, and the addresses of all members in memory are continuous.
one-dimensional array
If the global array is not initialized, the compiler will initialize it to zero. If the local array is not initialized, its content is random.
int a[10] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };//定义一个数组,同时初始化所有成员变量
int a[10] = {
1, 2, 3 };//初始化前三个成员,后面所有元素都设置为0
int a[10] = {
0 };//所有的成员都设置为0
//[]中不定义元素个数,定义时必须初始化
int a[] = {
1, 2, 3, 4, 5 };//定义了一个数组,有5个成员
The array name is an address constant, representing the address of the first element in the array.
#include<stdio.h>
int main() {
int a[10] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
printf("数组名a所表示的地址常量:%p\n", a);
printf("数组首个元素a[0]的内存地址:%p\n", &a[0]);
printf("数组a在内存中占%d个字节\n", sizeof(a));
printf("数组a的元素在内存中占%d个字节\n", sizeof(a[0]));
printf("开始输出数组a的全部元素:\n");
for (int i = 0; i < sizeof(a)/sizeof(a[0]); i++) {
printf("a[%d]=%d\n", i, a[i]);
}
return 0;
}
Run the above code, the output is as follows:
Two-dimensional array
There is no two-dimensional array in the memory. The actual hardware memory of the two-dimensional array is addressed continuously. Storage is the same.
#include<stdio.h>
int main() {
//连续赋值
int a[3][4] = {
1, 2, 3, 4, 5};
//分段赋值
//int a[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("a[%d][%d]=%d\n", i, j, a[i][j]);
}
}
}
Execute the above code, the output is as follows:
The array name is a constant address, representing the address of the first element in the array.
#include<stdio.h>
int main() {
int a[3][4] = {
{
1, 2, 3, 4}, {
5, 6, 7, 8}, {
9, 10, 11, 12} };
printf("二维数组名a所代表的地址常量:%p\n", a);
//第0个一维数组的数组名为a[0]
printf("二维数组的首个元素a[0]所代表的地址常量:%p\n", a[0]);
printf("a[0][0]在内存中的地址为:%p\n", &a[0][0]);
printf("二维数组a在内存中占%d个字节\n", sizeof(a));
printf("二维数组中的一行在内存中占%d个字节\n", sizeof(a[0]));
printf("二维数组中的元素在内存中占%d个字节\n", sizeof(a[0][0]));
printf("二维数组a的行数:%d\n", sizeof(a)/sizeof(a[0]));
printf("二维数组a的列数:%d\n", sizeof(a[0])/sizeof(a[0][0]));
printf("二维数组a的行数*列数:%d\n", sizeof(a)/sizeof(a[0][0]));
//[]中不定义元素个数,定义时必须初始化
int b[][4] = {
1, 2, 3, 4, 5};
printf("二维数组b的行数:%d\n", sizeof(b)/sizeof(b[0]));
return 0;
}
Running the above code, the output is as follows:
Character arrays and strings
The difference between character arrays and strings
- There is no data type of string in C language, which can be replaced by an array of char;
- A string must be an array of char, but an array of char is not necessarily a string;
- A char array ending with the number 0 (equivalent to the character '\0') is a string , but if the char array does not end with the number 0, then it is not a string, just an array of ordinary characters, so the string is a special array of char ;
#include<stdio.h>
int main() {
//没有'\0'作为结束符,所以hello1不是字符串,只是普通的字符数组
char hello1[] = {
'h', 'e', 'l', 'l', 'o'};//末尾输出乱码
printf("hello1 = %s\n", hello1);
//hello2以'\0'作为结束符,所以hello2是字符串
char hello2[] = {
'h', 'e', 'l', 'l', 'o', '\0'};
printf("hello2 = %s\n", hello2);
//以第1个'\0'作为结束符,所以不会输出后面的world
char hello3[] = {
'h', 'e', 'l', 'l', 'o', '\0', 'w', 'o', 'r', 'l', 'd', '\0'};
printf("hello3 = %s\n", hello3);
return 0;
}
string initialization
#include<stdio.h>
int main() {
char buf[] = {
'I', ' ', 's', 'a', 'y', ':', '\0'};
//编译器在末尾自动补上\0,所以数组长度为13
char buf2[] = {
"Hello world!"};
printf("数组buf2的长度为:%u\n", sizeof(buf2) / sizeof(char));
//指定长度,后面没有赋值的元素,自动补0
char buf3[30] = {
"Welcome to"};
printf("数组buf3的长度为%u\n", sizeof(buf3) / sizeof(char));
//直接使用字符串初始化,编译器自动在后面补0,常用
char buf4[] = "our world";
printf("数组buf4的长度为%u\n", sizeof(buf4) / sizeof(char));
printf("%s%s\n", buf, buf2);
printf("C:%s %s!!\n", buf3, buf4);
//char buf5[2] = { '1', '2', '3' };//数组越界
char buf5[] = {
'a', 'b', 'c', 0, 'd'};
char buf6[] = {
'a', 'b', 'c', '0', 'd'};
char buf7[] = {
'a', 'b', 'c', '\0', 'd'};
printf("输出buf5:%s\n", buf5);
printf("输出buf6:%s\n", buf6);
printf("输出buf7:%s\n", buf7);
//'\0'后面最好不要连着数字,有可能几个数字连起来刚好是一个转义字符
//'\ddd'八进制字义字符,'\xhh'十六进制转移字符
// \012相当于\n
char str[] = "\012abc";
printf("str == %s\n", str);
return 0;
}
Running the above code, the output is as follows:
String input and output
gets()
#include <stdio.h>
char *gets(char *s);
功能:从标准输入读入字符,并保存到s指定的内存空间,直到出现换行符或读到文件结尾为止。
参数:
s:字符串首地址
返回值:
成功:读入的字符串
失败:NULL
gets(str)
The difference with scanf("%s",str)
:
gets(str)
The input string is allowed to contain spacesscanf("%s",str)
no spaces allowed
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main() {
char str[100];
printf("请输入一个字符串(gets):\n");
gets(str);
printf("您输入的是%s\n", str);
char str2[100];
printf("请输入一个字符串(scanf):\n");
scanf("%s", str2);
printf("您输入的是%s\n", str2);
return 0;
}
Running the above code, the output is as follows:
fgets()
#include <stdio.h>
char *fgets(char *s, int size, FILE *stream);
功能:从stream指定的文件内读入字符,保存到s所指定的内存空间,直到出现换行字符、读到文件结尾或是已读了size - 1个字符为止,最后会自动加上字符 '\0' 作为字符串结束。
参数:
s:字符串
size:指定最大读取字符串的长度(size - 1)
stream:文件指针,如果读键盘输入的字符串,固定写为stdin
返回值:
成功:成功读取的字符串
读到文件尾或出错: NULL
fgets()
When reading a character string entered by the user through the keyboard, the carriage return entered by the user is also regarded as part of the character string. When entering a string through scanf
and gets
, the ending "\n" is not included, but there are more "\n" at the end through fgets. The fgets() function is safe and there is no buffer overflow problem.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main() {
char str[5];
fgets(str, sizeof(str)/sizeof(char), stdin);
printf("您输入的是%s\n", str);
return 0;
}
To run the above code, execute as follows:
puts()
#include <stdio.h>
int puts(const char *s);
功能:标准设备输出s字符串,在输出完成后自动输出一个'\n'。
参数:
s:字符串首地址
返回值:
成功:非负数
失败:-1
fputs()
#include <stdio.h>
int fputs(const char * str, FILE * stream);
功能:将str所指定的字符串写入到stream指定的文件中, 字符串结束符 '\0' 不写入文件。
参数:
str:字符串
stream:文件指针,如果把字符串输出到屏幕,固定写为stdout
返回值:
成功:0
失败:-1
fputs()
Yes puts()
the file manipulation version, but fputs() doesn't automatically output a '\n'.
strlen()
#include <string.h>
size_t strlen(const char *s);
功能:计算指定指定字符串s的长度,不包含字符串结束符‘\0’
参数:
s:字符串首地址
返回值:字符串s的长度,size_t为unsigned int类型
#include<stdio.h>
#include<string.h>
int main() {
char str[] = {
'a', 'b', 'c', '\0', 'd'};
printf("字符串的长度为%u", strlen(str));
return 0;
}
Run the above code, the execution result is as follows: