zoukankan      html  css  js  c++  java
  • c语言 参数传值和传地址

    static void TestCharP(char **p)
    {
    	char *q = "ssssss";
    	*p=q;
    }
    
    static void TestCharP1(char *p)
    {
    	char *q = "ssssss";
    	p=q;
    }
    
    static void TestInt(int *a)
    {
    	*a = 5;
    }
    
    static void TestInt1(int a)
    {
    	a = 5;
    }
    
    static void TestBuf(char buf[])
    {
    	buf[0] = 'a';
    }
    
    
    //传值和传地址的区别
    int main()
    {
    	int a = 0;
    	int a1 = 0;
    	char *p=NULL;
    	char buf[5] = {0};
    	char *p1 = NULL;
    
    	TestInt(&a);
    	printf("%d
    ",a);
    
    	TestInt1(a1);
    	printf("%d
    ",a1);
    
    	TestCharP(&p);
    	printf("%s
    ",p);
    
    	TestCharP1(p1);
    	printf("%s
    ",p1);
    
    	TestBuf(buf);
    	printf("%s
    ",buf);
    
    	return 0;
    }
    

    输出:

    2.查看地址转换

    static void TestCharP(char *p)
    {
    	//p指向地址:0x00045860
    	char *q = "ssssss";
    	//q指向地址:0x00045858
    	p=q;
    	//p指向地址:0x00045858
    }
    
    
    //传值和传地址的区别
    int main()
    {
    	char *p="aaa";
    	//p指向地址:0x00045860
    	TestCharP(p);
    	//p指向地址:0x00045860
    	printf("%s
    ",p);
    
    	return 0;
    }
    

    查看 p指向地址没有改变

    static void TestCharP(char **p)
    {
    	//*p指向地址:0x0014f888
    	char *q = "ssssss";
    	//q指向地址:0x01185858
    	*p=q;
    	//*p指向地址:0x01185858
    }
    
    
    //传值和传地址的区别
    int main()
    {
    	char *p="aaa";
    	//p指向地址:0x01185860
    	TestCharP(&p);
    	//p指向地址:0x00045858
    	printf("%s
    ",p);
    
    	return 0;
    }
    

      查看 p指向地址改变

  • 相关阅读:
    TSQL存储过程:获取父级类别图片
    ASP.NET小收集<7>:JS创建FORM并提交
    JS收集<4>:限制输入搜索串
    js编码风格
    学习日志0504
    记于20120508日晚
    NHibernate中的Session yangan
    SQL Server2005排名函数 yangan
    让IE8兼容网页,简单开启兼容模式 yangan
    Log4Net跟我一起一步步 yangan
  • 原文地址:https://www.cnblogs.com/yaosj/p/6732023.html
Copyright © 2011-2022 走看看