C语言程序开发宝典-指针

    xiaoxiao2021-03-25  62

    实例051使用指针实现数据交换; 实例054使用指针输出数组元素; for(p=a;p<(a+10);p++) { printf(“%d\n”,&p); }

    实例55:用指针实现逆序存放数组元素值;

      用指针实现逆序存放数组;不是很难,自己实现的话也是一个for循环就能解决问题;   照着敲一遍:

    #include <stdio.h> #include <conio.h> void inverte(int *x,int n) { int *p,*i,*j; int temp; int m=(n-1)/2; i=x; j=x+n-1; p=x+m; for(;i<=p;i++,j--) { temp = *i; *i = *j; *j =temp; } } void main() { int i,a[10]={1,2,3,4,5,6,7,8,9,0}; printf("The elements of original array:\n"); for(i=0;i<10;i++) printf("%d,",a[i]); printf("\n"); inverte(a,10); printf("The elements has been inverted:\n"); for(i=0;i<10;i++) printf("%d,",a[i]); printf("\n"); getch(); }

    就这么简答的代码还是碰见2个问题, 问题1:VS 不知道怎么搞的总是让想人用_getch,_fopen之类的东西,如下:

    警告 1 warning C4996: ‘getch’: The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details. e:\vs2010-c++\c语言程序开发范例宝典\test55\test55\test55\test.c 32 1 test55

    好吧,查了下,有人说直接无视,有人说加上#pragma warning(disable: 4996)屏蔽掉;我也不管了,懒得加_getch

    电脑上先是装了VS2010,然后后来又犯装了VS2015,大家有用VS2010的,有用VS2012的,还有用VS2015的,这么不统一,真是纠结,不过感觉VS2015还是比2010好用多了,多了好多封装的函数,使用更傻瓜式了。VS2010还总有定义了不能直接用的BUG。

    15版编辑直接过,再用10编辑死活就是编译不过:

    错误 2 error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 E:\VS2010-C++\C语言程序开发范例宝典\test55\test55\test55\LINK test55 is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details. e:\vs2010-c++\c语言程序开发范例宝典\consoleapplication55\consoleapplication55\main.c 31 1 ConsoleApplication55

    找了一下:原因就是因为装了15之后什么东西被改变了。 解决方案见:就是改了下工程属性->配置属性-> 清单工具->输入和输出->嵌入清单,选择[否]

    http://blog.csdn.net/zhouxicai/article/details/8365338

    实例056:输出二维数组 a[0]+1等价于*(a+0)+1等价于&a[0][1];

    实例057:指向一维数组的指针变量 int (*p)[4] p为二维数组中的行指针,p+i表示二维数组第i行的地址; p指向二维数组a的行地址 p=a;//p=&a[0]

    http://blog.csdn.net/lg2lh/article/details/7229980

    p是一个指针变量,它指向二维数组a 或指向第一个一维数组a[0],其值等于a,a[0],或&a[0][0]等。而p+i则指向一维数组a[i]。从前面的分析可得出(p+i)+j是二维数组i行j 列的元素的地址,而(*(p+i)+j)则是i行j列元素的值。

    实例058:用指针查找数列中的最大值最小值 void max_min(int a[],int n,int *max, int *min) { int *p; *max=*min=*a; for(p=a+1; p < a+n; p++) if( * p> *max) *max=*p; else if (*p < *min) *min= *p; return 0; } 实例059:用指针数组构造字符串数组

    实例060:strcmp(char *str1,char *str2)

    实例061:用指向函数的指针比较大小: int(*pmin)(); pmin=min;//min为一函数 m=(*pmin)(a,b);

    实例062:返回找到最大数的地址

    实例063:返回字符串中的位置;

    实例064:寻找指定元素的指针; //创建search(),查找指定值在数组中的位置 int search(int *pt,int n,int key) //创建find函数返回指定值的指针 int *find(int *pt,int n,int key) 实例065:在两个数组中寻找相同元素 返回有序数组中首个元素相同的地址;

    字符串与指针 实例066:使用指针实现字符串复制; 实例067:字符串的连接

    char* insert(char *s,char *q,int n) { int i=0; static char *str,strcp[50]; str=strcp; for(i=0; *s !='\0';i++){ if(i==n-1) for(;*q!='\0';){ str[i]=*q; /* printf("%c",str[i]);*/ q++; i++; } str[i]= *s; /*printf("%c",str[i]);*/ s++; } str[i]='\0'; return str; }

    实例069:字符串匹配:实现匹配2个字符串s与t,s 字符串是被匹配的,t是需要匹配的; char s[]="One world,one dream"; char t[]="world"; 思路:从字符串t的最后一个字符开始从字符s中寻找与之相同的字符,若找到则从字符串t的第一个字符开始依次比较字符t与字符s,j为匹配到相同字符的个数,若匹配到相同字符的个数与字符串t的长度相同,则返回匹配成功; 从t最后一个字符d开始匹配而不是从第一个字符w开始匹配,这样做的原因是可以减少循环次数! 以下为代码片段:

    ****************************/ int match(char *B,char *A) { int i,j,start=0; int lastB=strlen(B)-1;//长字符串,被匹配One world one dream int lastA=strlen(A)-1;//需要匹配的字符串 world int endmatch=lastA; for(j=0;endmatch<=lastB;endmatch++,start++) { printf("B[endmatch]=%c,A[lastA]=%c\n",B[endmatch],A[lastA]); if(B[endmatch]==A[lastA]) { printf("%c\n",B[endmatch]); putchar(B[endmatch]); printf("\n"); printf("用输出=%s\n",A); puts(A); for(j=0,i=start;j<lastA && B[i]==A[j];) { printf("B[i]=%c\t%c\n",B[i],A[j]); /* printf("B[i]=%c\n",A[j]);*/ i++,j++; } } if (j==lastA){ return (start+1); } } if(endmatch>lastB){ printf("The string is not matchable!"); return -1; } }

    指向指针的指针: 实例070:使用指针的指针输出字符串 指向指针数据的指针变量定义: char * * p等价于char * ( *p)表示p指向的是一个指针变量;

    int main(void) { char *strings[]={ "C language", "Basic", "World wide", "Olympic", "Great Wall"};//字符串数组 char **p;//指向指针的指针 int i; p=strings; for (i=0;i<5;i++) { printf("%s\n",*(p+i)); }

    实例071:同上

    实例072:使用指向指针的指针对字符串排序strcpm

    ———————–over————————

    转载请注明原文地址: https://ju.6miu.com/read-37133.html

    最新回复(0)