发新话题
打印

intel面试题(转)

面霸应届生求职网欢迎您!

intel面试题(转)

intel 面食题,我说对linux很熟悉,他就让我挑bug,在linux下运行:
#include <stdio.h>
char *reverse(char* str)
{
    int len=0, i=0;
    char *pstr=str, *ptemp,*pd;
    while(*++pstr)
       len++;
    pstr--;
    //ptemp=(char*)malloc(len+1);
    ptemp=(char*)malloc(len+1);
    pd=ptemp;
    while(len--){
        *ptemp=*pstr;
        ptemp++;
        pstr--;
        i++;
    }
    *ptemp=*pstr;
    ptemp++;
    *ptemp=&#39;&#39;\0&#39;&#39;;
    return pd;
}
main()
{
    char string[40]="Hello World!";
    char *pstr=string;
    printf("%s", pstr);
    printf("%s", reverse(pstr));
}
运行结果为 :
Segmentation Fault (core dumped)
但把char string[40]="Hello World!";
改为char string[40]="Hello World";
结果正确!!!
Nature is restless
发新话题