Chinaunix首页 | 论坛 | 博客
  • 博客访问: 288928
  • 博文数量: 134
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 118
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-01 14:02
文章分类

全部博文(134)

文章存档

2015年(2)

2014年(4)

2013年(128)

分类: C/C++

2013-08-01 14:13:18

原文地址:指针基础 _0214 作者:丫叩酱


点击(此处)折叠或打开

  1. 1.#验证指针存的是地址
  2. #include <stdio.h>

  3. int main(int argc, const char *argv[])
  4. {
  5.     int a,*p;
  6.     p=&a;
  7.     printf("%p\n",p);
  8.     printf("%p\n",&a);
  9.     return 0;
  10. }
  11. 2.#没对指针赋值时,不能引用指针的值
  12. #include <stdio.h>

  13. int main(int argc, const char *argv[])
  14. {
  15.     int *p;
  16.     printf("%d\n",*p);
  17.     return 0;
  18. }
  19. 3.#指针做参数
  20. #include <stdio.h>
  21. void swap(int *a,int *b)
  22. {
  23.     int tmp;
  24.     tmp=*a;
  25.     *a=*b;
  26.     *b=tmp;
  27. }

  28. int main(int argc, const char *argv[])
  29. {
  30.     int a=3,b=4;
  31.     swap(&a,&b);
  32.     printf("a=%d b=%d\n",a,b);
  33.     return 0;
  34. }
  35. 4.#返回值为指针型
  36. #include <stdio.h>

  37. int *max(int *a,int *b)
  38. {
  39.     return *a>*b? a:b;
  40. }

  41. int main(int argc, const char *argv[])
  42. {
  43.     int a=5,b=3;
  44.     printf("max=%d\n",*max(&a,&b));
  45.     return 0;
  46. }
  47. 5.#永远不要返回指向局部变量的地址
  48. #include <stdio.h>

  49. int *func(void)
  50. {
  51.     int i=10;
  52.     return &i;
  53. }

  54. int main(int argc, const char *argv[])
  55. {
  56.     int *p;
  57.     p=func();
  58.     printf("hello!\n");
  59.     printf("%d\n",*p);
  60.     return 0;
  61. }
  62. 6.#用指针输出数组的值
  63. #include <stdio.h>

  64. int main(int argc, const char *argv[])
  65. {
  66.     int a[10]={1,2,3,4,5,6,7,8,9,10};
  67.     int i,*p;
  68.     p=a;
  69.     for (i = 0; i < 10; i++)
  70.     {
  71.         printf("%4d",*(p+i));
  72.     }
  73.     printf("\n");
  74.     return 0;
  75. }
  76. 7.#指针的算术运算,数组名为常量,不能做加减运算
  77. #include <stdio.h>

  78. int main(int argc, const char *argv[])
  79. {
  80.     int a[10]={1,2,3,4,5,6,7,8,9,10};
  81.     int *p=a;
  82.     printf("%d\n",*p++);
  83.     printf("%d\n",*++p);
  84.     //printf("%d\n",*a++);
  85.     int *q;
  86.     p=&a[5];
  87.     q=&a[1];
  88.     printf("%d\n",p-q);
  89.     printf("%d\n",&a[5]-&a[1]);
  90.     printf("%d\n",(int)&a[5]-(int)&a[1]);
  91.     return 0;
  92. }
  93. 8.#*p++(*p)++,一个是地址加,一个是数值加
  94.   #++的优先级等于*的优先级,由左向右运算,先是左结合,再是右结合。
  95. #include <stdio.h>

  96. int main(int argc, const char *argv[])
  97. {
  98.     int a[10]={1,20,30};
  99.     int *p=a;
  100.     printf("%d\n",*p++);
  101.     printf("%d\n",(*p)++);
  102.     printf("%d\n",*p);
  103.     return 0;
  104. }
  105. 9.#用指针实现puts函数,"hello"指的是'h'的地址(只读的),str[]="hello"可读可写
  106. #include <stdio.h>

  107. void myputs(char *s)
  108. {
  109.     while(*s)
  110.         putchar(*s++);
  111.     printf("\n");
  112. }

  113. int main(int argc, const char *argv[])
  114. {
  115.     char *s="hello world!";
  116.     char str[20]="hello world!";
  117.     myputs(s);
  118.     return 0;
  119. }
  120. 10.#所有的指针都为4个字节
  121. #include <stdio.h>

  122. int main(int argc, const char *argv[])
  123. {
  124.     char *arrc[3];
  125.     int arri[3];
  126.     printf("%4d%4d\n",sizeof(arrc),sizeof(arri));
  127.     return 0;
  128. }
  129. 11.#指针数组,数组的元素均为指针
  130.    #int *arr[5];
  131. #include <stdio.h>

  132. int main(int argc, const char *argv[])
  133. {
  134.     int i;
  135.     char *str[3]={"hello","world","akaedu"};
  136.     puts(str[0]);
  137.     puts(str[1]);
  138.     puts(str[2]);
  139.     for (i = 0; i < 3; i++)
  140.     {
  141.         printf("%s\n",str[i]);
  142.     }

  143.     return 0;
  144. }
  145. 12.#输出该函数的各参数
  146. #include <stdio.h>

  147. int main(int argc, const char *argv[])
  148. {
  149.     int i;
  150.     for (i = 0; i < argc; i++)
  151.     {
  152.         puts(argv[i]);
  153.     }
  154.     return 0;
  155. }
  156. 13.#数组指针,指向数组的指针
  157.    #int (*arr)[5];
  158. #include <stdio.h>

  159. int main(int argc, const char *argv[])
  160. {
  161.   int a[4][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20}};
  162.   int (*p)[5];
  163.   p=a;
  164.   p[0]
  165.     return 0;
  166. }
  167. 14.#函数指针,指向函数名(函数入口地址)的指针
  168.    #int (*p)(int,int);
  169. #include <stdio.h>

  170. void print(void)
  171. {
  172.     printf("hello!\n");
  173. }

  174. int add(int a,int b)
  175. {
  176.     return a+b;
  177. }

  178. int main(int argc, const char *argv[])
  179. {
  180.    
  181.     void (*p)(void);
  182.     int (*q)(int,int);
  183.     p=print;
  184.     q=add;
  185.     p();
  186.     printf("%d\n",q(3,4));
  187.     return 0;
  188. }
  189. 15.#指针数组函数
  190. #include <stdio.h>
  191. #include <string.h>

  192. int add(int a,int b)
  193. {
  194.     return a+b;
  195. }

  196. int sub(int a,int b)
  197. {
  198.     return a-b;
  199. }

  200. int mul(int a,int b)
  201. {
  202.     return a*b;
  203. }

  204. int div(int a,int b)
  205. {
  206.     if(b==0)
  207.         return 0;
  208.     return a/b;
  209. }

  210. int (*fun[])(int,int)={add,sub,mul,div};

  211. int main(int argc, const char *argv[])
  212. {
  213.     int a,b,result,i;
  214.     char ch;
  215.     char *s="+-*/";
  216.     scanf("%d%c%d",&a,&ch,&b);
  217.     
  218.     for (i = 0; i < strlen(s); i++)
  219.     {
  220.         if(ch==s[i])
  221.             result=fun[i](a,b);
  222.     }
  223.     printf("%d %c %d = %d\n",a,ch,b,result);
  224.     return 0;
  225. }
  226. 16.#使用指针改写求一个数组中的最大值
  227.    #使用指针改写将一数列反向
  228. #include <stdio.h>

  229. int *max(int *p,int n)
  230. {
  231.     int i,*max;
  232.     max=p;
  233.     for (i = 0; i < n; i++)
  234.     {
  235.         if(*max<*(p+i))
  236.         {
  237.             max=p+i;
  238.         }
  239.     }
  240.     return max;

  241. }
  242. void reverse(int *a,int n)
  243. {
  244.     int *p=a;
  245.     int *q=a+n-1;
  246.     int tmp;
  247.     while(p<q)
  248.     {
  249.         tmp=*p;
  250.         *p=*q;
  251.         *q=tmp;
  252.     }
  253. }

  254. int main(int argc, const char *argv[])
  255. {
  256.     int i,a[10]={1,2,3,4,5,6,7,8,9,10};
  257.     printf("*a[%d]=%d\n",max(a,10)-a,*max(a,10));
  258.     printf("Before:");
  259.     for (i = 0; i < 10; i++)
  260.     {
  261.         printf("%4d",a[i]);
  262.     }
  263.     printf("\nAfter :");
  264.     reverse(a,10);
  265.     for (i = 0; i < 10; i++)
  266.     {
  267.         printf("%4d",a[i]);
  268.     }
  269.     printf("\n");
  270.     return 0;
  271. }
  272. 17.#用指针实现strlen函数
  273. #include <stdio.h>
  274. int mystrlen(char *s)
  275. {
  276.     int n=0;
  277.     while(*s)
  278.     {
  279.         s++;
  280.         n++;
  281.     }
  282.     return n;
  283. }

  284. int main(int argc, const char *argv[])
  285. {
  286.     char str[20];
  287.     printf("str=");
  288.     scanf("%s",str);
  289.     printf("strlen=%d\n",mystrlen(str));
  290.     return 0;
  291. }
  292. 18.编写一程序,输入月份号,输出该月的英文名字,例如:输入“3”,则输出“March”,要求用指针数组实现
  293. #include <stdio.h>

  294. char *p[12]={"Janury","February","March","April","May","June","July",
  295.                  "August","Septemble","October","November","Decemble"};
  296. int main(int argc, const char *argv[])
  297. {
  298.     int m;
  299.     printf("month=");
  300.     scanf("%d",&m);
  301.     printf("%s\n",p+m-1);
  302.     return 0;
  303. }
  304. 19.#学习使用并实现atoi函数
  305. #include <stdio.h>

  306. int myatoi(char *s)
  307. {
  308.     int i,flag=0,num=0,cnt=0;
  309.     char *p;
  310.     p=s;
  311.     while(*s)
  312.     {
  313.         cnt++;
  314.         s++;
  315.     }
  316.     s=p;
  317.     if(s[0]=='-')
  318.         flag=1;
  319.     for (i = 0+flag; i < cnt; i++)
  320.     {
  321.         num=num*10+(s[i]-'0');
  322.     }
  323.     if(flag==1)
  324.         num*=-1;
  325.     return num;

  326. }

  327. int main(int argc, const char *argv[])
  328. {
  329.     char str[10];
  330.     printf("str=");
  331.     scanf("%s",str);
  332.     printf("toi=%d\n",myatoi(str));
  333.     return 0;
  334. }
  335. 20.#学习使用strcmp函数
  336. #include <stdio.h>

  337. int mystrcmp(char *s1,char *s2)
  338. {
  339.     while(*s1==*s2)
  340.     {
  341.         if(*s1='\0')
  342.             return 0;
  343.         s1++;
  344.         s2++;
  345.     }
  346.     return *s1>*s2?1:-1;
  347. }

  348. int main(int argc, const char *argv[])
  349. {
  350.     char str1[20],str2[20];
  351.     printf("str1=");
  352.     scanf("%s",str1);
  353.     printf("str2=");
  354.     scanf("%s",str2);
  355.     printf("%d\n",mystrcmp(str1,str2));
  356.     return 0;
  357. }
  358. 21.#学习并实现strcpy
  359. #include <stdio.h>

  360. char *mystrcpy(char *dd,char *ss)
  361. {
  362.     int i=0;
  363.     while(*(ss+i))
  364.     {
  365.         *(dd+i)=*(ss+i);
  366.         i++;
  367.     }
  368.     *(dd+i)='\0';
  369.     return dd;
  370. }

  371. int main(int argc, const char *argv[])
  372. {
  373.     char d[100],s[32];
  374.     printf("d=");
  375.     scanf("%s",d);
  376.     printf("s=");
  377.     scanf("%s",s);
  378.     printf("d=%s\n",mystrcpy(d,s));
  379.     return 0;
  380. }
  381. 22.#学习并实现strchr和strrchr
  382. #include <stdio.h>

  383. char *mystrchr(char *s,char c)
  384. {
  385.     while(*s)
  386.     {
  387.         if(c==*s)
  388.             return s;
  389.         s++;
  390.     }
  391.     return NULL;
  392. }

  393. char *mystrrchr(char *s,char c)
  394. {
  395.     char *bak=s;
  396.     while(*s)
  397.         s++;
  398.     while(--s>=bak)
  399.     {
  400.         if(*s==c)
  401.             return s;
  402.     }
  403.     return NULL;
  404. }

  405. int main(int argc, const char *argv[])
  406. {
  407.     char str[32]="hello world";
  408.     char ch='o';
  409.     printf("strchr=%s\n",mystrchr(str,ch));
  410.     printf("strrchr=%s\n",mystrrchr(str,ch));
  411.     return 0;
  412. }

阅读(254) | 评论(0) | 转发(0) |
0

上一篇:结构体 _0215

下一篇:函数 _0213

给主人留下些什么吧!~~