zoukankan      html  css  js  c++  java
  • 指针杂谈

    #include<cstdio>
    //http://blog.csdn.net/solomon1558/article/details/40798901
    //http://www.360doc.com/content/12/0403/11/9140140_200442978.shtml
    double do1()
    {
    	printf("uses do1
    ");
    	return 122.6546564;
    }
    int main()
    {
    	double (*do2)()=do1;//函数指针
    	int (*do3)()=(int(*) ())do1;//强制类型转换函数指针
    	printf("1 %f
    ",do2());//printf("%f
    ",(*do2)());也可以,正常的输出函数返回值
    	printf("2 %d
    ",do2);//正常的方式输出函数地址
    	printf("3 %f
    ",do2);//奇怪的方式输出函数地址
    	printf("4 %f
    ",do3());//(这样子强制类型转换函数指针,尽量不要用)%f的方式输出
    	printf("5 %d
    ",do3());//只会输出奇怪的东西
    	printf("6 %f
    ",do3);//用奇怪的方式输出函数的地址
    	printf("7 %d
    ",do3);//用正常的方式输出函数的地址
    	
    	int a=(int)(&do1);
    	printf("a1 %d
    ",*(int*)&a);
    	printf("a2 %f
    ",*(int*)&a);//实际上,前面好几个输出(如3)都是由于这个原因
    	//就是直接读取整数a在内存中的数据且把它当成浮点数输出,而浮点数和整数存储方式不同,因此输出错误
    	return 0;
    }
    #include<cstdio>
    //http://blog.csdn.net/stpeace/article/details/22220777
    struct A
    {
    	int p;
    	A& f(int x)
    	{
    		p=x;
    		return *this; 
    	}
    };
    struct B
    {
    	int p;
    	B *f(int x)
    	{
    		p=x;
    		return this; 
    	}
    };
    int main()
    {
    	A *a=new A;
    	a->f(3).f(5).f(25);
    	printf("1 %d
    ",a->p);
    	delete a;
    	A b;
    	b.f(3).f(4).f(6);
    	printf("2 %d
    ",b.p);
    	B c;
    	c.f(3)->f(4)->f(24);
    	printf("3 %d
    ",c.p);
    	B *d=new B;
    	d->f(5)->f(7)->f(9);
    	printf("4 %d
    ",d->p);
    	delete d;
    	printf("1a %d
    ",a->p);
    }
    #include<cstdio>
    struct A
    {
    	int p;
    	A& f(int x)
    	{
    		p=x;
    		return *this; 
    	}
    };
    int main()
    {
    	while(true)
    	{
    		A *a=new A;
    		delete a;//去掉这句就不停吃内存 
    	}
    }


  • 相关阅读:
    17. Letter Combinations of a Phone Number
    16. 3Sum Closest
    15. 3Sum
    14. Longest Common Prefix
    13. Roman to Integer
    12. Integer to Roman
    11. Container With Most Water
    10. Regular Expression Matching
    9. Palindrome Number
    8. String to Integer (atoi)
  • 原文地址:https://www.cnblogs.com/hehe54321/p/8470442.html
Copyright © 2011-2022 走看看