标签:return sizeof pen amp http include 技术分享 [1] point
1.#include <stdio.h>
int main()
{
int a[5] = {1, 2, 3, 4, 5};
int* p1 = (int*)(&a + 1);
int* p2 = (int*)((int)a + 1);
int* p3 = (int*)(a + 1);
printf("%d, %d, %d\n", p1[-1], p2[0], p3[1]);
return 0;
}
// A. 数组下标不能是负数,程序无法运行
// B. p1[-1]将输出随机数,p2[0]输出2, p3[1]输出3
// C. p1[-1]将输出乱码, p2[0]和p3[1]输出2
2.#include <stdio.h>
#include <malloc.h>
int main()
{
char s1[] = {‘H‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘};
int i = 0;
char s2[] = {‘W‘, ‘o‘, ‘r‘, ‘l‘, ‘d‘};
char* p0 = s1;
char* p1 = &s1[3];
char* p2 = s2;
int* p = &i;
printf("%d\n", p0 - p1);
//printf("%d\n", p0 + p2);
printf("%d\n", p0 - p2);
//printf("%d\n", p0 - p);
//printf("%d\n", p0 * p2);
//printf("%d\n", p0 / p2);
return 0;
}
3.#include <stdio.h>
#include <malloc.h>
#define DIM(a) (sizeof(a) / sizeof(*a))
int main()
{
char s[] = {‘H‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘};
char* pBegin = s;
char* pEnd = s + DIM(s);//指向字符数组的后一个元素,这里并未访问,所以没有越界。
char* p = NULL;
for(p=pBegin; p<pEnd; p++)
{
printf("%c", *p);
}
printf("\n");
return 0;
}
4.#include <stdio.h>
#include <time.h>
int main()
{
clock_t start;
clock_t end;
int a[10000];
int b[10000];
int* pEnd = &a[10000];
int* pa = NULL;
int* pb = NULL;
int i = 0;
int k = 0;
start = clock();
for(k=0; k<10000; k++)
{
for(i=0; i<10000; i++)
{
b[i] = a[i];
}
}
end = clock();
printf("Index Timing: %d\n", end - start);
start = clock();
for(k=0; k<10000; k++)
{
for(pa=a, pb=b; pa<pEnd;)
{
*pb++ = *pa++;
}
}
end = clock();
printf("Pointer Timing: %d\n", end - start);
return 0;
}
标签:return sizeof pen amp http include 技术分享 [1] point
原文地址:http://www.cnblogs.com/wxb20/p/6150483.html