3-3

函数:strcpy(t,s);
把s中的字符串拷贝到t中。s可以是一个数组名,也可以是有个字符串常量。成功,返回第一个参数的值。
例:

include <stdio.h>

include <string.h>

void main()
{
char t[50];
//char s[]="i love beijing";
//strcpy(t,s);
strcpy(t,"i love beijing");
puts(t);
// puts(s);
}
练习:mystrcpy();

include <stdio.h>

//#include <string.h>
void mystrcpy(char *a,char *b)
{
int i=0;
while(b[i]!='\0')
{
a[i]=b[i];
i++;
}
a[i]='\0';
}
void main()
{
char a[50];
char b[]="abcdefg";
mystrcpy(a,b);
puts(a);
}

include <stdio.h>

include <string.h>

void main()
{
char a[50]="abcdefg";
char b[]="hijklmn";
char *p=strcpy(a+2,b);
//strcpy成功,返回第一个参数的值。
puts(a);
puts(p);
}

strncpy(a,b,n):把b中的n个字符拷贝到a中。

include <stdio.h>

include <string.h>

void main()
{
char a[10]="abcdefg";
char b[20]="bbbbbbbbnmnmnmnm";
char *p=strncpy(a+3,b,5);
//成功,返回第一个参数的值。
puts(a);
puts(p);
}

strcat(a,b);把b中的字符串接在a中的最后。作为一个整体的字符串存在a中。所以a数组的大小应足够容纳得下最终的字符串。
有返回值,返回的是第一个参数的值。
例:

include <stdio.h>

include <string.h>

void main()
{
char a[50]="abcd";
char b[]="efgh";
char *p=strcat(a,b);
puts(a);
puts(b);
puts(p);

}

strncat(a,b,n):把b中的n个字符接在a的后面,并把最终的字符串存在a中。有返回值,返回第一个参数的值。

include <stdio.h>

include <string.h>

void main()
{
char a[50]="abcd";
char b[]="efgh";
char *p=strncat(a,b,3);
puts(a);
puts(b);
puts(p);
}

strcmp(str1,str2); 比较字符串str1和str2是否相等(一致),相等,返回0;否则,str1大于str2,返回整数;str1小于str2,返回负数。
例:

include <stdio.h>

include <string.h>

void main()
{
char a[50]="abdd";
char b[]="abcd";
int n=strcmp(a,b);
printf("%d\n",n);
}
2个字符串比大小:一个字符一个字符的比,比较第一个不相同字符,哪一个ascii值大,则这个字符串大。
abcde
abcfe => d<f 所以下面的字符串大。

strncmp(str1,str2,n):比较str1和str2前n个字符是否一致。一致返回0,

include <stdio.h>

include <string.h>

void main()
{
char *list[]={"wtl is nb","wtl is tall","wtl is smart","wtl is beautiful","wtl is wise"};
int i;
for(i=0;i<=4;i++)
{
if(strncmp(list[i],"wtl is a good boy",6)==0)
printf("%s\n",list[i]);
}

}

int a=1;
char c='a';
printf("this is %d %c",a,c);
sprintf(b,"this is %d %c",a,c);
sprintf():把printf本该输出到屏幕上的字符串输出到一个字符数组中去了。
例:

include <stdio.h>

include <string.h>

void main()
{
int a=1;
char b='b';
char c[30];
sprintf(c,"this is %d %c",a,b);
printf("%s\n",c);
}
//strchr() strrchr()

学生:
年龄:int
性别:char
姓名:char []
分数:float

自定义一个数据类型,用来接受学生的个人信息
struct student
{
int age;
char sex;
char name[30];
flaot score;
};
//用该自定义的数据类型定义一个变量stu,用来接受学生的个人信息。
struct student stu; //int a;

例:

include <stdio.h>

//定义一个变量,来接受一个学生的个人信息。
//自定义一个相应的数据类型
struct student
{
int num;
char name[30];
char sex;
float score;
};//分号不能漏
void main()
{//用该数据类型定义的变量就可以接受学生信息 类型名+变量名
struct student stu1={13,"xxx",'m',78.9};
//数据类型本身不占内存,但是用类型定义的变量会给变量分配空间。
printf("num:%d name:%s sex:%c score:%.1f\n",stu1.num,stu1.name,stu1.sex,stu1.score);

}
/*
1.结构体类型定义通常会放在头文件中,如果不放在头文件中,通常会放在程序#include的后面
2.定义好结构体类型后,当要用该类型定义变量时,结构体类型名为:struct +结构体名 +变量名。如:struct student stu1; struct关键字不能省略
3.结构体名可以和成员变量名同名:
例:
struct test
{
int a;
int test;
};
二者互不影响
4.一个结构体中的成员可以是另一个结构体类型定义的变量
例:
struct data
{
int year;
int month;
int day;
};
struct student
{
int num;
char name[20];
struct data bithday;
float score;
};
访问结构体成员: 结构体变量名.成员名
定义一个学生: struct student a;
访问它的学号: a.num;
访问他的出生year: a.birthday.year;
5.结构体中的成员名,可以和其他变量名同名,也互不影响
6.定义结构体类型的时候,它的数据成员不能是自己的结构体类型定义的变量。
例:
struct test
{
int a;
struct test b; 错
};
7.结构体变量除了在初始化的时候可以整体赋值外,其他时候不能整体访问,只能通过访问它的成员变量来访问它。
8.用一个结构体类型定义一个变量,必须在定义变量之前得存在这个结构体类型的定义,否则出错。
*/

