20150604高级指针及函数指针_练习

//

//  main.c

//  IOS150604_Exercise

//

//  Created by Peng Junlong on 15/6/4.

//  Copyright (c) 2015 Peng Junlong. All rights reserved.

//


//*************************

//*                       *

//*       指针练习题       *

//*                       *

//*************************


#include <stdio.h>


//实现一个函数,使用地址传递的方式,交换两个变量的值。并在函数内将交换后的值输出。

//比如:

//输入3 6

//输出为

//6 3


//void swap(int *a,int *b)

//{

//    int temp;

//    temp = *a;

//    *a = *b;

//    *b = temp;

//    

//}

//int main(int argc, const char * argv[]) {

//    int a = 100;

//    int b = 300;

//    swap(&a,&b);

//    printf("a = %d b = %d\n",a,b);

//    return 0;

//}



//实现一个函数,向函数传递一个数组,形参使用一个指针变量接收,利用指针变量将数组中的内容输出

//比如:

//输入3 6 3 5 7

//

//输出为

//3

//6

//3

//5

//7


//void printArray(int *parray,int len)

//{

//    for (int i=0; i<len; i++) {

//        printf("%d\n",*(parray+i));

//    }

//}

//

//int main(int arg,const char *argv[])

//{

//    int arra[5] = {23,4,5,6,99};

//    printArray(arra,5);

//    return 0;

//}


//实现一个函数,向函数中传递一个字符数组,形参使用一个指针变量接收,利用指针将数组中的小写字符转换成大写字符并输出

//比如:

//输入

//a e D r W

//输出为

//A

//E

//D

//R

//W


//void lowcaseToUpCase(char *pstr)

//{

//    int i=0;

//    while (pstr[i] != '\0') {

//        if (pstr[i] >= 'a' && pstr[i]<='z') {

//            pstr[i] = pstr[i]-('a'-'A');

//        }

//        i++;

//    }

//}

//

//int main(int arg ,const char *argv[])

//{

//    char str[20];

//    printf("请输入字符串:");

//    scanf("%s",str);

//    lowcaseToUpCase(str);

//    printf("\n");

//    int i=0;

//    while (str[i] != '\0') {

//        printf("%c\n",str[i]);

//        i++;

//    }

//    return 0;

//}



//定义一个整型指针变量,指向一个已经初始化的整型变量,利用指针变量将指向的变量的值改变,并将修改后的值进行输出。

//比如:

//输入5

//

//输出为

//5

//int main(int arg,const char *argv[])

//{

//    int a = 22;

//    int *p = &a;

//    printf("a的初值是%d\n",a);

//    printf("输入修改值为:");

//    scanf("%d",p);

//    printf("a修改后的值为%d\n",a);

//    

//    return 0;

//}


//定义一个有5个元素的整型数组a,并终端输入初始化值,再定义一个有5个元素的一维指针数组p

//并将p数组中的每个指针变量指向a数组中对应的元素值,然后剩用p数组将a数组中的值输出出来。

//比如:

//输入5 6 8 3 4

//

//输出为

//5

//6

//8

//3

//4


//int main(int arg ,const char *argv[])

//{

//    int a[5]={0};

//    int *p[5];

//    printf("输入5个整数:");

//    for (int i=0; i<5; i++) {

//        scanf("%d",&a[i]);

//        p[i] = &a[i];

//    }

//    

//    for(int i=0;i<5;i++)

//    {

//        printf("%d\n",*p[i]);

//    }

//    

//    return 0;

//}


//定义一个函数,向函数中传递一个32列的整型数组,在函数中用一个指向二维数组的指针变量接收,

//在函数中利用指针变量将所有的值累加求和,并返回输出。

//比如:

//输入

//5 6 8

//3 4 6

//

//输出为

//32


//int sum(int *par[],int rlenght,int clenght)

//{

//    int sum = 0;

//    for (int i=0; i<rlenght; i++) {

