f********1 发帖数: 228 | 1 【 以下文字转载自 Programming 讨论区 】
发信人: falcon8241 (falcon), 信区: Programming
标 题: A weird C programming language--Return a pointer to array
发信站: BBS 未名空间站 (Wed May 29 13:54:54 2013, 美东)
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} |
c******y 发帖数: 14 | 2 这么改?
void GetSomeIDs(int*& x){
int ids[8];
/* The ids are defined here */
x = ids;
} |
S**I 发帖数: 15689 | 3 返回指向局部变量的指针,这个函数压根就是错的嘛。
【在 f********1 的大作中提到】 : 【 以下文字转载自 Programming 讨论区 】 : 发信人: falcon8241 (falcon), 信区: Programming : 标 题: A weird C programming language--Return a pointer to array : 发信站: BBS 未名空间站 (Wed May 29 13:54:54 2013, 美东) : 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?
|
o****d 发帖数: 2835 | 4 ids是局部变量 在栈空间中
函数返回就没了
可以用malloc来动态分配这个数组
【在 f********1 的大作中提到】 : 【 以下文字转载自 Programming 讨论区 】 : 发信人: falcon8241 (falcon), 信区: Programming : 标 题: A weird C programming language--Return a pointer to array : 发信站: BBS 未名空间站 (Wed May 29 13:54:54 2013, 美东) : 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?
|
f********1 发帖数: 228 | 5 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... |
H****r 发帖数: 2801 | 6 Unique pointer?
★ 发自iPhone App: ChineseWeb 7.7
【在 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...
|
c****9 发帖数: 164 | 7 Just change return type to integer array. int[]? which will return a copy
not a pointer.
【在 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 | 8 This results in a compiler error:
error: expected identifier or ‘(’ before ‘[’ token
【在 c****9 的大作中提到】 : Just change return type to integer array. int[]? which will return a copy : not a pointer.
|
f********1 发帖数: 228 | 9 I am not familiar with unique pointer. It seems that means you can only
modify the memory location with one pointer. I don't know how to apply it
here. Could you elaborate?
Also, will function pointer be helpful in anyway?
【在 H****r 的大作中提到】 : Unique pointer? : : ★ 发自iPhone App: ChineseWeb 7.7
|
H****r 发帖数: 2801 | 10 纯C的话只知道用malloc 或者 传入memory地址,unique pointer 是C++而且只适合特
殊情况,只有一个owner好像才行
★ 发自iPhone App: ChineseWeb 7.7
【在 f********1 的大作中提到】 : I am not familiar with unique pointer. It seems that means you can only : modify the memory location with one pointer. I don't know how to apply it : here. Could you elaborate? : Also, will function pointer be helpful in anyway?
|