• 指针的话题


    指针与数组

    
     /*
         * 指针与数组
         * */
        int scores[]={65,61,26,72,83,90,100};
    //    方法1
        //int *p=&scores[0];
    //    方法2
        int *p=scores;
        for (int i = 0; i < sizeof(scores)/ sizeof(int); ++i) {
            //printf("%d		",scores[i]);
            //printf("%d		",p[i]);
            //printf("%d		",*(p+i));
            //printf("%d		",*(scores+i));
            //printf("%d		",*(i+p));
            //printf("%d		",*(i+scores));
            //printf("%d		",i[scores]);
            printf("%d		",i[p]);
        }
    ****************************************************函数******************************************************************
    #include <stdio.h>
    // 方法1
    /*void handle1D(int marks[],int index){
        for (int i = 0; i <index ; i++) {
            printf("%d		",marks[i]);
        }
    }*/
    //方法2
    void handle1D(int *marks,int index){
        for (int i = 0; i <index ; i++) {
            printf("%d		",marks[i]);
        }
    }
    int main() {
        /*
         * 指针与函数
         * */
        int scores[]={65,61,26,72,83,90,100};
        //方法1
    //    handle1D(scores, sizeof(scores)/ sizeof(int));
    //  方法2
        //handle1D(&scores[0], sizeof(scores)/ sizeof(int));
        //这个大部分编译器是不合法的   但是Clion 编译器可以出结果
        handle1D(&scores, sizeof(scores)/ sizeof(int));
        getchar();
    }
    

    指针与字符串

    #include <stdio.h>
    
    int main() {
    
        //char motto[]="nothing is equal to knowledge";
        char *motto="nothing is equal to knowledge";
        //输出 方法1
        printf("%s	
    ",motto);
        //输出方法2
    
        for (int i = 0; i <*(motto+i)!='' ; ++i) {
           // putchar(motto[i]);
            printf("%c",*(motto+i));
        }
        putchar(10);
    
        //打印下标为4的元素值
       // printf("%c	
    ",motto[4]);
        getchar();
    }
    ****************************************函数***********************************************
    #include <stdio.h>
    /*
    void handleString(char maxim[]){
        //方法1
        puts(maxim);
        //方法2
        for (int i = 0; maxim[i]!='' ; ++i) {
            putchar(maxim[i]);
        }
        //方法3
        for (int i = 0; *(maxim+i)!='' ; ++i) {
            putchar(*(maxim+i));
        }
    }
    */
    
    
    void handleString(char *maxim){
        //方法1
        puts(maxim);
        //方法2
        for (int i = 0; maxim[i]!='' ; ++i) {
            putchar(maxim[i]);
        }
        putchar(10);//换行
        //方法3
        for (int i = 0; *(maxim+i)!='' ; ++i) {
            putchar(*(maxim+i));
        }
    
    }
    
    int main() {
        char *motto="nothing is equal to knowledge";
    
        handleString(motto);
      //  handleString(&motto[0]);
      //这种方式是错误的
      /*
       *warning: passing argument 1 of 'handleString' from incompatible pointer type [-Wincompatible-pointer-types]
         handleString(&motto);
    
         expected 'char *' but argument is of type 'char **'
     void handleString(char *maxim){
          ^~~~~~~~~~~~
       * */
     //   handleString(&motto);
    
        getchar();
    }
    
    

    函数和指针

    #include <stdio.h>
    
    int maxValue(int,int);//函数声明
    
    int main() {
    
        printf("max_Value=%d	
    ",maxValue(5,6));//通过函数名调用maxValue函数
        getchar();
    }
    
    //定义maxValue函数
    int maxValue(int x,int y){
        return (x>y)?x:y;
    }
    
    ***********************************************************************************************************************************
    #include <stdio.h>
    /*
     * 用  函数指针变量  调用函数
     * */
    int maxValue(int,int);//函数声明
    
    int minValue(int,int);//函数声明
    // 类型名 (*指针变量名)(函数参数列表);
    int (*pValue)(int,int);//定义指向函数的指针变量pMaxValue
    
    int main() {
    
        pValue=maxValue;
        printf("max_Value=%d	
    ",maxValue(5,6));//通过函数名调用maxValue函数
    
        printf("max_Value=%d	
    ",(*pValue)(5,6));//通过指针变量调用maxValue函数
        puts("***********************************************************");
    
        pValue=minValue;
        printf("min_Value=%d	
    ",minValue(5,6));//通过函数名调用maxValue函数
    
        printf("min_Value=%d	
    ",(*pValue)(5,6));//通过指针变量调用maxValue函数
    
    
    
        getchar();
    }
    
    //定义maxValue函数
    int maxValue(int x,int y){
        return (x>y)?x:y;
    }
    
    //定义maxValue函数
    int minValue(int x,int y){
        return (x>y)?y:x;
    }
    
    
    
    
    
    
    
    
    
    
    
  • 相关阅读:
    linux下修改mysql密码
    会话跟踪技术之——cookie
    servlet之注册登录(简写)
    java服务端和用户端
    JavaBean和jsp的开发模型
    session的用法
    jsp元素
    servlet
    ServletContext对象统计在线人数
    图片站点服务
  • 原文地址:https://www.cnblogs.com/wanson/p/10015285.html
Copyright © 2020-2023  润新知