重邮信科笔试题

xiaowai0219 2010-10-10 04:37:25
void fun(int n,char *str1,char *str2,...);
用C写一函数打印n个字符串,原型如上。不能用va系列的宏定义,需从编译角度考虑。
...全文
492 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
madgene 2010-10-11
  • 打赏
  • 举报
回复
还有个疑问
如何保证first++之后的地址就是指向堆栈中&n+1之后的地址的地址?
也就是说,*first++如何与后面的参数对应?
huhacn 2010-10-11
  • 打赏
  • 举报
回复
果然很xx啊
yg2362 2010-10-11
  • 打赏
  • 举报
回复
这个还是要根据编译器的特点,是从左到右压参数还是从右到左。我的机器上是前者。代码如下
void fun(int n,...)
{
for ( int i = 0,j = 1;i < n;++i,++j )
{
char **tmp = (char **)(&n+j);
printf("%s ",*tmp);
}
printf("\r\n");
}

int main()
{
fun(3,"hello1","hello2","hello3");
return 1;
}
羽飞 2010-10-11
  • 打赏
  • 举报
回复
二楼神人,对程序理解太透彻了,佩服
libinfei8848 2010-10-11
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 madgene 的回复:]
还有个疑问
如何保证first++之后的地址就是指向堆栈中&n+1之后的地址的地址?
也就是说,*first++如何与后面的参数对应?
[/Quote]
lz题目中输出
N个字符串,就已经确定好了不定参数的类型,所以这个地址也是能够确定的,在这里就是一个字节
libinfei8848 2010-10-11
  • 打赏
  • 举报
回复
不同编译器压栈顺序不一致,会造成隐患的
飞雪是这样的么?
madgene 2010-10-10
  • 打赏
  • 举报
回复
1楼强,经12楼一提示,也看懂了。多谢楼主问的好问题
tiantangniao232 2010-10-10
  • 打赏
  • 举报
回复
利用的函数参数压栈顺序吧,呵呵
具体实现应该跟编译器函数参数压栈顺序有关,从左到右还是从右到左
harderman 2010-10-10
  • 打赏
  • 举报
回复
飞雪 ,给解释下吧
saishow 2010-10-10
  • 打赏
  • 举报
回复
标记一下...
harderman 2010-10-10
  • 打赏
  • 举报
回复
那个叫"飞雪"的却是厉害.
S_zxing 2010-10-10
  • 打赏
  • 举报
回复
我刚才在论坛找到的一篇文章,我发下来不算不尊重版权吧,如果有,深表歉意……
http://blog.csdn.net/arong1234/archive/2008/05/18/2456455.aspx
S_zxing 2010-10-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 baihacker 的回复:]

C/C++ code
#include <stdio.h>
void fun(int n, ...)
{
char** first = (char**)(&n + 1);
for (; n--; puts(*first++));
}
int main()
{
fun(3,"baihcker","csdn","飞雪");
return 0;
}
[/Quote]
这个能不能解释一下,或者推荐一下这方面的资料……
wing_0706 2010-10-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 baihacker 的回复:]
C/C++ code
#include <stdio.h>
void fun(int n, ...)
{
char** first = (char**)(&n + 1);
for (; n--; puts(*first++));
}
int main()
{
fun(3,"baihcker","csdn","飞雪");
return 0;……
[/Quote]
飞雪大神 能解释一下吗。。。谢谢啦、。。
zecard_fu 2010-10-10
  • 打赏
  • 举报
回复
顶~ 回复内容太短了!
xiaowai0219 2010-10-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 baihacker 的回复:]
C/C++ code
#include <stdio.h>
void fun(int n, ...)
{
char** first = (char**)(&n + 1);
for (; n--; puts(*first++));
}
int main()
{
fun(3,"baihcker","csdn","飞雪");
return 0;……
[/Quote]
这个不错,只是当时就是想不起来。嘿嘿
djjlove_2008 2010-10-10
  • 打赏
  • 举报
回复
题目意思没太看懂,呵呵。
这就是不定函数参数问题吧。

const int N = 100;
string[N];
void fun(int n,...)
{
for(int i = 0; i < n; ++i)
cout << string[i] << endl;
}
baihacker 2010-10-10
  • 打赏
  • 举报
回复
#include <stdio.h>
void fun(int n, ...)
{
char** first = (char**)(&n + 1);
for (; n--; puts(*first++));
}
int main()
{
fun(3,"baihcker","csdn","飞雪");
return 0;
}

69,369

社区成员

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

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