指针的话题

Posted wanson

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了指针的话题相关的知识,希望对你有一定的参考价值。

指针与数组


 /*
     * 指针与数组
     * */
    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;
}









以上是关于指针的话题的主要内容,如果未能解决你的问题,请参考以下文章

第十三章 高级指针话题指针

指针的话题

片段中的空指针异常

活动到片段通信:当我尝试从活动更新片段中的文本视图时,出现空指针异常

C和指针 第十三章 高级指针话题

C的指针疑惑:C和指针13(高级指针话题)