c语言指针

C语言用三种方式打印数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include
void pointarray2(int a[]){//用指针变量指向数组元素
int *point;
int i;
for(i=0;i<10;i++){
point=&a[i];
printf("%d",*point);
}
printf("\n");
}
void pointarray(int a[]){//通过数组名计算数组的元素地址,找出元素的值
int *point;
int i;
point=a;
for(i=0;i<10;i++){
printf("%d",*(point+i));
}
printf("\n");
}

int main(){
int a[10]={9,8,7,6,5,4,3,2,1,0};
int i;
for(i=0;i<10;i++){//下标法
printf("%d",a[i]);
}
printf("\n");
pointarray(a);
pointarray2(a);
return 0;
}

实现数组倒序存储

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include
void swap(int *p1,int *p2){
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
void reverse(int a[],int n){
int i;
int *low,*high;
for(i=0;i<=(n-1)/2;i++){
low=&a[i];
high=&a[n-1-i];//n-1-i指向对应的元素
swap(low,high);
}
}

int main(){
int a[10]={3,4,6,2,3,1,4,5,6,0};
int n=10,i;
for(i=0;i
printf("%d",a[i]);
}
printf("\n");
printf("reverse later---------\n");
reverse(a,10);//将数组倒叙排放
for(i=0;i
printf("%d",a[i]);
}
printf("\n");
}

max min search

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include
int *max,*min;
void Max_Min_Search(int *a,int n){
int i=0;
max=a;
min=a;
for(;i
if(a[i]>*max){
max=&a[i];
}
if(a[i]<*min){
min=&a[i];
}
}
}
int main(){
int a[10]={24,10,23,5,2,540,26,30,34,35};

int i=0;
for(;i<10;i++){
printf("%d ",a[i]);
}

printf("\n");
printf("---------------Searching------------------\n");
Max_Min_Search(a,10);
printf("max=%d min=%d\n",*max,*min);
}

image-20191024163739066

打印二维数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include

void PrintTwoArray(int (*a)[4],int row,int len){//(*a)[4]必须要传递列值,数组指针变量
int i,j;
for(i=0;i
for(j=0;j
printf("%d ",*(*(a+i)+j));
}
printf("\n");
}
}

int main(){
int a[3][4]={5,6,7,8,9,3,0,4,5,6,7,3};
printf("a的地址:%d\n",a);
printf("a: %d\n",&a);
printf("a[0]:%d\n",a[0]);
printf("*(*(a+1)):%d\n",*(*(a+1)));
printf("*(*(a+1)+2):%d\n",*(*(a+1)+2));
printf("&a[1][0]:%d\n",&a[1][0]);
printf("--------------------\n");
PrintTwoArray(a,3,4);
}

copy字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include


void print_string(char *string){
printf("----------------string b is------\n");
for(;*string!='\0';string++){
printf("%c",*string);
}
printf("\n");
}
void copy_string5(char *a,char *b){
char *p;
p=b;
while(*p++=*a++){}
p=b;
print_string(p);
}


void copy_string4(char *a,char *b){
char *p;
p=b;
while(*a!='\0'){
*p++=*a++;
}
p=b;
print_string(p);
}

void copy_string3(char *a,char *b){
char *p;
p=b;
while((*p++=*a++)!='\0'){
}
p=b;
print_string(p);
}
void copy_string2(char *a,char *b){
char *p;
p=b;
while((*p=*a)!='\0'){
p++;
a++;
}
p=b;
print_string(p);

}
void copy_string1(char *a,char *b){
char *p1,*p2;
p2=b;
p1=a;
for(;*p1!='\0';p1++,p2++){
*p2=*p1;
}
*p2='\0';
p2=b;
printf("-------------------------\n");
print_string(p2);
}
int main(){
char string[]="hello world";
char *a="i love you",b[]="hello world ",*p1,*p2;//此处应该定义为char b[] 而不能用char *b;因为char *b放在了常量区,不能被修改.
char *s="hello ";
int i;
printf("%s\n",string);
printf("%s\n",s);

p1=a;
p2=b;
for(;*p1!='\0';p1++,p2++){
*p2=*p1;
}
*p2='\0';
printf("----------------string b is-------\n");
for(i=0;b[i]!='\0';i++){
printf("%c",b[i]);
}
printf("---------------------\n");
copy_string1(a,b);
copy_string2(a,b);
copy_string3(a,b);
copy_string4(a,b);
copy_string5(a,b);

}

image-20191025192914077

image-20191025200423203

image-20191025200537661

image-20191025200650572

image-20191025201016685

指针函数传递参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include

int add(int x,int y){
return x+y;
}
int min(int x,int y){
if(x>y){
return y;
}
return x;
}
int max(int x,int y){
if(x>y){
return x;
}else{
return y;
}
}
void prosess(int x,int y,int (*fun)()){///////////////////
printf("%d\n",fun(x,y));
}
int main(){
int min(int,int);
int max(int,int);
int add(int,int);
void prosess(int ,int ,int (*fun)());

int a,b;
scanf("%d %d",&a,&b);
printf("max=");
prosess(a,b,max);
printf("min=");
prosess(a,b,min);
printf("sum=");
prosess(a,b,add);
}

输入序号打印学生各科成绩

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include
int *search(int (*score)[4],int id){
return *(score+id);
}
int main(){
int score[][4]={12,3,34,54,56,25,63,90,12,57,63,27};
int id,i;
int *p;
scanf("%d",&id);
if(id-1>0&&id-1<3){
p=search(score,id-1);
for(i=0;i<4;i++){
printf("%d ",*(p+i));
}
printf("\n");
}

}

判断不及格的学生

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include
int *search(int (*score)[4],int id){
return *(score+id);
}

int print_score1(int (*score)[4],int i){
int j;
for(j=0;j<4;j++){
if(*(*(score+i)+j)<60){
return i+1;
}
}
return -1;
}

void print_score(int *p){
int i=0;
for(;i<4;i++){
printf("%d ",*(p+i));
}
printf("\n");
}

int main(){
int score[][4]={12,3,34,54,70,89,63,90,12,57,63,27};
int id,i;
int *p1;
scanf("%d",&id);
if(id-1>=0&&id-1<3){
p1=search(score,id-1);
print_score(p1);
}
for(i=0;i<3;i++){
if(print_score1(score,i)>0){
printf("%d号不及格 ",print_score1(score,i));
}else{
printf("及格 ");
}
}
}

image-20191026001020291

字符串排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include
#include
void sort(char *name[],int n){
int i,j;
char *p;
for(i=0;i-1;i++){
for(j=i+1;j
if(strcmp(name[i],name[j])>0){
p=name[i];
name[i]=name[j];
name[j]=p;
}
}
}
}
void print(char *name[] , int n){
int i;
for(i=0;i
printf("%s ",name[i]);
}
printf("\n");
}
int main(){//指针数组
char *name[]={"php","go","c++","java","c"};

int n=5;
sort(name,n);
print(name,n);

}

image-20191026004814972

1
2
3
4
5
6
7
8
char *name[]={"php","go","c++","java","c"};
char **p;
int i;
int n=5;
for(i=0;i
p=name+i;
printf("%5s",*p);
}

image-20191026005311004

image-20191026005650753

image-20191026010454116

image-20191026010537453

image-20191026010620952

1
2
3
4
5
6
7
void test(){
int name[]={1,2,4,53,3};
int *p1,*p2;
p1=&name[1];
p2=&name[4];
printf("%d",p2-p1);//3
}

image-20191026011538963

image-20191026011736366

image-20191026011836077

带参数的宏定义

image-20191027135958486

image-20191027140124000

1
2
3
4
5
6
7
8
9
#include

#define M(a,b) (a>b)?a:b

int main(){
int a,b;
scanf("%d %d",&a,&b);
printf("max=%d",M(a,b));
}

image-20191027140729793

image-20191027140848028

image-20191027141110205

image-20191027141521576

image-20191027142416728

image-20191027142443507

image-20191027143938237

结构体

image-20191027144701230

image-20191027150035130

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include
struct date{
int year;
int month;
int day;
};
struct{
int id;
char *name;
char gender;
int age;
float score;
char *addr;
struct date birthday;
}student1,student2={1,"hanmeimei",'m',45,22.2f,"hubei",2019,12,12};

int main(){
student1=student2;
printf("number=%d\nname=%s\ngender=%c\nage=%d\nscore=%f\naddr=%s\nbirthday=%d-%d-%d\n",student1.id,student1.name,student1.gender,student1.age,student1.score,student1.addr,student1.birthday.year,student1.birthday.month,student1.birthday.day);

}

实现投票

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include
#include
struct PushTicket{
char *name;
int ticket;
}*point,ticket[5]={{"hanmeimei",0},{"lilei",0},{"lili",0},{"c",0},{"java",0}};

void PrintTicket(struct PushTicket *point){
int i;
point=ticket;
printf("------------候选人的票-----------\n");
for(i=0;i<5;i++,point++){
printf("%s-----:%d\n",point->name,point->ticket);
}

}
void compare(char *input){
int i;
point=ticket;
for(i=0;i<5;i++,point++){
if(strcmp(input,point->name)==0){
(point->ticket)++;
}
}
}
int main(){
int i;
char input[20];
point=ticket;
printf("------------候选人-----------\n");
for(i=0;i<5;i++,point++){
printf("%s ",point->name);
}
printf("\n");
printf("---------------\n");
while(1){
printf("请输入候选人名称投票:");
scanf("%s",&input);
compare(input);
if(strcmp(input,"-1")==0){
break;
}


}
PrintTicket(point);
}

动态数组

image-20191027183935021

image-20191027184043371

image-20191027184110721

image-20191027184156656

链表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include
struct student{
int num;
float score;
struct student *next;
};
void PrintLink(struct student *p){
while(p!=NULL){
printf("num=%d,score=%f",p->num,p->score);
p=p->next;
}

}
int main(){
struct student a,b,c,*head;
a.num=1;
a.score=89.0f;
a.next=&b;
b.num=2;
b.score=90;
b.next=&c;
c.num=3;
c.score=100;
c.next=NULL;
head=&a;
PrintLink(head);
}

image-20191027223049024

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include
#include

#define LEN sizeof(struct student)
struct student{//定义链表
int num;
float score;
struct student *next;
};
void print(struct student *head){//打印链表
while(head!=NULL){
printf("num=%d,score=%f\n",head->num,head->score);
head=head->next;
}
}
struct student *DeleteNode(int input,struct student *head){
struct student *p1,*p2;
if(head==NULL){
printf("空链表\n");
}
p1=head;
while(p1->num!=input&&p1->next!=NULL){//当输入的shu和链表的书不相等时.
p2=p1;
p1=p1->next;
}
if(p1->num==input){
if(p1==head){
head=p1->next;
}else{
p2->next=p1->next;
}
}
return head;
}
struct student *insert(struct student *stu,struct student *head){//有bug
struct student *p1,*p2,*p0;
p0=stu;
if(head==NULL){
printf("NUll linktable\n");
}
p1=head;
while(p0->num>p1->num&&(p1->next!=NULL)){
p2=p1;
p1=p1->next;
if(p0->num<=p1->num){
if(head==p1){
head=p0;
}else{
p2->next=p0;
p0->next=p1;
}

}else{
p1->next=p0;
p0->next=NULL;
}

}
p1=head;
p2=p1;
return head;


}
struct student *create(){//创建链表
int n=0;
struct student *head,*p1,*p2;
p1=p2=(struct student *)malloc(LEN);
printf("input num-------------:");
scanf("%d",&p1->num);
printf("input score-----------:");
scanf("%f",&p1->score);
head=NULL;
while(p1->num!=0){
n++;
if(n==1){
head=p1;
}else{
p2->next=p1;
}


p2=p1;
p1=(struct student *)malloc(LEN);
printf("input num-------------:");
scanf("%d",&p1->num);
printf("input score-----------:");
scanf("%f",&p1->score);
}
p2->next=NULL;

return head;

}
int main(){
int input;
struct student *stu,*p,stu1;
stu=create();
p=stu;
print(p);
printf("\n");
printf("删除节点-----:");
scanf("%d",&input);
print(DeleteNode(input,p));
printf("\n");
printf("插入节点");
printf("input num-------------:");
scanf("%d",&stu1.num);
printf("input score-----------:");
scanf("%f",&stu1.score);
print(insert(&stu1,stu));
printf("\n");


printf("插入节点");
printf("input num-------------:");
scanf("%d",&stu1.num);
printf("input score-----------:");
scanf("%f",&stu1.score);
print(insert(&stu1,stu));
printf("\n");

}

image-20191028182743903

1
2
3
4
5
6
7
8
9
10
11
12
#include
#include

int main(){
FILE *file;
if(!(file=fopen("d:\\hello.txt","w"))){
printf("error");
}else{
printf("success!!!\n");

}
}

image-20191029131827023

image-20191029142743283

image-20191029144251829

image-20191029144350161

image-20191029144636084

image-20191029144811799

image-20191029144847446

用位运算实现大小写转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include


int main(){
char ch;
printf("please input char:");
ch=getchar();
while(!(ch>'A'&&ch<'z')||(ch>'Z'&&ch<'a')){
printf("error please input:\n");
ch=getchar();
}
if(ch & 32){//32的二进制位是100000 ch&100000得到第五位数是0,然后就换转大写
ch=ch&223;
}else{

ch=ch|255;//得到第五位数是1 转小写.
}
putchar(ch);

ch=getchar();//打印回车
putchar(ch);


}

image-20191029151159306

image-20191029151309955

image-20191029151500290

image-20191029151813040

image-20191029152251623

image-20191029152632086

image-20191029152726176

image-20191029153550877

struct与malloc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include
#include
#define LEN sizeof(struct student)
struct student{
int id;
char *name;
int score;
};

void fun(struct student *p){
(*p).id=12;
p->name="hello";
p->score=90;

}
void show(struct student *p){
printf("%d %s %d\n",p->id,p->name,p->score);
}

int main(){
struct student *stu;
stu=(struct student *)malloc(LEN);
fun(stu);
//printf("%d %s %d",stu.id,stu.name,stu.score);
show(stu);
}

动态数组的增删排

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include
#include
#include
struct Arr{
int *pBase;//数组的首地址
int len;//数组的长度
int cnt;//当前有效元素的个数
};


void init_arr(struct Arr *,int length);

int append_arr(struct Arr *,int val);//追加

int insert_arr(struct Arr *,int pos,int val);

int delete_arr(struct Arr *,int pos,int *val);

//int get();

int is_empty(struct Arr *p);

int is_full(struct Arr *p);

int sort_arr(struct Arr *p);

void show_arr(struct Arr *p);

int reverse_arr(struct Arr *p);

int main(){
struct Arr arr;
int val;
init_arr(&arr,6);
append_arr(&arr,100);
append_arr(&arr,99);
append_arr(&arr,80);
append_arr(&arr,23);
if(append_arr(&arr,45)){
printf("append success\n");
}
insert_arr(&arr,7,555);
if(append_arr(&arr,567)){
printf("append success\n");
}

if(append_arr(&arr,1000)==1){
printf("append success\n");
}
printf("%d\n",arr.cnt);
show_arr(&arr);
printf("----------删除元素-------\n");
delete_arr(&arr,3,&val);
printf("--------删除的元素是:%d\n",val);
show_arr(&arr);
printf("----------倒置元素--------\n");
reverse_arr(&arr);
show_arr(&arr);
printf("----------排序-------------\n");
sort_arr(&arr);
show_arr(&arr);
printf("%d\n",arr.cnt);


}
void init_arr(struct Arr *p,int length){
p->pBase=(int *)malloc(sizeof(int)*length);
if(NULL==p->pBase){
printf("error\n");
exit(-1);
}
p->len=length;
p->cnt=0;

return;
}
int is_empty(struct Arr *p){
if(p->cnt==0){
return 1;
}
return 0;

}
void show_arr(struct Arr *p){
int i;
if(is_empty(p)==1){
printf("数组为空\n");
}
for(i=0;icnt;i++){
printf("%d ",*(p->pBase+i));
}
printf("\n");
}

int is_full(struct Arr *p){
if(p->cnt==p->len){
return 1;
}
return 0;
}
int append_arr(struct Arr *p,int val){
if(is_full(p)==1){
printf("数组已满\n");
return 0;
}
//不满时
p->pBase[p->cnt]=val;
(p->cnt)++;
return 1;
}
int insert_arr(struct Arr *p,int pos,int val){
int i=(p->cnt)-1;
if(pos<1||pos>(p->cnt)+1){//<1或者大于当前有效元素
return 0;
}
if(is_full(p)==1){
printf("数组已满\n");
return 0;
}
//pos的值从1开始,且从pos的位置之前插入
for(;i>=pos-1;i--){
p->pBase[i+1]=p->pBase[i];
}
p->pBase[pos-1]=val;
(p->cnt)++;
return 1;

}
int delete_arr(struct Arr *p,int pos,int *val){
int i;
if(is_empty(p)==1){
printf("数组为空\n");
return 0;
}
if(pos<1||pos>p->cnt){
return 0;
}
*val=p->pBase[pos-1];
for(i=pos;i<=p->cnt;i++){
p->pBase[i-1]=p->pBase[i];
}
(p->cnt)--;
return 1;
}
int reverse_arr(struct Arr *p){
int i=0,temp,j=p->cnt-1;
//int *low,*high;
if(is_empty(p)==1){
printf("数组为空\n");
return 0;
}
while(i
temp=p->pBase[i];
p->pBase[i]=p->pBase[j];
p->pBase[j]=temp;
i++;
j--;
}

/*low=&(p->pBase[0]);
high=&(p->pBase[p->cnt]);
for(i=0;i<=(high-low)/4;i++){
low=&(p->pBase[i]);
high=&(p->pBase[p->cnt-1-i]);
temp=*low;
*low=*high;
*high=temp;
}*/
return 1;

}
int sort_arr(struct Arr *p){
int i,min,j,temp;
for(j=0;jcnt;j++){
min=p->pBase[j];
for(i=j+1;icnt;i++){
if(p->pBase[i]<min){
temp=p->pBase[i];
p->pBase[i]=min;
min=temp;
}
}
p->pBase[j]=min;
}

return 1;
}

本文标题:c语言指针

文章作者:philxling

发布时间:2020年04月04日 - 05:41

最后更新:2020年10月15日 - 12:26

原始链接:http://philxling.club/2020/04/04/c++/1-c%E8%AF%AD%E8%A8%80%E6%8C%87%E9%92%88/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

philxling wechat
ex. subscribe to my blog by scanning my public wechat account
-------------本文结束感谢您的阅读-------------
谢谢你给我糖吃!!!
0%