由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - A weird C programming language--Return a pointer to array (
相关主题
谁做过这些题目?发给我答案看看?想成为嵌入式程序员应知道的0x10个基本问题 zz
问道编程题问个链表反转的老题
Help, a tricky interview questioncopy link with random additional pointers
一道C面试题,谁试试C++ Q93 - Q95
c interview question谁对design pattern比较熟?
请教一个问题,发两个包子。reverse random pointers of a single linked list
[合集] export control licenseHow can one determine whether a singly linked list has a cycle?
大家google/amazon/facebook onsite白板coding的时候是不是都写出完整的code啊?一个简单的java题
相关话题的讨论汇总
话题: pointer话题: someids话题: return话题: getsomeids话题: ids
进入JobHunting版参与讨论
1 (共1页)
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?

1 (共1页)
进入JobHunting版参与讨论
相关主题
一个简单的java题c interview question
javaScript 教程?请教一个问题,发两个包子。
请教下copy list with random pointer[合集] export control license
js的oo和普通的oo有什么不同?大家google/amazon/facebook onsite白板coding的时候是不是都写出完整的code啊?
谁做过这些题目?发给我答案看看?想成为嵌入式程序员应知道的0x10个基本问题 zz
问道编程题问个链表反转的老题
Help, a tricky interview questioncopy link with random additional pointers
一道C面试题,谁试试C++ Q93 - Q95
相关话题的讨论汇总
话题: pointer话题: someids话题: return话题: getsomeids话题: ids