码迷,mamicode.com
首页 > 其他好文 > 详细

指针的话题

时间:2018-11-25 14:47:12      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:pass   \n   led   类型   printf   i++   getch   定义   字符   

指针与数组


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









指针的话题

标签:pass   \n   led   类型   printf   i++   getch   定义   字符   

原文地址:https://www.cnblogs.com/wanson/p/10015285.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!