zoukankan      html  css  js  c++  java
  • 指针的话题

    指针与数组

    
     /*
         * 指针与数组
         * */
        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;
    }
    
    
    
    
    
    
    
    
    
    
    
  • 相关阅读:
    Android入门程序(ListView包括一个网络状态显示)
    shell
    android.os.NetworkOnMainThreadException
    apache虚拟主机名不区分大小写的解决办法
    QT制作全屏播放器以及出现的问题
    QT使用Q_OBJECT链接不通过的一种情况
    使用PHP返回需要登录验证的HTML页面
    怎么样得到Boost的options_description的描述字符串
    QT的QHttp无信号发出,窗口程序关闭时崩溃
    apache 配置https
  • 原文地址:https://www.cnblogs.com/wanson/p/10015285.html
Copyright © 2011-2022 走看看