2011年7月8日
摘要: #include <stdio.h> #include <string.h> struct TStudent { long int ID; union{ char name[10]; char mingzi[11]; }; char dept[17]; }; int main() { TStudent stu; strcpy(stu.name,"zh"); strcpy(stu.dept,"computer science"); printf("student`s mingzi is:%s\n",stu.min 阅读全文
posted @ 2011-07-08 23:20 程序员新鲜事 阅读(145) 评论(0) 推荐(0)
摘要: 代码:#include <stdio.h> char* GetString1() { char p[] = "Hello World"; return p; } char* GetString2() { char *p = "Hello World"; return p; } int main() { printf("GetString1 returns: %s. \n", GetString1()); printf("GetString2 returns: %s. \n", GetString2()) 阅读全文
posted @ 2011-07-08 21:45 程序员新鲜事 阅读(161) 评论(0) 推荐(0)
摘要: 代码:#include <stdio.h> int main() { int a=0,b=0; printf("%d\n",!a||++b||b++); printf("b=%d\n",b); printf("%d\n",!a&&++b&&b++); printf("b=%d\n",b); return 0; }疑:以上输出什么,为什么?解答:int a=0,b=0;在在第一次printf语句中,!a为1,后面的++b和b++就不做了,所以第二次输出是0,第三次print 阅读全文
posted @ 2011-07-08 21:42 程序员新鲜事 阅读(124) 评论(0) 推荐(0)
摘要: 代码:#include <iostream> using namespace std; class A { public: A() { Print(); } virtual void Print() { cout<<"A is constructed.\n"; } }; class B: public A { public: B() { Print(); } virtual void Print() { cout<<"B is constructed.\n"; } }; int main() { A* p... 阅读全文
posted @ 2011-07-08 21:39 程序员新鲜事 阅读(179) 评论(0) 推荐(0)
摘要: 代码:头文件print_tools.h#include<stdio.h> void printStr(const char *pStr) { printf("%s\n",pStr); } void printtInt(const int i) { printf("%d\n",i); } 头文件counter.h#include"print_tools.h" static int sg_value; void counter_init() { sg_value=0; } void counter_count() { sg_v 阅读全文
posted @ 2011-07-08 21:15 程序员新鲜事 阅读(179) 评论(0) 推荐(0)
摘要: 代码:#include<iostream> using namespace std; class A { private: int value; public: A() { value=0; } void coutHello() { cout<<"hello"<<endl; } void coutValue() { cout<<value<<endl; } }; int main() { A *pA=NULL; //空指针,所指向的内容不可访问存取 pA->coutHello(); pA->coutVa 阅读全文
posted @ 2011-07-08 15:09 程序员新鲜事 阅读(133) 评论(0) 推荐(0)
  2011年7月7日
摘要: 导读:原文作者Kevin Pang在kevinwilliampang.com发表一篇《10 Programming Proverbs Every Developer Should Know》。译文由伯乐在线整理编译成《10句编程箴言 每个程序员都应该知道》。文章内容如下:所谓谚语,就是用言简意赅、通俗易懂的方式传达人生箴言和普遍真理的话,它们能很好地帮助你处理生活和工作上的事情。也正因如此,我才整理了10句编程谚语,每位开发人员都应该铭记他们,武装自己。1. 无风不起浪别紧张,这也许只是一场消防演习代码设计是否糟糕,从某些地方就可以看出来。比如:a. 超大类或超大函数b. 大片被注释的代码c. 阅读全文
posted @ 2011-07-07 17:17 程序员新鲜事 阅读(213) 评论(0) 推荐(0)
  2011年7月6日
摘要: 代码:#include<iostream> using namespace std; class A { }; class B { char a; int b; }; class C { void foo(){}; }; class D { virtual void foo(){}; }; int main() { cout<<sizeof(A)<<sizeof(B)<<sizeof(C)<<sizeof(D); return 0; }疑:结果是什么,为什么呢?解答:sizeof(A)为1,而不是0,虽然空类没有任何成员变量,但其实体 阅读全文
posted @ 2011-07-06 01:11 程序员新鲜事 阅读(190) 评论(0) 推荐(0)
摘要: 代码:#include<iostream> using namespace std; int main() { char *str1 = "string"; char *str2 = "string"; if(str1 == str2) cout<<"str1 is same as str2"; }疑:str1 的值是否等于 str2 而输出字符串“str1 is same as str2”呢,为什么?解答:是的 “str1 is same as srr2”,也就是说str1与str2指向了相同的内存地址,因为 阅读全文
posted @ 2011-07-06 00:37 程序员新鲜事 阅读(149) 评论(0) 推荐(0)
摘要: 代码:#include<iostream> using namespace std; void foo(int p1,int p2,int p3) { cout<<"p1="<<p1<<endl <<"p2="<<p2<<endl <<"p3="<<p3<<endl; } int main() { int i; cout<<"first call:"<<endl; i=0; 阅读全文
posted @ 2011-07-06 00:24 程序员新鲜事 阅读(135) 评论(0) 推荐(0)