1. //1.在win32平台上运行下面程序,并说明sizeof的性质。 
  2.  
  3. #include<stdio.h> 
  4.  
  5. #include<stdlib.h> 
  6.  
  7. int main(void
  8.  
  9.  
  10. char a[30]; 
  11.  
  12. char *b = (char *)malloc(20 * sizeof(char));      //本列是相应的输出结果 
  13.  
  14. cout << sizeof(a) << endl;       //30 
  15.  
  16. cout << sizeof(b) << endl;   //4 
  17.  
  18. cout << sizeof(a[3]) << endl;        //1 
  19.  
  20. cout << sizeof(b+3) << endl;         //4 
  21.  
  22. cout << sizeof(*(b+4)) << endl;  //1 
  23.  
  24. return 0; 
  25.  
  26.  
  27. //下面的我写的测试程序: 
  28.  
  29. #include<iostream> 
  30.  
  31. using namespace std; 
  32.  
  33. #include<stdio.h> 
  34.  
  35. #include<stdlib.h> 
  36.  
  37. int main(void
  38.  
  39.  
  40.    char a[30]; 
  41.  
  42.    char *b = (char *)malloc(20 * sizeof(char)); 
  43.  
  44.     char *c = b+3; 
  45.  
  46.     cout << sizeof(int ) << endl;                  //4   
  47.  
  48.     cout << sizeoflong ) << endl;                //4    
  49.  
  50.    cout << sizeofshort ) << endl;                //2 
  51.  
  52.     cout << sizeof(b) << endl;    //4 
  53.  
  54. cout << sizeof(char *) << endl;                //4 
  55.  
  56. cout << sizeof(c) << endl;                    //4 
  57.  
  58. cout << sizeof(b+3) << endl;         //4  
  59.  
  60. cout << sizeof(a) << endl;   //30 
  61.  
  62.     cout << sizeof(a[3]) << endl;        //1 
  63.  
  64. cout << sizeof(char ) << endl;                 //1 
  65.  
  66. cout << sizeof(*(b+4)) << endl;  //1(b+4)是一个指向char型的指针,                                    
  67.  
  68.                                               ///所以*(b+4)就是一个char型。 
  69.  
  70. return 0; 
  71.  

 

相比到这里大家应该都明白差不多了吧。

以下是MSDNsizeof关键字的解释:

sizeof Operator

sizeof expression

The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). 

This keyword returns a value of type size_t.

The expression is either an identifier or a type-cast expression (a type specifier enclosed in parentheses).

When applied to a structure type or variable, sizeof returns the actual size, which may include padding

 bytes inserted for alignment. When applied to a statically dimensioned array, sizeof returns the size of the entire array. 

The sizeof operator cannot return the size of dynamically allocated arrays or external arrays.

 

2.现有一单向链表,但只知道一个指向某一节点的指针p,且假设该节点不是尾节点,试编程删除这个节点。

 

分析:

要删除这个节点,首先肯定想到的是把前一个节点p-1的指针指向后一个节点p+1,然后freep。但是问题来了,单链表的话如何得到节点p的前一个节点呢?仅这条件就只能查到节点p后面的节点啊。。。。。。。

好吧,其实我也是看了提示才恍然大悟的。。。。。。

想想删除了这个节点的结果是什么?

不就是“上一个节点p-1的指针所指向的下一个节点中的值”“未删除节点pp+1节点上的值”吗?

所以法子来了:
    1.把节点p上的值复制为下一节点p+1中所存的值。

2.然后删除下一个节点p+1.

3.即:q = p->next;

          P->next = q->next;

          Free(q);