紧急求教高手一个C的面试题

hgxinyu 2010-09-20 10:07:20
针对下面这段程序,附上我的解答,帮我看看那里出错了
要用C语言,不能用C++

Line in file Contents
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 }

1。 第500行能有其他写法么,要保持同样效果的函数原型
我想不出有什么好的方法能够只改这一行,请大牛指教

2. 改正GetSomeIDs()函数,为他加以下功能。

新函数要求:

a) 保持相同的返回类型 “int *”

b) 对它的呼叫函数,提供一个能使用的aliasID结构的指针数列,这个数列的长度是由GetNumberOfAliases()函数决定。

IN ADDITION to its regular function return, provide to its calling functions a usable array of pointers to aliasID structures. The length of this array of pointers is returned by a call to GetNumberOfAliases(), which you may call only from within GetSomeIDs().

c) 使用以下定义的结构和函数

typedef struct {
char* alias; /* '\0'-terminated C string */
int specific_id;
} aliasID;


/* How many structures should be pointed to by the array
*/
int GetNumberOfAliases(void);

/* Get a pointer to the next structure. The structure itself
* will be filled with data.
* Caller is responsible for the cleanup of the returned structure
* and its content. The latter are allocated in
* dynamic memory.
*/
aliasID * GetNextAlias(void);

Do NOT(!) use "C++" syntax or language contructs.
This should be written in plain “C”.
Use good programming practice, as much as these instructions allow.


int* GetSomeIDs(aliasID* aID[],int &sz){
sz=GetNumberOfAliases();
int *ids=(int*)malloc(sizeof(int)*8);
/* The ids are defined here */
aID=(aliasID**)malloc(sizeof(aliasID*)*sz);
for(int i=0;i<sz;i++){
aID[i]=GetNextAlias();
free(aID[i]->alias);
}
return ids;
}


3 .写一个函数去调用上面定义的 GetSomeIDs(),打印出它返回的数据,清理动态分配的内存


For both (7), and (8), above, DO NOT use "C++" syntax or language contructs. These should be written in plain “C”.
Use good programming practice, as much as these instructions allow.

void ProcessIDs(){

aliasID** aID=0;
int size=0;
int* ID=GetSomeIDs(aID,size);
for(int i=0;i<8;i++)
printf("%d\n",ID[i]);

for(int i=0;i<size;i++){
printf("aID[%d]: string=%s specific_id=%d\n",i,aID[i]->alias,aID[i]->specific_id);
}

free(ID);
for(int i=0;i<size;i++){
free(aID[i]->alias);
}
free(aID);
}


...全文
466 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
木有看懂要做个什么,题目不清楚!我太肤浅
zzlhw2005 2010-09-22
  • 打赏
  • 举报
回复
路过,看看
d741963250 2010-09-22
  • 打赏
  • 举报
回复
还真是,不能在for语句里面定义变量,C是先把所以的变量都定义了,然后开始执行语句
wesweeky 2010-09-21
  • 打赏
  • 举报
回复
C 哎 没学好额
乐CC 2010-09-21
  • 打赏
  • 举报
回复
int* GetSomeIDs(aliasID** aID,int* sz)
{
int i; //VC6.0不能将定义写在for里面,先放这里,还有要将所有定义语句写在其他语句前面,所以把下面两句换了个顺序.

int* ids = (int *)malloc(8 *sizeof(int));

*sz = GetNumberOfAliases();

*aID = (aliasID*)malloc((*sz)*sizeof(aliasID)); //aiD是个结构体数组,应该这样申请空间.

for(i=0;i<(*sz);i++){
aID[i]=GetNextAlias();
free(aID[i]->alias); //我不知道这句是干什么了
}
return ids; //这里是不安全的,申请的空间会被销毁.
}
乐CC 2010-09-21
  • 打赏
  • 举报
回复
*sz=GetNumberOfAliases();
这个能通过编译么?
sz是个数组名,虽然它可以当指针使用,但这样是不合法的,除非你传入的就是指针.
hurricane880 2010-09-21
  • 打赏
  • 举报
