2.12一级指针作为函数形参和返回值

【注:自己从老师讲解出理解的,不对的地方望指正】

【注:本程序验证是使用vs2013版】

【独学而无友,孤陋而寡闻】

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable:4996)


void fun(int *p){
    p = 0xaabb;
    printf("fun:p= %p\n", p);
}

void fun2(int **temp){
    *temp = 0xbbcc;
    printf("fun2:*temp= %p\n", *temp);
}

int *fun3(int *p){
    p = 0xccdd;
    printf("fun3:p= %p\n", p);
    return p;
}

int main(void){
    /*
        一个变量,应该定义一个什么样类型的指针来保存它的地址
        应该在原来的基础上加一个*来保存它的地址
    */
    int a = 100;
    int *b = &a;
    int **c = &b;

    int *********t1 = NULL;
    int **********t2 = &t1;

    /*
        1.值传递:形参的修改不会影响到实参
        2.地址传递:形参(通过*操作符号)的任何修改会影响到实参。
        3.函数返回值:可以通过函数返回值来影响实参,不过一般返回值都作为状态判断使用。
        4.地址传递和函数返回值相比的优点:通用性强,更加灵活
    */
    int *d = 0x1122;
    printf("d: %p\n", d);
  

fun(d);
//这个是值传递,不是地址传递,(除了数组,名字就代表地址)地址传递需要在实参前面加*号 printf("d: %p\n", d);   

fun2(
&d);//地址传递,修改实参 printf("d= %p\n", d);   

d
= fun3(&d);//通过函数返回值修改实参 printf("d= %p\n", d);   
printf(
"\n"); system("pause"); return 0; }

猜你喜欢

转载自www.cnblogs.com/wlstm/p/11087382.html
今日推荐