目录
1、continue、break、goto关键字、数组、字符串数组处理函数
1、continue、break、goto关键字、数组、字符串数组处理函数
/*
break ;
continue;
逗号运算符
条件运算符
goto 标签;【尽量避免使用 一般是内循环直接跳到外循环】
*/
//1
#include <stdio.h>
int main()
{
int ch;
while((ch=getchar())!='\n')
{
if(ch=='C')
{
continue;
}
putchar(ch);
}
putchar('\n');
return 0;
}
[wlp@192 class_1]$ gcc test5.c && ./a.out
l love fishC
l love fish
//2
#include <stdio.h>
int main()
{
int i=5;
while(i++)
{
if(i>10)
{
goto a;
}
}
a: printf("Here,i=%d\n",i);
return 0;
}
[wlp@192 class_1]$ vi test5.c
/*
数组:
C99特性:
int a[10]={[3]=3,[5]=5,[8]=8};【其余默认赋值为0】
sizeof(数组名); 计算数组占用内存大小 单位字节
*/
//输入并求出10位同学的平均分
#include <stdio.h>
#define NUM 10
int main()
{
int s[NUM];//定义一个数组 []里面可以省略
int i,sum=0;
for(i=0;i<10;i++)
{
printf("请输入第%i位同学的成绩:",i+1);//%i与%d效果相同
scanf("%d",&s[i]);//向数组里存数据
sum+=s[i];
}
printf("评均分是%.2f\n",(double)sum/NUM);
return 0;
}
//创建输入自定义长度的数组
#include <stdio.h>
int main()
{
int n,i;
printf("输入数组的个数:");
scanf("%d",&n);
char a[n+1];//+1是为了增加\0
printf("开始输入字符:");
getchar();//接受回车
for(i=0;i<n;i++)
{
scanf("%c",&a[i]);
}
a[n]='\0';
printf("输入的字符串是:%s\n",a);
return 0;
}
/*
字符串常量初始化数组:
char str[]="FisgC";
初始化字符数组:
char str1[]={'F','i','s','h','C','\0'};
字符串处理函数:
获取字符串长度:
1、strlen(数组名)
拷贝字符串:
2、strcpy(数组名1,数组名2)
后面的内容拷贝到前面
注意:数组1长度要大于等于数组2
3、strncpy(数组名1,数组名2,想要获取到的长度)
连接字符串:
strcat(字符串2/数组2,字符串2/数组2);
strncat()
*/
//1
#include <stdio.h>
#include <string.h>
int main()
{
char str[]="I love you";
printf("sizeof str=%d\n",sizeof(str));
printf("strlen str=%u\n",strlen(str));//unsigned型的
return 0;
}
===============================================
//2
#include <stdio.h>
#include <string.h>
int main()
{
char str1[]="Original String";
char str2[]="New String";
char str3[100];
strcpy(str1,str2);
strcpy(str3,"Copy Successful");
printf("str1:%s\n",str1);
printf("str2:%s\n",str2);
printf("str3:%s\n",str3);
return 0;
}
[wlp@192 class_1]$ gcc test5.c && ./a.out
sizeof str=11 //包含\0
strlen str=10
//3
#include <stdio.h>
#include <string.h>
int main()
{
char str1[]="To be or not to be";
char str2[40];
strncpy(str2,str1,5);
str2[5]='\0';
printf("str2:%s\n",str2);
return 0;
}
[wlp@192 class_1]$ gcc test5.c && ./a.out
str2:To be
=====================================================
//4
#include <stdio.h>
#include <string.h>
int main()
{
char str1[]="I love";
char str2[]="FishC.com";
strcat(str1," ");
strcat(str1,str2);
printf("str2:%s\n",str1);
return 0;
}
[wlp@192 class_1]$ gcc test5.c && ./a.out
str2:I love FishC.com
=========================================================
//6
#include <stdio.h>
#include <string.h>
int main()
{
char str1[]="FishC.com";
char str2[]=".cn";
char str3[]=".cn";
if(strcmp(str1,str2))
{
printf("两个字符串一致\n");
}
else
{
printf("两个字符串有差异\n");
}
return 0;
}
[wlp@192 class_1]$ gcc test5.c && ./a.out
两个字符串一致
2、二维数组、指针变量、指针与数组
/*
二维数组;
指针、指针变量;
指针和数组;
*/
/*
二维数组:
定义
int a[6][5];//理解为6*5的二维数组//只有第一维度可以省略
*/
//1
#include <stdio.h>
int main()
{
int a[3][4]={{1,2,3,4},//3可以省略
{5,6,7,8},
{9,10,11,12}
};
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}
[wlp@192 class_1]$ gcc test5.c && ./a.out
1 2 3 4
5 6 7 8
9 10 11 12
/*
指针:即地址
指针变量:存放指定数据类型的地址值
int *p=num; >> p里存的是&num
定义指针变量:
int *a;//定义一个指向整型的指针变量
获取某个变量的地址:
int *a=&b;//将获取的地址赋值给指针变量
获取指针变量指向的数据:
printf("%d\n",*a);
注意:避免访问为初始化的指针
int num=520;
int *p=#
则:
num 520
p【指针】 &num
*p num 520
pp【指针的指针】 &p
*pp p &num
**pp *p num 520
指向指针的指针指向指针数组:
优势:1、避免重复分配内存
2、只需要进行一处修改。
*/
#include <stdio.h>
int main()
{
char *cBooks[]={//定义指针数组
"C程序设计语言",
"C专家编程",
"C陷阱与缺陷",
"C和指针",
"带你学C带你飞",
"C Primer Plus"
};
char **byFishC;//byFishC相当于pp
char **jiayuloves[4];
int i;
byFishC=&cBooks[5];//指向指针的指针指向指针数组 【pp=&p】
jiayuloves[0]=&cBooks[0];
jiayuloves[1]=&cBooks[1];
jiayuloves[2]=&cBooks[2];
jiayuloves[3]=&cBooks[3];
printf("FishC出版的书有:%s\n",*byFishC);
printf("小甲鱼喜欢的书有:\n");
for(i=0;i<4;i++)
{
printf("%s\n",*jiayuloves[i]);
}
return 0;
}
[wlp@192 class_1]$ gcc test5.c && ./a.out
FishC出版的书有:C Primer Plus
小甲鱼喜欢的书有:
C程序设计语言
C专家编程
C陷阱与缺陷
C和指针
//1
#include <stdio.h>
int main()
{
char a='F';
int f=123;
char *pa=&a;
int *pf=&f;
printf("a=%c\n",*pa);
printf("f=%d\n",*pf);
printf("the addr of a is :%p\n",pa);
printf("the addr of f is:%p\n",pf);//地址值占位符%p
return 0;
}
[wlp@192 class_1]$ gcc test5.c && ./a.out
a=F
f=123
the addr of a is :0x7fffdc7ff16f
the addr of f is:0x7fffdc7ff168
/*
指针和数组很像,都可以通过索引或地址访问存放在对应地址的值
数组地址就是数组第一个元素地址
*/
//指针变量间接访问变量
#include <stdio.h>
int main()
{
int a;
int *pa=&a;
printf("请输入一个整数:");
scanf("%d",&a);
printf("a=%d\n",a);
printf("请重新输入一个整数:");
scanf("%d",pa);//通过指针间接访问变量[取地值操作符&在定义指针变量已经写过了]
printf("a=%d\n",a);
return 0;
}
==
//数组地址与数组元素地址关系
#include <stdio.h>
int main()
{
char str[120];
printf("请输入字符:");
scanf("%s",str);//这里没有加取地址符,因为直接打印数组名,就是打印数组地址%p接收
printf("输入的是:%s\n",str);
printf("str地址是:%p\n",str);
printf("str第一个元素地址是:%p\n",&str[0]);
return 0;
}
[wlp@192 class_1]$ gcc test5.c && ./a.out
请输入字符:aa
输入的是:aa
str地址是:0x7fffe120ed80
str第一个元素地址是:0x7fffe120ed80
#include <stdio.h>
int main()
{
char a[]="FishC";
int b[5]={1,2,3,4,5};
float c[5]={1.1,1.2,1.3,1.4,1.5};
double d[]={1.1,1.2,1.3,1.4,1.5};
char *p=a;
printf("*p=%c,*(p+1)=%c,*(p+2)=%c\n",*p,*(p+1),*(p+2));
printf("a[0] ->%p,a[1] ->%p,a[2] ->%p\n",&a[0],&a[1],&a[2]);
printf("b[0] ->%p,b[1] ->%p,b[2] ->%p\n",&b[0],&b[1],&b[2]);
printf("c[0] ->%p,c[1] ->%p,c[2] ->%p\n",&c[0],&c[1],&c[2]);
printf("d[0] ->%p,d[1] ->%p,d[2] ->%p\n",&d[0],&d[1],&d[2]);
return 0;
}
[wlp@192 class_1]$ gcc test5.c && ./a.out
*p=F,*(p+1)=i,*(p+2)=s
a[0] ->0x7fff7b827980,a[1] ->0x7fff7b827981,a[2] ->0x7fff7b827982
b[0] ->0x7fff7b827960,b[1] ->0x7fff7b827964,b[2] ->0x7fff7b827968
c[0] ->0x7fff7b827940,c[1] ->0x7fff7b827944,c[2] ->0x7fff7b827948
d[0] ->0x7fff7b827910,d[1] ->0x7fff7b827918,d[2] ->0x7fff7b827920
==
//通过指针变量遍历字符串
#include <stdio.h>
#include <string.h>
int main()
{
char *str="I love you!";//通过指针变量间接初始化字符内容
int i,length;
length=strlen(str);
for(i=0;i<length;i++)
{
printf("%c",str[i]);
}
printf("\n");
return 0;
}
[wlp@192 class_1]$ gcc test5.c && ./a.out
I love you!
==
//指针变量遍历一卫数组
#include <stdio.h>
int main()
{
int array[10]={0,1,2,3,4,5,6,7,8,9};
int *p=array;
int i;
for(i=0;i<10;i++)
{
printf("%d\n",*(p+i));
}
return 0;
}
//指针变量遍历二维数组
#include <stdio.h>
int main()
{
int array[3][4]={
{0,1,2,3},
{4,5,6,7},
{8,9,10,11}}:
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%2d",*(*(array+i)+j)):
}
printf("\n");
}
return 0;
}
==
//数组指针遍历一维数组
#include <stdio.h>
int main()
{
int temp[5]={1,2,3,4,5};
int (*p2)[5]=&temp;//()为p,存放地址
int i;
for(i=0;i<5;i++)
{
printf("%d\n",*(*p2+i));//输出的是值,所以外面加取值符
}
return 0;
}
//数组指针遍历二维数组
#include <stdio.h>
int main()
{
int array[][4]={
{0,1,2,3},
{4,5,6,7},
{8,9,10,11}}:
int (*p)[4]=array;//看成一维数组指针变量;
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%2d",*(*(p+i)+j));//指针变量遍历二维数组
}
printf("\n");
}
return 0;
}
3、数组与指针的关系
/*
指针与数组的关系
指针数组
数组指针
多维数组实质
*/
/*
1、指针与数组的区别:
指针变量是一个左值【地址,且名字可变】;数组名只是一个地址
2、指针数组和数组指针:
2.1、指针数组:和指针变量差不多理解;//int *p=num; p=#
是数组,每个数组元素存放一个指针变量【这里数组下标[]优先级要比取值运算符*要高】
如:int *p1[5]; 每一个下标存放的是整型指针变量
2.2、数组指针:是指针,指向数组【这里()与[]优先级相同,所以从左到右】
如:int (*p2)[5];
*/
//1、通过指针变量计算字符串有多少字符
#include <stdio.h>
int main()
{
char str[]="I love you";
char *target=str;//定义指针变量指向数组,这样while里就为左值,可变的了
int count=0;
while(*target++ !='\0')//字符数组指针指向结束位为止
{
count++;
}
printf("总共有%d个字符\n",count);
return 0;
}
[wlp@192 class_1]$ gcc test5.c && ./a.out
总共有10个字符
==
//2.1、初始化指针数组
#include <stdio.h>
int main()
{
int a=1;
int b=2;
int c=3;
int d=4;
int e=5;
int *p1[5]={&a,&b,&c,&d,&e};
int i;
for(i=0;i<5;i++)
{
printf("%d",*p1[i]);
}
return 0;
}
12345
//注意与下一个对比
#include <stdio.h>
int main()
{
char a[]="FishC";
char *p=a;
printf("*p=%c,*(p+1)=%c,*(p+2)=%c\n",*p,*(p+1),*(p+2));
return 0;
}
[wlp@192 class_1]$ gcc test5.c && ./a.out
*p=F,*(p+1)=i,*(p+2)=s
==
#include <stdio.h>
int main()
{
char *p1[3]={
"加油吧",
"少年",
"此时不搏,何时搏"
};
int i;
for(i=0;i<3;i++)
{
printf("%s\n",p1[i]);//这里如果加*,则是打印的字符串地址
}
return 0;
}
[wlp@192 class_1]$ gcc test5.c && ./a.out
加油吧
少年
此时不搏,何时搏
//2.2、
//*取值符 &取址符 这里*p2=&temp
#include <stdio.h>
int main()
{
int temp[5]={1,2,3,4,5};
int (*p2)[5]=&temp;//定义指针指向【存放】数组第一个元素地址
int i;
for(i=0;i<5;i++)
{
printf("%d\n",*(*p2+i));//输出的是值,所以外面加取值符
}
return 0;
} //与下面代码比较
#include <stdio.h>
int main()
{
int temp[5]={1,2,3,4,5};
int *p2=temp;
int i;
for(i=0;i<5;i++)
{
printf("%d\n",*(p2+i));
}
return 0;
}
/*
多维数组的实质,就是一维数组的线性拓展
*(array+i)==array[1]
*(*(array+i))==array[i][j]
*(*(*(array+i)+j)+k)==array[i][j][k]
*/
#include <stdio.h>
int main()
{
int array[4][5]={0};
printf("sizeof int:%d\n",sizeof(int));
printf("array:%p\n",array);
printf("array+1:%p\n",array+1);
return 0;
}
[wlp@192 class_1]$ gcc test5.c && ./a.out
sizeof int:4
array:0x7fff03b1a010
array+1:0x7fff03b1a024//跨越了5个元素
/*
数组指针与二维数组
*/
#include <stdio.h>
int main()
{
int array[2][3]={{0,1,2},{3,4,5}};
int (*p)[3]=array;
printf("%d\n",**(p+1));
printf("%d\n",**(array+1));
printf("%d\n",array[1][0]);
printf("%d\n",*(*(p+1)+2));
printf("%d\n",*(*(array+1)+2));
printf("%d\n",array[1][2]);
return 0;
}
[wlp@192 class_1]$ gcc test5.c && ./a.out
3
3
3
5
5
5
/*
例题:输入并输出数组的全部元素
1、下标法
2、数组名计算数组元素地址,找出元素的值
3、用指针变量指向数组元素
*/
#include <stdio.h>
void main()
{
int a[10];
int i;
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("\n");
//2、
for(i=0;i<10;i++)
printf("%d",*(a+i));
//3、
int *p;
for(p=a;p<(a+10);p++)
{
printf("%d",*p);
}
}
C语言基础语法【3】_z输关的博客-CSDN博客