定义一个结构体变量:
1.先定义结构体类型,再定义结构体变量
例:
struct student
{
int num;
char sex;
};
struct student stu1;
2.在定义结构体类型的同时定义结构体变量
struct student
{
int num;
char sex;
}stu1,stu2; 定义了2个结构体变量stu1,stu2.
struct student stu3;

3.定义无名结构体
struct
{
int num;
char sex;
}stu1,stu2;
这总无名结构体类型只能用一次,所以只能在它定义类型的同时定义变量。后面用不了了。

include <stdio.h>

struct student
{
int num;
char sex;
};
void main()
{
struct student stu1={1,'w'},stu2;
stu2=stu1;//对的
// if(stu2==stu1)错的
// printf("xxx\n");
printf("%d %c\n",stu2.num,stu2.sex);
}
/*
注:不能整体操作一个结构体变量,但是在同种类型定义的结构体变量之间可以相互赋值。只有这个意外。
*/

例:

include <stdio.h>

struct student
{
int num;
char sex;
};
void main()
{
struct student a={1,'m'};
struct student p=&a;
printf("%d %c\n",a.num,a.sex);
printf("%d %c\n",(
p).num,(*p).sex);
printf("%d %c\n",p->num,p->sex);
}

定义一个结构体类型数组:

struct student
{
int num;
char sex;
}stu[30];

struct student
{
int num;
char sex;
};
struct student stu[30];
3
struct
{
int num;
char sex;
}stu[30];

结构体数组的赋值:
1.初始化赋值
a.
struct student
{
int num;
char sex;
}stu[2]={1,'m',2,'w'};

例:

include <stdio.h>

struct student
{
int num;
char sex;
}stu[2]={{1,'m'},{2,'w'}};
//={1,'m',2,'w'};
void main()
{
printf("%d %c\n",stu[0].num,stu[0].sex);
printf("%d %c\n",stu[1].num,stu[1].sex);
}

b.
struct student
{
int num;
char sex;
};
struct student stu[2]={1,'m',2,'w'};

c.
struct
{
int num;
char sex;
}stu[2]={1,'m',2,'w'};

2.先定义再赋值。一个一个赋值
struct student
{
int num;
char sex;
};
struct student stu[2];
stu[0].num=1;
stu[0].sex='m';
stu[1].num=2;
stu[1].sex='w';

练习:定义一个结构体数组,输入3个学生的个人信息并输出。

include <stdio.h>

struct student
{
int num;
char name[30];
char sex;
};
void main()
{
struct student stu[3];
int i;
for(i=0;i<3;i++)
{
printf("please input data:");
scanf("%d %s %c",&stu[i].num,stu[i].name,&stu[i].sex);
}
for(i=0;i<3;i++)
{
printf("%d %s %c\n",stu[i].num,stu[i].name,stu[i].sex);
}
}

include <stdio.h>

struct student
{
int num;
char name[30];
char sex;
};
void main()
{
struct student stu[3];
struct student *p=stu;
for(;p<=stu+2;p++)
{
printf("please input data:");
scanf("%d %s %c",&p->num,p->name,&p->sex);
}
for(p=stu;p<=stu+2;p++)
{
printf("%d %s %c\n",p->num,p->name,p->sex);
}
}

练习:定义一个表示平面上的点的数据类型,然后定义函数,给定一个点和半径,在这个函数中判断,该点是不是在以原点为中心,给定的半径的圆内。sqrt(xx+yy) math.h
gcc 1.c -lm

include <stdio.h>

include <math.h>

//定义一个描述点(x,y)的数据类型
struct point
{
double x;
double y;
};

int Cycle(struct point p,double l)
{
if(sqrt(p.xp.x+p.yp.y)<=l)
return 1;
else
return 0;

}
void main()
{
//定义一个点
struct point p;
printf("输入点的位置:");
scanf("%lf %lf",&p.x,&p.y);
printf("输入半径的长度:");
double l;
scanf("%lf",&l);
if(Cycle(p,l))
printf("在圆内!\n");
else
printf("在圆外!\n");
}

include <stdio.h>

typedef struct student
{
int num;
char sex;
}Stu;
typedef float X;
void main()
{
Stu stu1;
Stu stu2;
X a=1.2;
printf("%.1f\n",a);
}/*
typedef:类型重命名
格式:typedef 类型名 新类型名;
例: typedef float X; 以后X就表示float.
*/

共用体/联合体

include <stdio.h>

//定义了一个共用体类型,关键字union
union data
{
int a;
int b;
char c;
};
void main()
{
//用共用体类型定义变量
union data p;
p.a=356;
printf("%d\n",p.b);
printf("%d\n",p.c);
}
/*
注:共用体的所有成员公用一块存储区域,它的大小是成员最大的那个成员的大小。
*/

include <stdio.h>

//自定义一个枚举类型
enum weekday{sunday=5,monday,tursday};
void main()
{ enum weekday day;
day=sunday;
printf("%d\n",day);
printf("%d %d %d\n",sunday,monday,tursday);
}/*
1.枚举类型,把用枚举类型定义的变量的所有可能的取值都放在定义它的大括号中
2.枚举类型中列出的项都是一个常量,不能给它赋值,它本身对应有一个标号值。默认第一项从0开始,后面的项依次递加。
3.可以在定义枚举类型的同时给第一项赋值,改变对应标号的初始值。
*/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容