c*****o 发帖数: 178 | 1 1. q = b[0]; getbuf() 中没有buff的初始化部分代码。如果没有,q的值不定。
2. 不应该返回localvariable的地址,编译有warning。如果只想得到一个char值,可
以返回buff[]中的具体值。不知道怎么改写,这个返回地址本身就不对。
3. 不是,同2.
4. 可以执行。
5. 同2.
6. 如果一定要修改一个数组的值,不如传数组的reference作为getbuf()的一个
parameter
7. 在主程序里declare引用的函数。在b = getbuf (); 之前:char * getbuf ();
8. void getbuf(char*, char*); |
|
s******e 发帖数: 493 | 2 LINE Contains
50 char * b, q, *r;
200 b=getbuf();
201 q = *b;
212 r= anotherfunction(b);
213-300 /* we want to use ‘q’ and ‘r’ here*/
2000 char * getbuf()
2001 {
2002 char buff[8];
2003-2050 /* unspecified, buff defined here *./
2051 return (char *) buff;
2052 }
1. What will be in variable ‘q’ after line 201 is executed? Under what
conditions might this not be so?
2. Is there an alternative, but equivalent, way to write line 2000? If so,
what is it?
3. Is getbuf() a reasonable function? |
|
T*****J 发帖数: 193 | 3 【 以下文字转载自 Programming 讨论区 】
发信人: ThomasJ (JY), 信区: Programming
标 题: 问个面试的编程题目
发信站: BBS 未名空间站 (Sat Jan 2 23:23:23 2010, 美东)
现场让30分钟做完
50 char *b, q, *r;
200 b = getbuf ();
201 q = *b;
212 R = anotherfunction (b);
213-2003 /* we want to use 'q' and 'r' here */
2000 char * getbuf ()
2001 {
2002 char buff [8];
2003-2050 /* unspecified buff defined here */
2051 return (char*) buff;
2052 }
1. What will be in variable 'q' after line 201 is executed? Under
what conditions might this not be so?
2. Is t |
|
t****t 发帖数: 6806 | 4
basically right. i might add that, since buff[] is on stack, it is very
easy to get destroyed very quickly.
not very sure, probably he means char * getbuf(void)?
I would not call that reasonable. after adding static, it is ok, but still
not very good.
fine
not sure what he want. but 2051 is fine. however you don't need the cast.
probably you need an additional "char* getbuf()" before invoking it,
but it will probably pass the compile.
probably my english is so bad, but i don't understand the |
|
t****t 发帖数: 6806 | 5
basically right. i might add that, since buff[] is on stack, it is very
easy to get destroyed very quickly.
not very sure, probably he means char * getbuf(void)?
I would not call that reasonable. after adding static, it is ok, but still
not very good.
fine
not sure what he want. but 2051 is fine. however you don't need the cast.
probably you need an additional "char* getbuf()" before invoking it,
but it will probably pass the compile.
probably my english is so bad, but i don't understand the |
|
i****t 发帖数: 113 | 6 Can anyone give me some hints on the following questions? Thanks.
50 char *b, q, *r;
200 b = getbuf ();
201 q = *b;
212 R = anotherfunction (b);
213-2003
/* we want to use q and r here */
2000 char * getbuf ()
2001 {
2002 char buff [8];
2003-2050
/* unspecified buff defined here */
2051 return (char*) buff;
2052 }
1. What will be in variable q after line 201 is executed? Under what
conditions might this not be so?
2. Is there an alternative, but equivalent, way to wr |
|
W*******o 发帖数: 301 | 7 【 以下文字转载自 JobHunting 讨论区 】
发信人: WEKingpro (口味蛇), 信区: JobHunting
标 题: 问一道C++的题目。
发信站: BBS 未名空间站 (Sat Jan 6 21:09:12 2007)
char* getbuf() {
char buff[8]="abcdefg";
return (char*)buff;
}
void main() {
char* b, q;
b = getbuf();
q = *b;
printf("%c\n", q);
}
输出结果中,q除了等于a, 还有可能等于什么? |
|
s*******e 发帖数: 664 | 8 ☆─────────────────────────────────────☆
ThomasJ (JY) 于 (Sat Jan 2 23:23:23 2010, 美东) 提到:
现场让30分钟做完
50 char *b, q, *r;
200 b = getbuf ();
201 q = *b;
212 R = anotherfunction (b);
213-2003 /* we want to use 'q' and 'r' here */
2000 char * getbuf ()
2001 {
2002 char buff [8];
2003-2050 /* unspecified buff defined here */
2051 return (char*) buff;
2052 }
1. What will be in variable 'q' after line 201 is executed? Under
what conditions might this not be so?
2. Is there an alternative, but equiv |
|
e********e 发帖数: 12 | 9
must be void getbuf(char**, char**) |
|
k****f 发帖数: 3794 | 10 buff是在栈里的,退出getbuf,应该就不是有效的 |
|
b***y 发帖数: 2799 | 11 ☆─────────────────────────────────────☆
topbull (塞猪~最近比较衰) 于 (Tue Oct 18 01:52:00 2005) 提到:
char * getbuf()
{
char buff[8];
....
return (char *)buff;
}
☆─────────────────────────────────────☆
campos (Gentoo 真土) 于 (Tue Oct 18 02:03:46 2005) 提到:
returned pointer contains nothing
accessing it will cause access violation
☆─────────────────────────────────────☆
xingxing (星星) 于 (Tue Oct 18 02:27:39 2005) 提到:
that pointer is just on the stack.
I think most likely there won't |
|
x***y 发帖数: 45 | 12 char * b, q, *r;
b=getbuf();
q = *b;
What will be in variable ‘q’? Under what conditions might this not be so? |
|
|