前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >指针数组与指向指针的指针

指针数组与指向指针的指针

作者头像
阳光岛主
发布2019-02-19 17:43:46
1.8K0
发布2019-02-19 17:43:46
举报
文章被收录于专栏:米扑专栏米扑专栏

指针数组与指向指针的指针

http://wlkc.gdqy.edu.cn/jpkc/portal/blob?key=173314

指针数组和数组指针的区别

http://allew.blog.163.com/blog/static/3374389720094148449239/

指针数组[组图]

http://school.cnd8.com/c/jiaocheng/9212.htm

函数指针和指针函数

http://lionwq.spaces.eepw.com.cn/articles/article/item/18258

========================================

source 1

#include <stdio.h> int main(void) { char *ptr[4] = {"beijing", "shanghai", "tianjin", "chongqing"}; int i, *iptr[3], a[3]={1,2,3},b[3][2]={1,2,3,4,5,6}; for(i=0; i<4; i++) printf("%s/t", ptr[i]); printf("/n"); for(i=0; i<3; i++) iptr[i]=&a[i]; for(i=0; i<3; i++) printf("%d/t", *iptr[i]); printf("/n"); for(i=0; i<3; i++) iptr[i]=b[i]; for(i=0; i<3; i++) printf("%d:%d/t", *iptr[i], *iptr[i]+1); printf("/n"); return 0; }

result 1:

[work@db-testing-com06-vm3.db01.baidu.com c++]$ gcc -o array_ptr array_ptr.c 

[work@db-testing-com06-vm3.db01.baidu.com c++]$ ./array_ptr 

beijing shanghai        tianjin chongqing

1       2       3

1:2     3:4     5:6

========================

source 2

#include <stdio.h> #include <string.h> #include <stdlib.h> void show(char *ptr[], int); void sort(char *ptr[], int); int main(void) { char *ptr[4], *tmp; int i; for(i=0; i<4; i++) { ptr[i] = (char *)malloc(20); fgets(ptr[i], 20, stdin); } show(ptr, 4); sort(ptr, 4); show(ptr, 4); return 0; } void show(char *ptr[], int num) { int i; for(i=0; i<num; i++) printf("%10s", ptr[i]); printf("/n"); //换行 } void sort(char *ptr[], int num) { int i, j; char *tmp; for(i=0; i<num-1; i++) for(j=0; j<num-1-i; j++) { if(strcmp(ptr[j], ptr[j+1])>0) { tmp = ptr[j]; ptr[j]=ptr[j+1]; ptr[j+1]=tmp; } } }

result 2:

[work@db-testing-com06-vm3.db01.baidu.com c++]$ ./array_ptr_sort 

beijing

shanghai

tianjin

chongqing

  beijing

 shanghai

  tianjin

chongqing

  beijing

chongqing

 shanghai

  tianjin

=================================

指针函数

Source

#include <stdio.h> int main() { char *ch(char *, char *); char str1[] = "I am glad to meet you"; char str2[] = "Welcome to study C"; printf("%s/n", ch(str1, str2)); return 0; } char *ch(char *str1, char *str2) { int i; char *p = str2; //指针赋值 printf("str1: %s/nstr2: %s/n", str1, str2); return p; //返回指针 }

Result

[work@db-testing-com06-vm3.db01.baidu.com c++]$ gcc -o func_pfun func_pfun.c 

[work@db-testing-com06-vm3.db01.baidu.com c++]$ ./func_pfun                  

str1: I am glad to meet you

str2: Welcome to study C

Welcome to study C

=================================

函数指针

Source

#include <stdio.h> int main() { int max(int, int); int i, a, b, m; int (*f)(); //定义函数指针 scanf("%d %d", &a, &b); f = max; //给函数指针f赋值,使它指向函数max m = (*f)(a, b); //通过函数指针f调用函数max printf("%d %d max: %d/n", a, b, m); return 0; } int max(int x, int y) { return x>y ? x:y; }

Result

work@db-testing-com06-vm3.db01.baidu.com c++]$ gcc -o func_ptr func_ptr.c   

[work@db-testing-com06-vm3.db01.baidu.com c++]$ ./func_ptr 

3 6

3 6 max: 6

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2010年11月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档