Posts Tagged ‘C’

May 21, 2009

变量作用域

1
2
3
4
5
6
7
8
9
int x = 5;
int f() 
{
   int x = 3;       //此x在f作用域中将覆盖全局的x
   {
       extern int x;   //通过extern关键词引入文件作用域的x
       return x;        //于是此作用域内的想是全局的x
   }
}

指针

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int foo();
 
int (*p)() = &foo;
cout<<p();
 
p = foo;
cout<<p();
 
p = *f;
cout<<*p)();
 
p = *******foo;
cout<<p();
//上述代码,每一对对指针的赋值和调用都是等价的
Tags: ,. 34 views
May 12, 2009

数组和指针之间存在如下等价关系:

  • 一维数组等价于元素的指针:int a[20] <==> int *const a;
  • 二维数组等价于指向一维数组的指针,int b[3][4] <==> int(*const b) [4];
  • 三维数组等价于指向二维数组的指针,int c[2][3][4] <==> int (*const c) [3][4];

其他多维数组与二、三维相似,不再赘述。

Tags: . 15 views

我的理解:

在main函数中由于定义了一个局部变量a,因此全局变量a被隐藏。int a = a;定义a(为a分配内存),然后用a(右侧的a)给a(左侧的a)赋值。所以这段代码是可以通过编译的,但是链接时候就会出错。因为,使用了一个未初始化的变量来给一个变量赋值。有可能在某些编译器上面是可以链接并运行的,说明它对变量的初始化不太严格。

Tags: ,. 18 views
April 27, 2009

运行期(runtime)判断
下面的程序可以在运行期判断 endianess:

int IsBigEndian (void)
{
        static const int v =1;
        return *(char*)&v?0:1;
}
Tags: ,. 21 views
April 25, 2009

Introduction

A good way to get into an argument with a computer programmer is to attempt to explain why the language they are using is not as good as the one you are using. Most of the programmers I know are positively religious over their Operating System (see my other article), their development language and finally their text editor.

Tags: . 10 views
Page 3 of 3123