- //1.在win32平台上运行下面程序,并说明sizeof的性质。
- #include<stdio.h>
- #include<stdlib.h>
- int main(void)
- {
- char a[30];
- char *b = (char *)malloc(20 * sizeof(char)); //本列是相应的输出结果
- cout << sizeof(a) << endl; //30
- cout << sizeof(b) << endl; //4
- cout << sizeof(a[3]) << endl; //1
- cout << sizeof(b+3) << endl; //4
- cout << sizeof(*(b+4)) << endl; //1
- return 0;
- }
- //下面的我写的测试程序:
- #include<iostream>
- using namespace std;
- #include<stdio.h>
- #include<stdlib.h>
- int main(void)
- {
- char a[30];
- char *b = (char *)malloc(20 * sizeof(char));
- char *c = b+3;
- cout << sizeof(int ) << endl; //4
- cout << sizeof( long ) << endl; //4
- cout << sizeof( short ) << endl; //2
- cout << sizeof(b) << endl; //4
- cout << sizeof(char *) << endl; //4
- cout << sizeof(c) << endl; //4
- cout << sizeof(b+3) << endl; //4
- cout << sizeof(a) << endl; //30
- cout << sizeof(a[3]) << endl; //1
- cout << sizeof(char ) << endl; //1
- cout << sizeof(*(b+4)) << endl; //1(b+4)是一个指向char型的指针,
- ///所以*(b+4)就是一个char型。
- return 0;
- }
相比到这里大家应该都明白差不多了吧。
以下是MSDN中sizeof关键字的解释:
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,然后free掉p。但是问题来了,单链表的话如何得到节点p的前一个节点呢?仅这条件就只能查到节点p后面的节点啊。。。。。。。
好吧,其实我也是看了提示才恍然大悟的。。。。。。
想想删除了这个节点的结果是什么?
不就是“上一个节点p-1的指针所指向的下一个节点中的值”是“未删除节点p时p+1节点上的值”吗?
所以法子来了:
1.把节点p上的值复制为下一节点p+1中所存的值。
2.然后删除下一个节点p+1.
3.即:q = p->next;
P->next = q->next;
Free(q);