#include <stdio.h> //正确函数 void swap(int *p1,int *p2) { int temp; temp = *p1; *p1 = *p2; *p2 = temp; } //错误函数,单纯地址交换,在形参中指针的内容交换 //执行完后释放掉,主调函数中的变量没有发生任何变化 void swap1(int *p1,int *p2){ int *temp; temp = p1; p1 = p2; p2 = temp; } void main(){ int a=2; int b=3; printf("交换前两数的值为:%d %d\n",a,b); swap(&a , &b); printf("交换后两数的值为:%d %d\n",a,b); }
#include <stdio.h> void nixu(int arr[],int n){ int *p= arr,*q= arr+(n-1),temp; while( p < q ){ temp = *p; *p = *q; *q = temp; p++; q--; printf("p = %p\t",p-1); printf("q = %p\n",q+1); } printf("\n交换后数组内容与地址*****************\n"); } void main(){ int i,arry[11]={1,2,3,4,5,6,7,8,9,10,10}; int *p = arry; //指针变量初始化 printf("p = %p\n",p); //输出指针变量的内容 printf("&p = %p\n",&p); //输出指针变量的地址 printf("arry = %p\n",arry); //输出数组首地址 printf("&arry[0] = %p\n",&arry[0]);//输出数组首个元素的地址 printf("\n交换前数组内容与地址*****************\n"); for(i=0;i<10;i++){ printf("a[%d] = %d\t",i,*(p++)); //输出 *p所指向的内容,然后p=p+1 printf("p = %p\t",p-1); //输出 p中存储的内容 printf("&a[%d] = %p\n",i,&arry[i]); //每个数组元素的地址 } printf("\n函数形参地址变化*****************\n"); nixu(arry,10); p=arry; for(i=0;i<10;i++){ printf("a[%d] = %d\t",i,*(p++)); printf("p = %p\t",p-1); printf("&a[%d] = %p\n",i,&arry[i]); //每个数组元素的地址 } printf("\n"); }
/* 排序的方法:冒泡排序,选择排序 字符串比较函数:调用<stdlib.h> strcmp(str1,str2) strcmp(str1,str2) < 0 ----> str1 > str2 = 0 ----> str1 = str2 > 0 ----> str1 < str2 */ #include <stdio.h> #include <stdlib.h> #include <string.h> int main(){ int i,j; char *temp; char *ch[]={"CHINA","AMERCIAN","AUSTIALIA","RUSSIA"}; printf("strlen(ch) = %ld\n",strlen(*ch)); printf("%s,%s,%s,%s\n",ch[0],ch[1],ch[2],ch[3]); for(i=0;i<2;i++){ for(j=0;j<3;j++){ if(strcmp(ch[j],ch[j+1]) > 0){ temp = ch[j]; ch[j] = ch[j+1]; ch[j+1] = temp; } } } printf("%s,%s,%s,%s\n",ch[0],ch[1],ch[2],ch[3]); return 0; }

1万+

被折叠的 条评论
为什么被折叠?



