Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4567051
  • 博文数量: 385
  • 博客积分: 21208
  • 博客等级: 上将
  • 技术积分: 4393
  • 用 户 组: 普通用户
  • 注册时间: 2006-09-30 13:40
文章分类

全部博文(385)

文章存档

2015年(1)

2014年(3)

2012年(16)

2011年(42)

2010年(1)

2009年(2)

2008年(34)

2007年(188)

2006年(110)

分类: C/C++

2006-10-30 17:30:52

1:a和b交换,只能用一句语句,不能用第三个变量。

#include <stdio.h>
int     main()
{
    int a=3;
    int b=5;
    
    printf("before a=%d,b=%d\n",a,b);
    
    a=a+b,b=a-b,a=a-b;
    
    printf("after a=%d,b=%d\n",a,b);
    return 0;
}


2:将单向链表reverse,如ABCD变成DCBA,只能搜索链表一次。

pre=NULL;
p=head;

while(p)
{
    post=p->next;
   
    p->next=pre;
   
    pre=p;
   
    p=post;
   

}

head=pre;

3:
int b=500;
const int *a=&b;
int const *a=&b;
int const *const a=&b;
三个是否等同,有什么区别。
解:
const int *a=&b;//定义a为指向int常量的指针,a为b的地址 此后a可变 而b不可变
int const *a=&b;//同上
int const *const a=&b;//定义a 为指向int常量的 常量指针 ,a, b 均不可赋值或改变

4:C和C++中的struct有什么不同,C++中struct和class的区别。
解: (1) 由于c是纯粹的面向过程的语言,c中的struct只是仅仅的结构体,不能有成员函数,只能有成员变量.
    而c++中的struct本身就是类,可以包含成员变量和成员函数
(2) C++中 struct 的成员默认为public成员,而 class则默认为 private成员


5.char * const *(*next[10])[];定义的是什么


解:     分析一下哦 不保证绝对正确,好久不搞这个了
    (1 ) 变量定义中 * 具有左结合性,可以把 char * const * 作为一个变量类型T看待
    char * const * pcpX ;//定义pcpX为指向 char*型常量的 指针,  char* 为字符指针类型
    (2 )       T (*next[10])[];
    对比 int a ; 和 int a[] ; 可以先分析T (*next[10]) ;
    T* next[10] ;//表示next为一指针数组,数组中元素为指向T类型的指针
    而T (*next[10])则表示next为指向 T 类型数组的指针  ???????
    故T (*next[10])[] 表示一个这样的数组,其中的每个元素均是指向 T 类型数组的指针
    而T为指向 char*型常量的 指针
    头大了吧
关于 char * const *(*next[10])[];定义的是什么


这个问题在code project上曾经有一篇文章,好像名叫:How to Interpret complex C/C++ Declarations,这篇文章就是讲如何分析这种复杂变态的定义。

按照文章所提出的分析方法可以这样分析:
next is an array of (some type) with 10 elements  -->next[10]
..................... of (pointer) .................. -->*next[10]
..................... of (pointer to an array) ...-->(*next[10])[]
..................... of (pointer to an array of pointer)... -->*(*next[10])[]
..................... of (pointer to an array of const pointer)... -->const *(*next[10])[]
..................... of (pointer to an array of const pointer to a pointer)... -->* const *(*next[10])[]
.....................of (pointer to an array of const pointer to a char pointer)... -->char* const *(*next[10])[]

to sum up:
the next is an array of (pointer to an array of const pointer to a char pointer) with 10 elements;
say, every single element in next is a pointer which points to an array of const pointer which points to a char pointer.

之所以用e文是因为它的语法和这个类型的声明规则很类似,表达起来更加容易。
无论怎样,实际应用的时候几乎不会出现这样的BT语句,除非是在应聘的时候。
阅读(2570) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~