昆仑山:眼中无形心中有穴之穴人合一

夫君子之行,静以修身,俭以养德;非澹泊无以明志,非宁静无以致远。夫学须静也,才须学也;非学无以广才,非志无以成学。怠慢则不能励精,险躁则不能冶性。年与时驰,意与岁去,遂成枯落,多不接世。悲守穷庐,将复何及!

 

指针的话题

指针与数组


 /*
     * 指针与数组
     * */
    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\t\t",scores[i]);
        //printf("%d\t\t",p[i]);
        //printf("%d\t\t",*(p+i));
        //printf("%d\t\t",*(scores+i));
        //printf("%d\t\t",*(i+p));
        //printf("%d\t\t",*(i+scores));
        //printf("%d\t\t",i[scores]);
        printf("%d\t\t",i[p]);
    }
****************************************************函数******************************************************************
#include <stdio.h>
// 方法1
/*void handle1D(int marks[],int index){
    for (int i = 0; i <index ; i++) {
        printf("%d\t\t",marks[i]);
    }
}*/
//方法2
void handle1D(int *marks,int index){
    for (int i = 0; i <index ; i++) {
        printf("%d\t\t",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\t\n",motto);
    //输出方法2

    for (int i = 0; i <*(motto+i)!='\0' ; ++i) {
       // putchar(motto[i]);
        printf("%c",*(motto+i));
    }
    putchar(10);

    //打印下标为4的元素值
   // printf("%c\t\n",motto[4]);
    getchar();
}
****************************************函数***********************************************
#include <stdio.h>
/*
void handleString(char maxim[]){
    //方法1
    puts(maxim);
    //方法2
    for (int i = 0; maxim[i]!='\0' ; ++i) {
        putchar(maxim[i]);
    }
    //方法3
    for (int i = 0; *(maxim+i)!='\0' ; ++i) {
        putchar(*(maxim+i));
    }
}
*/


void handleString(char *maxim){
    //方法1
    puts(maxim);
    //方法2
    for (int i = 0; maxim[i]!='\0' ; ++i) {
        putchar(maxim[i]);
    }
    putchar(10);//换行
    //方法3
    for (int i = 0; *(maxim+i)!='\0' ; ++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\t\n",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\t\n",maxValue(5,6));//通过函数名调用maxValue函数

    printf("max_Value=%d\t\n",(*pValue)(5,6));//通过指针变量调用maxValue函数
    puts("***********************************************************");

    pValue=minValue;
    printf("min_Value=%d\t\n",minValue(5,6));//通过函数名调用maxValue函数

    printf("min_Value=%d\t\n",(*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;
}










posted on 2018-11-25 12:50  Indian_Mysore  阅读(177)  评论(0编辑  收藏  举报

导航