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

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

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值