//        for (int j=0; j<clenght; j++) {

//            sum+= *(*(par+i)+j);

//        }

//    }

//    

//    return sum;

//}

//

//int main(int arg ,const char *argv[])

//{

//    int a[3][2] = {5,6,8,3,4,6};

//    int *p[3] = {NULL};

//    for (int i=0; i<3; i++) {

//        p[i] = a[i];

//    }

//    

//    printf("Sum = %d\n",sum(p,3,2));

//    

//    return 0;

//}


//定义一个一维字符指针数组,并利用不定数量(不多于10)个字符串将其初始化,然后将各字符串输出。

//比如:

//输入

//asdfw uuio fff tyu

//

//输出为

//tyu

//fff

//uuio

//asdfw


//#include <stdlib.h>

//int main(int arg ,const char *argv[])

//{

//    char *str[10];

//    int i=0;

//    int count =0;

//    for (; i<10; i++) {

//        str[i] = (char *)malloc(20*sizeof(char));

//        if (!str[i]) {

//            return -1;

//        }

//        scanf("%s",str[i]);

//        count++;

//        if (getchar() == '\n') {

//            break;

//        }

//    }

//    for (i=count-1; i>=0; i--) {

//        printf("%s\n",str[i]);

//    }

//    

//    return 0;

//}



//定义一个一维字符指针数组,并利用不定长字符串将其初始化,最后一段字符串以‘@’结尾,然后将各字符串输出.

//比如:

//输入

//asdfw

//dffweff@

//输出为

//asdfw

//dffweff


//#include <stdlib.h>

//unsigned long my_strlen(char *str)

//{

//    int i=0;

//    while (str[i] != '\0') {

//        i++;

//    }

//    return i;

//}

//

//int main(int arg ,const char *argv[])

//{

//    char *pstr[10]={NULL};

//    int count =0;

//    for (int i=0; i<10; i++) {

//        pstr[i] = (char *)malloc(100*sizeof(char));

//        if(!pstr[i])

//        {

//            return -1;

//        }

//        gets(pstr[i]);

//        int len = (int)my_strlen(pstr[i]);

//        count++;

//        if (pstr[i][len-1] == '@') {

//            pstr[i][len-1] = '\0';

//            break;

//        }

//        

//    }

//    //printf("%d\n",(int)my_strlen("hello"));

//    for (int i=0; i<count; i++) {

//        printf("%s\n",pstr[i]);

//    }

//

//    return 0;

//}


//实现一个函数,传递一个具有10个整形元素的数组,利用指针将数组逆序,并在main函数里将逆序后的结果输出。

//比如:

//输入:1 2 3 4 5 6 7 8 9 0

//输出:0 9 8 7 6 5 4 3 2 1


//void reverseArray(int *array,int lengh)

//{

//    int temp;

//    for (int i=0; i<lengh/2; i++) {

//        temp = array[i];

//        array[i] = array[lengh-1-i];

//        array[lengh-1-i] = temp;

//    }

//}

//

//int main(int arg ,const char *argv[])

//{

//    int int_array[10] = {1,2,3,4,5,6,7,8,9,0};

//    reverseArray(int_array,10);

//    for (int i=0; i<10; i++) {

//        printf("%d ",int_array[i]);

//    }

//    

//    return 0;

//}


//实现一个函数,传递两个有5个整形元素的数组,利用指针完成数组元素的复制,并在main函数里将新数组中的内容输出。

//比如:

//输入:1 2 3 4 5

//输出:1 2 3 4 5


//void my_arrayCopy(int *array1,int *array2,int length)

//{

//    for (int i=0; i<length; i++) {

//        *(array2+i) = *(array1+i);

//    }

//}

//int main(int arg ,const char *argv[])

//{

//    int a1[5]={1,2,3,4,5};

//    int a2[5]={0};

//    my_arrayCopy(a1,a2,5);

//    for (int i=0; i<5; i++) {

//        printf("a2[%d]=%d ",i,a2[i]);