回复
GetSomeIDs
我怎么觉得这个函数的实现是错误的
返回一个指向被销毁的空间的指针。可以么?
goodluckyxl 2010-09-21
  • 打赏
  • 举报
回复
没看懂撒
定义局部数组放会 你前面还是加个static吧
sanbailiushiliuye 2010-09-21
  • 打赏
  • 举报
回复
int* GetSomeIDs(aliasID** aID,int sz[]){
*sz=GetNumberOfAliases();/*这行有问题吧*/
int* ids = (int *)malloc(8 *sizeof(int))

aID=(aliasID**)malloc((*sz)*sizeof(aliasID*));
for(int i=0;i<(*sz);i++){
aID[i]=GetNextAlias();
free(aID[i]->alias);
}
return ids;
}
hgxinyu 2010-09-20
  • 打赏
  • 举报
回复
不好意思,ls好像有点文不对题,如果觉得题目长的话,帮我看看我的code吧

typedef struct {
char* alias;
int specific_id;
} aliasID;


aliasID * GetNextAlias(){
/* 返回下一个指针*/
}

int GetNumberOfAliases(){
/* 返回个数*/
}

int* GetSomeIDs(aliasID** aID,int sz[]){

*sz=GetNumberOfAliases();
int* ids;
ids=(int*)malloc(8*sizeof(int));
int i=0;

aID=(aliasID**)malloc((*sz)*sizeof(aliasID*));
for(i=0;i<(*sz);i++){
aID[i]=GetNextAlias();
free(aID[i]->alias);
}
return ids;
}

void ProcessIDs(){
aliasID** aID=0;
int size=0;
int i=0;
int* ID=GetSomeIDs(aID,&size);
for(i=0;i<8;i++)printf("%d\n",ID[i]);

for(i=0;i<size;i++){
printf("aID[%d]: string=%s specific_id=%d\n",i,aID[i]->alias,aID[i]->specific_id);
}

free(ID);
for(i=0;i<size;i++){
free(aID[i]->alias);
}
free(aID);
}

lly212 2010-09-20
  • 打赏
  • 举报
回复
我很菜 看着这么长心里也烦
我回答下第一题吧 你返回的栈地址有两种改法
1 申请足够大 8 ------> 1000000
下次即使覆盖也不会 覆盖到你要的数据

2 malloc 这个是堆内存 但是要记得free啊
willabc 2010-09-20
  • 打赏
  • 举报
回复
学习了!
hgxinyu 2010-09-20
  • 打赏
  • 举报
回复
汗死,才知道C语言不用引用。大牛帮看看下面code怎么还出错?

int* GetSomeIDs(aliasID** aID,int sz[]){
*sz=GetNumberOfAliases();
int* ids = (int *)malloc(8 *sizeof(int))

aID=(aliasID**)malloc((*sz)*sizeof(aliasID*));
for(int i=0;i<(*sz);i++){
aID[i]=GetNextAlias();
free(aID[i]->alias);
}
return ids;
}
hgxinyu 2010-09-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 eclipse_2 的回复:]

int* ids = (int *)malloc(8 *sizeof(int))
[/Quote]

就是在这编译出问题,C语言不熟啊,
这是什么意思呢?好像改了也没用啊
小魔菇 2010-09-20
  • 打赏
  • 举报
回复
int* ids = (int *)malloc(8 *sizeof(int))
Csuxiaowu 2010-09-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hgxinyu 的回复:]

引用 1 楼 csuxiaowu 的回复:

第一题可以把返回值放到形参中


这不止修改一行的吧
[/Quote]
没看全 不好意思 这我就不知道了 等待高手来 ---
hgxinyu 2010-09-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 csuxiaowu 的回复:]

第一题可以把返回值放到形参中
[/Quote]

这不止修改一行的吧
Csuxiaowu 2010-09-20
  • 打赏
  • 举报
回复
第一题可以把返回值放到形参中

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