读书人

遇见几道面试题 有兴趣的进来给看看,该

发布时间: 2012-03-03 15:33:03 作者: rapoo

遇见几道面试题 有兴趣的进来给看看

有几道面试题不会 各位指点~
小弟真的很菜, 真的 ,但是再菜为了下个月不被房东扫地出门也得努力呀 ....


1. 哪里有问题?
-------------------------------------
void s_add( int n)
{
static int i = 100;
i += n;

return i;
}

2. 同上 除了最后没有把指针置空
-----------------------------------------
void stxc( char *p)
{
p = (char *)malloc(100);
strcpy(p, "hello world");
}

int main()
{
char * str = NULL;
stxc(str);
printf("%s\n", str);
free(str);

return 0;
}

3. 这道题问 A B在没有赋值的情况下是多少?
-----------------------------------------
emnu s{
A
B
}




[解决办法]
1.void s_add( int n)-->int s_add( int n)
2.传值方式不会改变p的值
3.0,1


[解决办法]
第二题改为这样:

C/C++ code
void stxc( char ** p){  *p = (char *)malloc(100);  strcpy(*p, "hello world");}int main(){  char * str = NULL;  stxc(&str);  printf("%s\n", str);  free(str);  return 0;}
[解决办法]
第二题函数可改为
void stxc( char &*p) //对指针得引用
{
p = (char *)malloc(100);
strcpy(p, "hello world");
}
[解决办法]
探讨
引用:
第二题改为这样:

C/C++ code
void stxc( char ** p)
{
*p = (char *)malloc(100);
strcpy(*p, "hello world");
}

int main()
{
char * str = NULL;
stxc(&str);
printf("%s\……

读书人网 >C++

热点推荐