由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - A weird C programming language--Return a pointer to array
相关主题
[合集] C coding problemsXSL question
why int** cannot convert to const int** ?Why do I need to use "plain" pointer?
a c++ question一个简单的小问题
Why this is a dangling pointer请问可以这样定义struct吗?
c ptr question how to do it ?=======Problem – coding in c++
请教大家一道C的面试题is smart_ptr really that good?
what's wrong with this C++ code?[合集] pointer in C
为啥允许这样的const设计pointer overflow
相关话题的讨论汇总
话题: getsomeids话题: someids话题: int话题: return话题: line
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
pointer overflowc ptr question
about STL functor and function pointers请教大家一道C的面试题
what's the purpose of pointer to pointers?what's wrong with this C++ code?
C++ Q05: pointer to constant variable为啥允许这样的const设计
[合集] C coding problemsXSL question
why int** cannot convert to const int** ?Why do I need to use "plain" pointer?
a c++ question一个简单的小问题
Why this is a dangling pointer请问可以这样定义struct吗?
相关话题的讨论汇总
话题: getsomeids话题: someids话题: int话题: return话题: line