3 1 includeiostream h void main int x10





![字符数组的指针变量 可以定义一个指针变量,存放字符数组的地址。 程序 3. 4 #include<iostream. h> void main( ) {char str[30]="This is a 字符数组的指针变量 可以定义一个指针变量,存放字符数组的地址。 程序 3. 4 #include<iostream. h> void main( ) {char str[30]="This is a](https://slidetodoc.com/presentation_image_h/2b67c68780d3212fc32937119556caa0/image-6.jpg)

![程序 3. 5 #include<string. h> #include<iostream. h> void main() { char str[20]="abcde"; 输出结果为: The 程序 3. 5 #include<string. h> #include<iostream. h> void main() { char str[20]="abcde"; 输出结果为: The](https://slidetodoc.com/presentation_image_h/2b67c68780d3212fc32937119556caa0/image-8.jpg)

![程序 3. 6 #include<string. h> #include<iostream. h> void main() {char str 1[20], str 2[20], 程序 3. 6 #include<string. h> #include<iostream. h> void main() {char str 1[20], str 2[20],](https://slidetodoc.com/presentation_image_h/2b67c68780d3212fc32937119556caa0/image-10.jpg)
![程序 3. 7 #include<string. h> #include<iostream. h> void main() {char str 1[20]="abcd", str 2[40]="12345"; 程序 3. 7 #include<string. h> #include<iostream. h> void main() {char str 1[20]="abcd", str 2[40]="12345";](https://slidetodoc.com/presentation_image_h/2b67c68780d3212fc32937119556caa0/image-11.jpg)
![程序 3. 8 #include<string. h> #include<iostream. h> void main() {char str 1[20]="bag", str 2[40]="big"; 程序 3. 8 #include<string. h> #include<iostream. h> void main() {char str 1[20]="bag", str 2[40]="big";](https://slidetodoc.com/presentation_image_h/2b67c68780d3212fc32937119556caa0/image-12.jpg)





![程序 3. 11 定义和使用结构体 struct student { char number[10]; char name[10]; char sex; int 程序 3. 11 定义和使用结构体 struct student { char number[10]; char name[10]; char sex; int](https://slidetodoc.com/presentation_image_h/2b67c68780d3212fc32937119556caa0/image-18.jpg)
![结构体数组 v数组的每个元素是结构体类型,这样的数组称为结构体数组。 v访问结构体数组的格式:数组名[下标]. 成员名 程序 3. 12 结构体数组 struct student { char number[10]; char 结构体数组 v数组的每个元素是结构体类型,这样的数组称为结构体数组。 v访问结构体数组的格式:数组名[下标]. 成员名 程序 3. 12 结构体数组 struct student { char number[10]; char](https://slidetodoc.com/presentation_image_h/2b67c68780d3212fc32937119556caa0/image-19.jpg)

![结构体中包括函数 在C++中,结构体可以包括数据成员和函数。 程序 3. 14 struct student { char number[10]; char name[10]; char sex; 结构体中包括函数 在C++中,结构体可以包括数据成员和函数。 程序 3. 14 struct student { char number[10]; char name[10]; char sex;](https://slidetodoc.com/presentation_image_h/2b67c68780d3212fc32937119556caa0/image-21.jpg)



- Slides: 24


程序 3. 1 使用&和*运算符举例 #include<iostream. h> void main() {int x=10, *pointer; pointer=&x; cout<<"x="<<x<<endl; cout<<"*pointer="<<*pointer<<endl; *pointer=20; cout<<"x="<<x<<endl; cout<<"*pointer="<<*pointer<<endl; } 输出结果为: x=10 *pointer=10 x=20 *pointer=20 2



![字符数组的指针变量 可以定义一个指针变量存放字符数组的地址 程序 3 4 includeiostream h void main char str30This is a 字符数组的指针变量 可以定义一个指针变量,存放字符数组的地址。 程序 3. 4 #include<iostream. h> void main( ) {char str[30]="This is a](https://slidetodoc.com/presentation_image_h/2b67c68780d3212fc32937119556caa0/image-6.jpg)
字符数组的指针变量 可以定义一个指针变量,存放字符数组的地址。 程序 3. 4 #include<iostream. h> void main( ) {char str[30]="This is a book. "; char *charpoint; //charpoint是指向字符数组的指针变量 charpoint=str; 输出结果为: cout<<charpoint<<endl; This is a book. cout<<charpoint[0]<<charpoint[1]<<endl; cout<<*(charpoint)<<*(charpoint+1)<<endl; Th Th } 6

![程序 3 5 includestring h includeiostream h void main char str20abcde 输出结果为 The 程序 3. 5 #include<string. h> #include<iostream. h> void main() { char str[20]="abcde"; 输出结果为: The](https://slidetodoc.com/presentation_image_h/2b67c68780d3212fc32937119556caa0/image-8.jpg)
程序 3. 5 #include<string. h> #include<iostream. h> void main() { char str[20]="abcde"; 输出结果为: The length is 5 "include , The length is 20 The length is 5 cout<<"The length is "<<strlen(str)<<endl; cout<<"include , The length is"<<sizeof(str)<<endl; char *p=str; cout<<"The length is "<<strlen(p)<<endl; } 8

![程序 3 6 includestring h includeiostream h void main char str 120 str 220 程序 3. 6 #include<string. h> #include<iostream. h> void main() {char str 1[20], str 2[20],](https://slidetodoc.com/presentation_image_h/2b67c68780d3212fc32937119556caa0/image-10.jpg)
程序 3. 6 #include<string. h> #include<iostream. h> void main() {char str 1[20], str 2[20], str 3[20]="abcde"; strcpy(str 1, "Monday"); strcpy(str 2, str 1); 输出结果为: strcpy(str 3, "ABCDE"); Monday cout<<str 1<<endl; Monday cout<<str 2<<endl; cout<<str 3<<endl; ABCDE } 10
![程序 3 7 includestring h includeiostream h void main char str 120abcd str 24012345 程序 3. 7 #include<string. h> #include<iostream. h> void main() {char str 1[20]="abcd", str 2[40]="12345";](https://slidetodoc.com/presentation_image_h/2b67c68780d3212fc32937119556caa0/image-11.jpg)
程序 3. 7 #include<string. h> #include<iostream. h> void main() {char str 1[20]="abcd", str 2[40]="12345"; strcat(str 1, "efg"); strcat(str 2, str 1); cout<<str 1<<endl; 输出结果为: abcdefg 12345 abcdefg cout<<str 2<<endl; } 11
![程序 3 8 includestring h includeiostream h void main char str 120bag str 240big 程序 3. 8 #include<string. h> #include<iostream. h> void main() {char str 1[20]="bag", str 2[40]="big";](https://slidetodoc.com/presentation_image_h/2b67c68780d3212fc32937119556caa0/image-12.jpg)
程序 3. 8 #include<string. h> #include<iostream. h> void main() {char str 1[20]="bag", str 2[40]="big"; 输出结果为: -1 1 0 cout<<strcmp(str 1, str 2)<<endl; cout<<strcmp(str 2, str 1)<<endl; cout<<strcmp("red", "red")<<endl; } 12

const关键字用于定义有名的常量。 程序 3. 9 用const定义常量 #include<iostream. h> void main() { const int size=10; char str[size]; cout<<"Thesize of str is "<<sizeof(str)<<endl; size++; //错误语句 } 13


程序 3. 10 定义和使用引用举例 void main( ) { int a=9; 程序运行结果: int &b=a; a=9 cout<<"Here a="<<a<<"n"; cout<<"Here &b="<<&b<<"n"; a++; cout<<"Here a="<<a<<"n"; Here &b=9 a=10 Here &b=10 cout<<"Here &b="<<&b<<"n"; } 15


![程序 3 11 定义和使用结构体 struct student char number10 char name10 char sex int 程序 3. 11 定义和使用结构体 struct student { char number[10]; char name[10]; char sex; int](https://slidetodoc.com/presentation_image_h/2b67c68780d3212fc32937119556caa0/image-18.jpg)
程序 3. 11 定义和使用结构体 struct student { char number[10]; char name[10]; char sex; int age; }; void main() {student 1; cout<<“输入学号”; cin>>student 1. number; //为成员输入数据 cout<<"输入姓名"; cin>>student 1. name; cout<<"输入性别"; cin>>student 1. sex; cout<<"输入年龄"; cin>>student 1. age; cout<<"该学生的情况如下:"<<endl; cout<<student 1. number<<endl; //输出成员数据 cout<<student 1. sex<<endl; cout<<student 1. name<<endl; cout<<student 1. age<<endl; } 18
![结构体数组 v数组的每个元素是结构体类型这样的数组称为结构体数组 v访问结构体数组的格式数组名下标 成员名 程序 3 12 结构体数组 struct student char number10 char 结构体数组 v数组的每个元素是结构体类型,这样的数组称为结构体数组。 v访问结构体数组的格式:数组名[下标]. 成员名 程序 3. 12 结构体数组 struct student { char number[10]; char](https://slidetodoc.com/presentation_image_h/2b67c68780d3212fc32937119556caa0/image-19.jpg)
结构体数组 v数组的每个元素是结构体类型,这样的数组称为结构体数组。 v访问结构体数组的格式:数组名[下标]. 成员名 程序 3. 12 结构体数组 struct student { char number[10]; char name[10]; char sex; int age; } void main() { student 1[3]; int k; for(k=0; k<3; k++) { cin>>student 1[k]. number; cin>>student 1[k]. name; cin>>student 1[k]. sex; cin>>student 1[k]. age; } for(k=0; k<3; k++) {cout<<endl; cout<<student 1[k]. number<<endl; cout<<student 1[k]. name<<endl; cout<<student 1[k]. sex<<endl; cout<<student 1[k]. age<<endl; } } 19

![结构体中包括函数 在C中结构体可以包括数据成员和函数 程序 3 14 struct student char number10 char name10 char sex 结构体中包括函数 在C++中,结构体可以包括数据成员和函数。 程序 3. 14 struct student { char number[10]; char name[10]; char sex;](https://slidetodoc.com/presentation_image_h/2b67c68780d3212fc32937119556caa0/image-21.jpg)
结构体中包括函数 在C++中,结构体可以包括数据成员和函数。 程序 3. 14 struct student { char number[10]; char name[10]; char sex; int age; }; void output() //函数,用于输出数据 {cout<<"该学生的情况如下:"<<endl; cout<<number<<endl; cout<<name<<endl; cout<<sex<<endl; cout<<age<<endl; }}; void main() {student 1; cout<<"输入学号"; cin>>student 1. number; cout<<"输入姓名"; cin>>student 1. name; cout<<"输入性别"; cin>>student 1. sex; cout<<"输入年龄"; cin>>student 1. age; student 1. output(); } 21



联合体中包括函数 C++可以将函数加到联合体。使联合体可以包括数据成员和操作。 程序 3. 10 union abc {char a; int b; long c; void output() { cout<<a<<"n"; cout<<b<<"n"; cout<<c<<"n"; } }; void main() { union abc x; x. c=0 x 41424344; x. output(); } 输出结果: 41424344 D 24
Public void drawsquare(int x, int y, int len)
Int sum(int a int n) int sum=0 i
Void main int main
Void f(int i) int j=0
Voidswap
Divideint
Int max(int x int y)
Interface calculator public int add(int a int b) class test
Void main(void)
Void main(void)
Medicis brough
저장 유형 지정자
Void main() int i j charval='j'
Void main() int i j charval='j'
쉽게 풀어 쓴 c언어 express 도전문제
#include stdio.h int main(void)
Public static void main int args
Int main(int argc, char** argv)
Nnxn com
Int main() int num=4
Int argc char argv
Using namespace std int main()
Void setup