还是和指针、内存相关,最近又看了林锐先生的文章,写了一些代码在这里,大家如果感兴趣,可以看看,答案都是什么?
程序是用vc6+sp6编译的,有编译问题、有运行时问题,呵呵。。。
Q1-Q8,都输出什么内容???
(暂时放首页几天,到时候撤下来)
#include "string.h"
#include "stdlib.h"
char* GetName(void){
char name[20] = "juqiang";
return name;
}
int GetSize(char buf[]){
return sizeof(buf);
}
int main(int argc, char* argv[])
{
char buf[] = "hello";
printf("Size of buf is:%d\r\n",sizeof(buf));// Q1
printf("Size of buf calling is:%d\r\n",GetSize(buf));// Q2
printf("Hello %s\r\n",GetName());// Q3
char* p = (char*)malloc(100);
int paddr = (int)p;
free(p);
int paddr2 = (int)p;
printf("paddr equals paddr2 is:%d\r\n",paddr==paddr2);// Q4
if(NULL != p){
strcpy(p,"hello!");
}
printf("The contents of p is:%s",p);// Q5
char name[] = "juqiang";
name[0] = 'J';
name[1] = 'U';
printf("Name is:%s\r\n",name);// Q6
char bufname[] = "juqiang";
char* name2 = bufname;
*(name2+0)='J';
*(name2+1)='U';
printf("Name is:%s,%s\r\n",bufname,name2);// Q7
char* name3 = "juqiang";
*(name3+0) = 'J';
*(name3+1) = 'U';
printf("Name is:%s\r\n",name3);// Q8
return 0;
}