f********1 发帖数: 228 | 1 A question I encountered during the interview. Could anyone give me some
idea.
The key issue is I was asked to modify line 500, which is the function
declaration...
Is there a different way to write line 500 which preserves the same
effective prototype? If so, what is it?
Line in file
Code:
30 int * someIDs, theFirst, *r;
110 someIDs =GetSomeIDs(); /* defined below */
111 theFirst = someIDs [0];
112 r= ReorderIDs(someIDs);
113-150 /* we want to use ‘theFirst’ and ‘r’ here*/
499 /*-------- GetSomeIDs-----*/
500 int * GetSomeIDs()
501{
502 int ids[8];
503-550 /* The ids are defined here */
551 return ids;
552} |
r*********r 发帖数: 3195 | 2 returning a pointer that points to local objects?
that's called a dangling pointer. |
b***i 发帖数: 3043 | 3 For interview, use keyword static before the array
【在 f********1 的大作中提到】 : A question I encountered during the interview. Could anyone give me some : idea. : The key issue is I was asked to modify line 500, which is the function : declaration... : Is there a different way to write line 500 which preserves the same : effective prototype? If so, what is it? : Line in file : Code: : 30 int * someIDs, theFirst, *r; : 110 someIDs =GetSomeIDs(); /* defined below */
|
f********1 发帖数: 228 | 4 Sorry cannot type Chinese here.
I understand this function is wrong and the question asks about how to
correct it on line 500 while maintaining the same effective prototype.
My previous solution is to modify line 502 using static variable, but the
interviewer said it's incorrect... |
d****i 发帖数: 4809 | 5 You can just modify line 502 as
int *ids = (int *) malloc(sizeof(int)*8);
and then don't forget to free memory in the caller.
【在 f********1 的大作中提到】 : Sorry cannot type Chinese here. : I understand this function is wrong and the question asks about how to : correct it on line 500 while maintaining the same effective prototype. : My previous solution is to modify line 502 using static variable, but the : interviewer said it's incorrect...
|
f********1 发帖数: 228 | 6 Yes. That's why I said this question is kind of weird. It asks to modify
line 500 which I have no idea how to modify that. |
d****n 发帖数: 1637 | 7 #define GetSomeIDs() ( int ret[8]; memcpy(ret, _getSomeIDs()); {ret;} )
int * _getSomeIDs(){
//... previous definition
} |
m***x 发帖数: 20 | 8 inline int * GetSomeIDs() |
d****n 发帖数: 1637 | 9 thats what I figured out last night.
【在 m***x 的大作中提到】 : inline int * GetSomeIDs()
|
X****r 发帖数: 3557 | 10 which is wrong
inline is just a hint to the compiler
【在 d****n 的大作中提到】 : thats what I figured out last night.
|
b***i 发帖数: 3043 | 11 真是这样,面试人员静问一些回字有几种写法这样的犄角旮旯问题。
【在 X****r 的大作中提到】 : which is wrong : inline is just a hint to the compiler
|
d****n 发帖数: 1637 | 12 your solutions?
【在 X****r 的大作中提到】 : which is wrong : inline is just a hint to the compiler
|