Error[8]: Undefined offset: 943, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

一维数组 认识一维数组 数组的创建与自动初始化
//数组的创建
//类型 数组名 [数组长度]
int iArr[5];
float fArr[8];
char cArr[3];
//数组初始化
//全部初始化
int iArr[5] = {1,3,4,5,6};
//部分初始化
int iArr[5] = {1,3,6}; //没有初始化的部分默认为0
//缺省数组长度,数组根据数组元素自动推断
int iArr[] = {1,3,6,5,2,8,9};
//别致的写法
int array['B'];   //等效    int array[66]
//错误写法一
int iArr[5];
iArr[5] = {1,3,4,5,6};
//错误写法二
int iArr[] = {};
//错误写法三
iArr[5] = {1,3,4,5,6,4,6};  //数组元素不能大于数组长度
数组的创建与手动初始化
int iArr[3]={0};
for(int i=0;i<3;i++)
{
    scanf("%d",&iArr[i]);	
    //i=0  scanf("%d",&iArr[0]) 
    //i=1  scanf("%d",&iArr[1]) 
    //i=2  scanf("%d",&iArr[2]) 
}
//错误写法
int  size=0;
scanf("%d",&size);
int array[size];		 //数组长度必须是常量
数组的遍历(打印数组)
int iArr[3]={1,2,3};
for(int i=0;i<3;i++)
{
    printf("%d\t",iArr[i]);
}
字符数组 字符数组创建与初始化 字符串数组
3 cArr]='A',{'B','}';//字符串char[		3
] cArr="AB";//等效 char cArr[3]={"AB"};char[   				20
字符串注意事项 数组的常规 *** 作 数字类型的数组 字符类型的数组
100
	] cStr=""; gets_s (,
	100)cStr; //求输入的字符串的长度  可见长度int=
	0
	; i while ([
	] !=cStr')'i++ ; printf(
		i"数组的长度是:%d",
	);//统计输入的字符串中的字母,数字,其他符号的个数= i0;
	int
	i [ 3]
	= count0}; while {([]
	!= ')'cStrifi( [ ]'A'
	{
		&& [cStr]i<= >= 'Z' ) cStr[i0 ] ++; 
			countelseif([]
		'a' && [cStr]i<= >= 'z' ) cStr[i0 ] ++;
			countelseif([]
		'0' && [cStr]i<= >= '9' ) cStr[i1 ] ++;
			countelse[2]++
		; 
			count++;}printf(
		i"字母:%d,数字:%d,其他字符:%d\n",
	[
	0],[ count1],[ count2]); count[+++][+++][+++][+++][+++]
)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 944, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

一维数组 认识一维数组 数组的创建与自动初始化
//数组的创建
//类型 数组名 [数组长度]
int iArr[5];
float fArr[8];
char cArr[3];
//数组初始化
//全部初始化
int iArr[5] = {1,3,4,5,6};
//部分初始化
int iArr[5] = {1,3,6}; //没有初始化的部分默认为0
//缺省数组长度,数组根据数组元素自动推断
int iArr[] = {1,3,6,5,2,8,9};
//别致的写法
int array['B'];   //等效    int array[66]
//错误写法一
int iArr[5];
iArr[5] = {1,3,4,5,6};
//错误写法二
int iArr[] = {};
//错误写法三
iArr[5] = {1,3,4,5,6,4,6};  //数组元素不能大于数组长度
数组的创建与手动初始化
int iArr[3]={0};
for(int i=0;i<3;i++)
{
    scanf("%d",&iArr[i]);	
    //i=0  scanf("%d",&iArr[0]) 
    //i=1  scanf("%d",&iArr[1]) 
    //i=2  scanf("%d",&iArr[2]) 
}
//错误写法
int  size=0;
scanf("%d",&size);
int array[size];		 //数组长度必须是常量
数组的遍历(打印数组)
int iArr[3]={1,2,3};
for(int i=0;i<3;i++)
{
    printf("%d\t",iArr[i]);
}
字符数组 字符数组创建与初始化 字符串数组
3 cArr]='A',{'B','}';//字符串char[		3
] cArr="AB";//等效 char cArr[3]={"AB"};char[   				20
字符串注意事项 数组的常规 *** 作 数字类型的数组 字符类型的数组
100
	] cStr=""; gets_s (,
	100)cStr; //求输入的字符串的长度  可见长度int=
	0
	; i while ([
	] !=cStr')'i++ ; printf(
		i"数组的长度是:%d",
	);//统计输入的字符串中的字母,数字,其他符号的个数= i0;
	int
	i [ 3]
	= count0}; while {([]
	!= ')'cStrifi( [ ]'A'
	{
		&& [cStr]i<= >= 'Z' ) cStr[i0 ] ++; 
			countelseif([]
		'a' && [cStr]i<= >= 'z' ) cStr[i0 ] ++;
			countelseif([]
		'0' && [cStr]i<= >= '9' ) cStr[i1 ] ++;
			countelse[2]++
		; 
			count++;}printf(
		i"字母:%d,数字:%d,其他字符:%d\n",
	[
	0],[ count1],[ count2]); count[+++][+++][+++][+++]
)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 945, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

一维数组 认识一维数组 数组的创建与自动初始化
//数组的创建
//类型 数组名 [数组长度]
int iArr[5];
float fArr[8];
char cArr[3];
//数组初始化
//全部初始化
int iArr[5] = {1,3,4,5,6};
//部分初始化
int iArr[5] = {1,3,6}; //没有初始化的部分默认为0
//缺省数组长度,数组根据数组元素自动推断
int iArr[] = {1,3,6,5,2,8,9};
//别致的写法
int array['B'];   //等效    int array[66]
//错误写法一
int iArr[5];
iArr[5] = {1,3,4,5,6};
//错误写法二
int iArr[] = {};
//错误写法三
iArr[5] = {1,3,4,5,6,4,6};  //数组元素不能大于数组长度
数组的创建与手动初始化
int iArr[3]={0};
for(int i=0;i<3;i++)
{
    scanf("%d",&iArr[i]);	
    //i=0  scanf("%d",&iArr[0]) 
    //i=1  scanf("%d",&iArr[1]) 
    //i=2  scanf("%d",&iArr[2]) 
}
//错误写法
int  size=0;
scanf("%d",&size);
int array[size];		 //数组长度必须是常量
数组的遍历(打印数组)
int iArr[3]={1,2,3};
for(int i=0;i<3;i++)
{
    printf("%d\t",iArr[i]);
}
字符数组 字符数组创建与初始化 字符串数组
3 cArr]='A',{'B','}';//字符串char[		3
] cArr="AB";//等效 char cArr[3]={"AB"};char[   				20
字符串注意事项 数组的常规 *** 作 数字类型的数组 字符类型的数组
100
	] cStr=""; gets_s (,
	100)cStr; //求输入的字符串的长度  可见长度int=
	0
	; i while ([
	] !=cStr')'i++ ; printf(
		i"数组的长度是:%d",
	);//统计输入的字符串中的字母,数字,其他符号的个数= i0;
	int
	i [ 3]
	= count0}; while {([]
	!= ')'cStrifi( [ ]'A'
	{
		&& [cStr]i<= >= 'Z' ) cStr[i0 ] ++; 
			countelseif([]
		'a' && [cStr]i<= >= 'z' ) cStr[i0 ] ++;
			countelseif([]
		'0' && [cStr]i<= >= '9' ) cStr[i1 ] ++;
			countelse[2]++
		; 
			count++;}printf(
		i"字母:%d,数字:%d,其他字符:%d\n",
	[
	0],[ count1],[ count2]); count[+++][+++][+++]
)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 946, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

一维数组 认识一维数组 数组的创建与自动初始化
//数组的创建
//类型 数组名 [数组长度]
int iArr[5];
float fArr[8];
char cArr[3];
//数组初始化
//全部初始化
int iArr[5] = {1,3,4,5,6};
//部分初始化
int iArr[5] = {1,3,6}; //没有初始化的部分默认为0
//缺省数组长度,数组根据数组元素自动推断
int iArr[] = {1,3,6,5,2,8,9};
//别致的写法
int array['B'];   //等效    int array[66]
//错误写法一
int iArr[5];
iArr[5] = {1,3,4,5,6};
//错误写法二
int iArr[] = {};
//错误写法三
iArr[5] = {1,3,4,5,6,4,6};  //数组元素不能大于数组长度
数组的创建与手动初始化
int iArr[3]={0};
for(int i=0;i<3;i++)
{
    scanf("%d",&iArr[i]);	
    //i=0  scanf("%d",&iArr[0]) 
    //i=1  scanf("%d",&iArr[1]) 
    //i=2  scanf("%d",&iArr[2]) 
}
//错误写法
int  size=0;
scanf("%d",&size);
int array[size];		 //数组长度必须是常量
数组的遍历(打印数组)
int iArr[3]={1,2,3};
for(int i=0;i<3;i++)
{
    printf("%d\t",iArr[i]);
}
字符数组 字符数组创建与初始化 字符串数组
3 cArr]='A',{'B','}';//字符串char[		3
] cArr="AB";//等效 char cArr[3]={"AB"};char[   				20
字符串注意事项 数组的常规 *** 作 数字类型的数组 字符类型的数组
100
	] cStr=""; gets_s (,
	100)cStr; //求输入的字符串的长度  可见长度int=
	0
	; i while ([
	] !=cStr')'i++ ; printf(
		i"数组的长度是:%d",
	);//统计输入的字符串中的字母,数字,其他符号的个数= i0;
	int
	i [ 3]
	= count0}; while {([]
	!= ')'cStrifi( [ ]'A'
	{
		&& [cStr]i<= >= 'Z' ) cStr[i0 ] ++; 
			countelseif([]
		'a' && [cStr]i<= >= 'z' ) cStr[i0 ] ++;
			countelseif([]
		'0' && [cStr]i<= >= '9' ) cStr[i1 ] ++;
			countelse[2]++
		; 
			count++;}printf(
		i"字母:%d,数字:%d,其他字符:%d\n",
	[
	0],[ count1],[ count2]); count[+++][+++]
)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 947, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

一维数组 认识一维数组 数组的创建与自动初始化
//数组的创建
//类型 数组名 [数组长度]
int iArr[5];
float fArr[8];
char cArr[3];
//数组初始化
//全部初始化
int iArr[5] = {1,3,4,5,6};
//部分初始化
int iArr[5] = {1,3,6}; //没有初始化的部分默认为0
//缺省数组长度,数组根据数组元素自动推断
int iArr[] = {1,3,6,5,2,8,9};
//别致的写法
int array['B'];   //等效    int array[66]
//错误写法一
int iArr[5];
iArr[5] = {1,3,4,5,6};
//错误写法二
int iArr[] = {};
//错误写法三
iArr[5] = {1,3,4,5,6,4,6};  //数组元素不能大于数组长度
数组的创建与手动初始化
int iArr[3]={0};
for(int i=0;i<3;i++)
{
    scanf("%d",&iArr[i]);	
    //i=0  scanf("%d",&iArr[0]) 
    //i=1  scanf("%d",&iArr[1]) 
    //i=2  scanf("%d",&iArr[2]) 
}
//错误写法
int  size=0;
scanf("%d",&size);
int array[size];		 //数组长度必须是常量
数组的遍历(打印数组)
int iArr[3]={1,2,3};
for(int i=0;i<3;i++)
{
    printf("%d\t",iArr[i]);
}
字符数组 字符数组创建与初始化 字符串数组
3 cArr]='A',{'B','}';//字符串char[		3
] cArr="AB";//等效 char cArr[3]={"AB"};char[   				20
字符串注意事项 数组的常规 *** 作 数字类型的数组 字符类型的数组
100
	] cStr=""; gets_s (,
	100)cStr; //求输入的字符串的长度  可见长度int=
	0
	; i while ([
	] !=cStr')'i++ ; printf(
		i"数组的长度是:%d",
	);//统计输入的字符串中的字母,数字,其他符号的个数= i0;
	int
	i [ 3]
	= count0}; while {([]
	!= ')'cStrifi( [ ]'A'
	{
		&& [cStr]i<= >= 'Z' ) cStr[i0 ] ++; 
			countelseif([]
		'a' && [cStr]i<= >= 'z' ) cStr[i0 ] ++;
			countelseif([]
		'0' && [cStr]i<= >= '9' ) cStr[i1 ] ++;
			countelse[2]++
		; 
			count++;}printf(
		i"字母:%d,数字:%d,其他字符:%d\n",
	[
	0],[ count1],[ count2]); count[+++]
)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
C语言基础知识之一维数组_C_内存溢出

C语言基础知识之一维数组

C语言基础知识之一维数组,第1张

一维数组 认识一维数组
  • 多个普通变量的集合,并且这些变量的名字是有规律,内存是连续的
  • 不存在数组下标 = 数组长度
  • 数组计数从0开始
  • 数组最大下标是数组长度减1
  • 数组名的含义:整个数组的首地址
数组的创建与自动初始化
//数组的创建
//类型 数组名 [数组长度]
int iArr[5];
float fArr[8];
char cArr[3];
//数组初始化
//全部初始化
int iArr[5] = {1,3,4,5,6};
//部分初始化
int iArr[5] = {1,3,6}; //没有初始化的部分默认为0
//缺省数组长度,数组根据数组元素自动推断
int iArr[] = {1,3,6,5,2,8,9};
//别致的写法
int array['B'];   //等效    int array[66]
//错误写法一
int iArr[5];
iArr[5] = {1,3,4,5,6};
//错误写法二
int iArr[] = {};
//错误写法三
iArr[5] = {1,3,4,5,6,4,6};  //数组元素不能大于数组长度
数组的创建与手动初始化
int iArr[3]={0};
for(int i=0;i<3;i++)
{
    scanf("%d",&iArr[i]);	
    //i=0  scanf("%d",&iArr[0]) 
    //i=1  scanf("%d",&iArr[1]) 
    //i=2  scanf("%d",&iArr[2]) 
}
//错误写法
int  size=0;
scanf("%d",&size);
int array[size];		 //数组长度必须是常量
数组的遍历(打印数组)
int iArr[3]={1,2,3};
for(int i=0;i<3;i++)
{
    printf("%d\t",iArr[i]);
}
字符数组 字符数组创建与初始化
  • 普通字符数组与数字类数组没啥区别

    char cArr[3]={'A','B','C','D'};   //只有在创建数组的时候初始化
    //错误写法
    cArr[3]={'A','B','C'};
    //输入
    for(int i=0;i<3;i++)
    {
        //scanf_s("%c",&cArr[i])
    	scanf_s("%c",&cArr[i],1);    
    }
    //输出
    for(int i=0;i<3;i++)
    {
    	putchar(cArr[i]);
        //printf("%c",cArr[i]);
    }
    
字符串数组
  • 字符串结束标记 :‘
  • 字符串的专有格式空字符: %s
  • %s工作机制: 从一个地址开始 访问到char 结束 %s对应的是一个地址
  • [
3 cArr]='A',{'B','}';//字符串char[		3
] cArr="AB";//等效 char cArr[3]={"AB"};char[   				20
字符串注意事项
  • %s 的方式只能打印字符串,不能打印字符数组,如果缺少’\0’ 将会打印乱码

  • 为了有效避免忘记加’\0’ 养成用空的字符串初始化一个字符数组,如

    ] str="";// char str[]=""; 错误写法//统计数组元素出现的次数int
    [
    
  • 字符串输入 scanf 遇到空格时候会截取当前字符,结束输入,

  • 也就是 带有空格的字符串输入要采用字符串处理函数: gets做输入,在其他编译中直接gets ,在vs中用的是:get_s(地址,长度); 与之对应的打印函数是:put_s(要打印的数组名);

  • 字符和字符串输入前,需要清空缓冲区

数组的常规 *** 作 数字类型的数组
  • 数据处理

  • 充当计数器

    	]
    	= num1, 2 { ,3,4,2,3,1,2,4,5,6,5,4,5,7,6,7,8,8,9,7,9};int [10
    	] nCount=0} ; { for (int
    	= 0; i < 22; i ++ )[ i[]
    	{
    		nCount]num++i;}for(
    	int
    	= 0; i < 10; i ++ )if i([
    	{
    		] !=nCount0i) printf ("%d 出现了 %d\n" 
    		{
    			,,[] i) nCount;i}}
  • 数组求和
  • int [
      3
    ] iArr=1, 2 { ,3 }; int =0
    	; sum for (int
    	= 0; i < 3; i ++ )+= i[] {
    		sum ; iArr}iprintf(
    	"sum = %d\n"
    	,);printf sum("请输入:"
    
    • 数组有序插入

      );scanf_s("%d"
      			,&); [temp++]
      			data=curSize;for ( tempint
      			= -1 i ; curSize 0 ;-- i > )if i([ {
      				] <data[i- 1 data]i ) int=[ {
      					] temp ; data[i]=
      					data[i- 1 data]i ; [-1
      					data]i = ;} } tempprintf
      				(
      			"输入要删除的数据:"
      
    • 数组元素删除

      );scanf_s("%d"
      			,&); fortemp(int
      			= 0; i < ;++ i ) curSizeif i(== {
      				[ ]temp ) data//printf("%d\t", i);i=; {
      					break
      					pos ; i}
      					}//所有数组的删除都是伪删除,数组内存是栈区内存
      				for
      			(
      			int
      			= ;< i ; pos++ i ) curSize//移位 *** 作 i[] {
      				=
      				data[i+ 1 data]i ; }break;
      			//查找
      			printf(
      
    • 数组 元素查找

      "输入要查找找的数据:"
      			);scanf_s("%d"
      			,&); fortemp(int
      			= 0; i < ;++ i ) curSizeif i(== {
      				[ ]temp ) dataprintfi("%d\t" {
      					,);} i}printf
      				(
      			"\n"
      			);//修改printf(
      
    • 数组元素更改

      "输入要修改的数据:"
      			);scanf_s("%d"
      			,&); fortemp(int
      			= 0; i < ;++ i ) curSizeif i(== {
      				[ ]temp ) data//printf("%d\t", i);i=; {
      					break
      					pos ; i}
      					}//修改匹配到的数据
      				printf
      			(
      			"请输入新的数据:"
      			);scanf_s("%d"
      			,&);[temp]=
      			data;posprintf ( temp"修改成功!\n"
      			);for(int
      
    • 打印所有数组元素

      = 0; i < ;++ i ) curSizeprintf i("%d\t" {
      				,[]) data;i}printf(
      			"\n"
      			);//冒泡排序int[
      
  • 一维数组冒泡排序

    ]
    	= iArr12, 23 { ,45,14,67,87,38,57,83,10};int =10
    	; size for (int
    	= 0; i < -1 i ; size ++ )//控制元素从第几个开始冒泡 ifor( int
    	{
    		= 0; j < -1 j - size ; ++ ) i//描述比较 jif(   [
    		{
    			//小----->大
    			] [iArr+j1 > iArr]j)int=[
    			{
    				] temp ; iArr[j]=
    				iArr[j+ 1 iArr]j ; [+1
    				iArr]j = ;} } temp}
    			//打印数组
    		for
    
    	(
    	int
    	= 0; i < ;++ i ) sizeprintf i("%d\t"
    	{
    		,[]) iArr;i}printf(
    	"\n"
    	);
  • 处理字符
  • 字符串处理
  • //数组计数功能功能
字符类型的数组
    char[
100
	] cStr=""; gets_s (,
	100)cStr; //求输入的字符串的长度  可见长度int=
	0
	; i while ([
	] !=cStr')'i++ ; printf(
		i"数组的长度是:%d",
	);//统计输入的字符串中的字母,数字,其他符号的个数= i0;
	int
	i [ 3]
	= count0}; while {([]
	!= ')'cStrifi( [ ]'A'
	{
		&& [cStr]i<= >= 'Z' ) cStr[i0 ] ++; 
			countelseif([]
		'a' && [cStr]i<= >= 'z' ) cStr[i0 ] ++;
			countelseif([]
		'0' && [cStr]i<= >= '9' ) cStr[i1 ] ++;
			countelse[2]++
		; 
			count++;}printf(
		i"字母:%d,数字:%d,其他字符:%d\n",
	[
	0],[ count1],[ count2]); count

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/3002720.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-27
下一篇 2022-09-27

发表评论

登录后才能评论

评论列表(0条)

保存