//    }

//    

//    return 0;

//}


//实现一个函数,传递一个具有10个整形元素的数组,利用指针,使用选择排序进行从大到小排序,并在main函数里将排序后的结果输出。

//比如:

//输入:1 2 3 4 5 6 7 8 9 0

//输出:9 8 7 6 5 4 3 2 1 0




//***************************************************************


//定义两个字符串,然后比较两个串的大小,将小的字符串输出。

//比如:

//输入

//asdfwd

//ffweff

//输出为

//asdfwd


//#include <stdlib.h>

//void my_strCmp(char *str1,char *str2)

//{

//    int i=0;

//    char *small = NULL;

//    while (str1[i] !='\0' && str2[i] != '\0') {

//        if(str1[i] != str2[i])

//        {

//            if (str1[i]>str2[i]) {

//                small = str2;

//                break;

//            }

//            else

//            {

//                small = str1;

//                break;

//            }

//        }

//        i++;

//    }

//    if (str1[i] == '\0') {

//        small = str1;

//    }

//    if (str2[i] == '\0') {

//        small = str2;

//    }

//    printf("%s",small);

//}

//

//int main(int arg ,const char *argv[])

//{

//    char *str[2];

//    for (int i=0; i<2; i++) {

//        str[i] = (char *)malloc(20*sizeof(char));

//        if (!str[i]) {

//            return  -1;

//        }

//        scanf("%s",str[i]);

//    }

//    

//    my_strCmp(str[0],str[1]);

//    

//    return 0;

//    

//}


//定义两个字符串,将两个字符串连接并输出

//比如:

//输入

//asdfwd

//ffweff

//输出为

//asdfwdffweff


//#include <stdlib.h>

//char *my_strCat(char *des,char *src)

//{

//    int i=0;

//    int j=0;

//    char catstr[100];

//    while (des[i] != '\0') {

//        catstr[i] = des[i];

//        i++;

//    }

//    while (src[j]!='\0') {

//        catstr[i] = src[j];

//        i++;

//        j++;

//    }

//    catstr[i] = src[j];

//    

//    return catstr;

//}

//

//int main(int arg ,const char *argv[])

//{

//    char *str1;

//    char *str2;

//    str1 = (char *)malloc(30*sizeof(char));

//    if (!str1) {

//        return  -1;

//    }

//    scanf("%s",str1);

//    str2 = (char *)malloc(20*sizeof(char));

//    if (!str2) {

//        return  -1;

//    }

//    scanf("%s",str2);

//    

//    char *catstr = my_strCat(str1,str2);

//

//    printf("%s\n",catstr);

//    return 0;

//

//}


//字符串排序。比较三个字符串的大小,然后按从小到大的顺序将字符串输出。

//比如:

//输入

//asdfwd

//ddrwf

//ffweff

//输出为

//asdfwd

//ddrwf

//ffweff


//#include <stdlib.h>

//void sortString(char *str1,char *str2,char *str3)

//{

//    int i=0;

//    char *small = NULL;

//    while (str1[i] !='\0' && str2[i] != '\0' && str2[i] != '\0') {

//        if(str1[i] != str2[i])

//        {

//            if (str1[i]>str2[i]) {

//                small = str2;

//                break;

//            }

//            else

//            {

//                small = str1;

//                break;

//            }

//        }

//        i++;

//    }

//    if (str1[i] == '\0') {

//        small = str1;

//    }

//    if (str2[i] == '\0') {

//        small = str2;

//    }

//    printf("%s",small);

//}

//

//int main(int arg ,const char *argv[])

//{

//    char *str[3];

//    for (int i=0; i<3; i++) {

//        str[i] = (char *)malloc(30*sizeof(char));

//        if (!str[i]) {

//            return  -1;

//        }

//        scanf("%s",str[i]);

//    }

//

//    sortString(str[0],str[1],str[2]);

//

//    return 0;

//

//}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值