Redian新闻
>
Anybody help me on these questions?
avatar
Anybody help me on these questions?# Programming - 葵花宝典
s*e
1
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?
avatar
s*e
2
Ok. I give you my ans and please help me check them.
1. p = buff[0](actually buff[] is gone.) So if the memory that buff[] has is
relocated to some other program right before lvalue b can be assigned to point
to that memery space, we will not be able to get buff[0].
2. ???
3. Yes. But line 2002 should be "static char buff[8]" to return array buff[].
4. Yes it will be excuted.
5. ???
6. No. the reason is in 1. and 3.
7. char * anotherfunction(char*);
8. ???
9. select count(gid)
from tablename
gro
avatar
t*t
3

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

【在 s******e 的大作中提到】
: Ok. I give you my ans and please help me check them.
: 1. p = buff[0](actually buff[] is gone.) So if the memory that buff[] has is
: relocated to some other program right before lvalue b can be assigned to point
: to that memery space, we will not be able to get buff[0].
: 2. ???
: 3. Yes. But line 2002 should be "static char buff[8]" to return array buff[].
: 4. Yes it will be excuted.
: 5. ???
: 6. No. the reason is in 1. and 3.
: 7. char * anotherfunction(char*);

avatar
s*e
4
Thanks a lot, man. Since we have the same thoughts about Q2. and Q5. I will
take them as my final answers even I am still not sure of them.
About Q8. I am thinking two ways to solve it, but I am not sure if I get the Q
or not.
1. Use a struct as the return type. such as
struct A { char *a; char*b}; (The question here is not able to maintain the
return type of the function)
2. Another one is such as "char * getbuff(char *a);

is
point
buff[].
well.

【在 t****t 的大作中提到】
:
: 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.

avatar
t*t
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

【在 s******e 的大作中提到】
: Ok. I give you my ans and please help me check them.
: 1. p = buff[0](actually buff[] is gone.) So if the memory that buff[] has is
: relocated to some other program right before lvalue b can be assigned to point
: to that memery space, we will not be able to get buff[0].
: 2. ???
: 3. Yes. But line 2002 should be "static char buff[8]" to return array buff[].
: 4. Yes it will be excuted.
: 5. ???
: 6. No. the reason is in 1. and 3.
: 7. char * anotherfunction(char*);

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。