va_start问题 [英] va_start problem

查看:76
本文介绍了va_start问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以下程序的行为如下:


1 #include< stdlib.h>

2 #include< stdarg.h> ;

3 #include< stdio.h>

4

5 void comd(char * p,int a,...) ;

6 void f(无效);

7

8 main(){

9 f() ;

10}

11

12 void f(无效){

13 char * p =(char *)malloc(10);

14 comd(p,5,''p'',''a'','d'',''h'',''j' ');

15

16}

17 void comd(char * p,int a,...)

18 {

19 int i;

20

21 char * q;

22

23 va_list ap;

24 va_start(ap,a);

25 q = p;

26 for( i = 0; i< = a; i ++)

27 {

28 * p ++ = va_arg(ap,char);

29}

30 printf(" \ np =%s \ n",q);

31 va_end(ap);

32}

33

以上程序给出输出为

p = padhj

现在如果我将第14行更改为

comd( p,4,''p'',''a'',''d'','h'',''j'');

那么输出也是一样的?

怎样va_start如何准确定位起点?

在ap?

解决方案

gyan写道:


为什么以下程序的行为如下:

1 #include< stdlib.h>
2 #include< ; stdarg.h>
3 #include< stdio.h>
4
5 void comd(char * p,int a,...);
6 void f (无效);
7
8 main(){
9 f();
10}
11
12 void f(void){
13 char * p =(char *)malloc(10);
14 comd(p, 5,''p'','''','''',''h'',''j'');
15
16}
17无效comd(char * p,int a,...)
18 {
19 int i;
20
21 char * q;
22
23 va_list ap;
24 va_start(ap,a);
25 q = p;
26 for(i = 0; i< = a; i ++)


在这种情况下,你将从0到5,总计_6_项目。

那应该是i< a。

27 {
28 * p ++ = va_arg(ap,char);
29}
30 printf(" \ np =%s \\ \\ n,q);
31 va_end(ap);
32}
33
上面的程序输出为
p = padhj


实际上,输出在''j'之后包含一个额外的字符,因为

你跑到了列表的末尾。显然,无论发生什么事情,

最终都是一个不可打印的角色,而且你没有看到它的价值。

现在如果我将第14行改为
comd(p,4,''p'',''a',''d'',''h'',''j''); 怎样va_start如何在ap中准确找到起点?
blockquote>


-

+ ------------------------- + -------------------- + ----------------------------- +

| Kenneth J. Brody | www.hvcomputer.com | |

| kenbrody / at\spamcop.net | www.fptech.com | #include< std_disclaimer.h> |

+ ------------------------- + -------------- ------ + ----------------------------- +

不要 - 给我发邮件:< mailto:Th ************* @ gmail.com>


gyan写道:

为什么以下程序的行为如下:

1 #include< stdlib.h>
2 #include< stdarg.h>
3 #include< stdio.h>
4
5 void comd(char * p,int a,...);
6 void f(void);
7
8 main(){
int main(void){//阅读FAQ以了解为什么9 f();
返回0; //阅读常见问题以了解原因10}
11
12 void f(无效){
13 char * p =(char *)malloc(10);
//需要包含stdlib.h //阅读FAQ以了解原因

char * p = malloc(10); //阅读常见问题解答,了解为什么14 comd(p,5,''p'',''a',''d'',''h'',''j'');
15
16}
17 void comd(char * p,int a,...)
18 {
19 int i;
20
21 char * q;
22
23 va_list ap;
24 va_start(ap,a);
25 q = p;
26 for(i = 0; i< = a; i ++)
for(i = 0; i< a; i ++)//添加多个元素..

//你添加了+ 1 27 {
28 * p ++ = va_arg(ap,char);
29}
* p =''\'''; // null终止字符串30 printf(" \ np =%s \ n",q);
31 va_end(ap);
32}
33
以上程序给出输出为
p = padhj
现在,如果我将第14行更改为
comd(p,4,''p'',''a'',''d' ',''h'',''j'');
然后输出也一样?
如何在ap中确切地找到va_start起点?
blockquote>


gyan写道:

为什么以下程序的行为如下:
[....] va_start如何准确定位起点
在ap?


这不是你的问题。


没人知道你的程序为什么这样做;你没有理由

希望它能够运行。


这一行可能会导致你的程序中止:28 * p ++ = va_arg(ap,炭);
所有的参数都是整数。


这一行导致试图获得更多的参数而不是26(i = 0; i< = a; i ++)


并且你未能正确终止字符串意味着30 printf(" \ np =%s \ n,q);
会带来灾难性的后果。


将您的代码与以下内容进行比较,注意标有

的行/ * mha * /:


#include< stdlib.h>

#include< stdarg.h>

#include< stdio。 h>


void comd(char * p,int a,...);

#define BIGENOUGH 1024


int / * mha * / main()

{

char p [BIGENOUGH];

comd(p,5,' 'p'','''','''','''',''j'');

comd(p,4,''p'', '''','''','''',''j'');

返回0;

}

void comd(char * p,int a,...)

{

int i;

char * q; < br $>
va_list ap;


printf(" \ n comd用a =%d \ n",a调用);

va_start(ap,a);

q = p;

for(i = 0; i< / * mha * / a; i ++)

* p ++ = va_arg(ap,int); / * mha * /

* p = 0; / * mha * /

printf(" p =%s \ n",q);

va_end(ap);

}

comd调用a = 5

p = padhj


comd调用a = 4

p = padh


[OP代码跟随,那些令人讨厌的行号] 1 #include< stdlib.h>
2 #include< stdarg.h> ;
3 #include< stdio.h>
4
5 void comd(char * p,int a,...);
6 void f(void);
7
8 main(){
9 f();
10}
11
12 void f(无效){
13 char * p =(char *)malloc(10);
14 comd(p,5,''p'',''a'',''d'',''h'','' j'');
15
16}
17 void comd(char * p,int a,...)
18 {
19 int i;
20
21 char * q;
22
23 va_list ap;
24 va_start(a p,a);
25 q = p;
26 for(i = 0; i< = a; i ++)
27 {
29}
30 printf (\ np =%s \ n,q);
31 va_end(ap);
32}
33



Why it is that following program behaving like this:

1 #include <stdlib.h>
2 #include <stdarg.h>
3 #include <stdio.h>
4
5 void comd(char *p,int a, ...);
6 void f(void);
7
8 main(){
9 f();
10 }
11
12 void f(void) {
13 char *p = (char *)malloc(10);
14 comd(p,5,''p'',''a'',''d'',''h'',''j'');
15
16 }
17 void comd(char *p,int a, ...)
18 {
19 int i;
20
21 char *q;
22
23 va_list ap;
24 va_start(ap,a);
25 q=p;
26 for(i=0;i<=a;i++)
27 {
28 *p++ = va_arg(ap,char);
29 }
30 printf("\n p = %s\n",q);
31 va_end(ap);
32 }
33
above program give output as
p = padhj
Now if i change line nos 14 to
comd(p,4,''p'',''a'',''d'',''h'',''j'');
then also output is same?
how how va_start exactly locate startingg point
in ap?

解决方案

gyan wrote:


Why it is that following program behaving like this:

1 #include <stdlib.h>
2 #include <stdarg.h>
3 #include <stdio.h>
4
5 void comd(char *p,int a, ...);
6 void f(void);
7
8 main(){
9 f();
10 }
11
12 void f(void) {
13 char *p = (char *)malloc(10);
14 comd(p,5,''p'',''a'',''d'',''h'',''j'');
15
16 }
17 void comd(char *p,int a, ...)
18 {
19 int i;
20
21 char *q;
22
23 va_list ap;
24 va_start(ap,a);
25 q=p;
26 for(i=0;i<=a;i++)
In this case, you will go from 0 though 5, for a total of _6_ items.
That should be "i < a".
27 {
28 *p++ = va_arg(ap,char);
29 }
30 printf("\n p = %s\n",q);
31 va_end(ap);
32 }
33
above program give output as
p = padhj
Actually, the output included an extra character after the ''j'', as
you ran off the end of your list. Apparently, whatever happened to
be there ended up being a non-printable character, and you didn''t
see it.
Now if i change line nos 14 to
comd(p,4,''p'',''a'',''d'',''h'',''j'');
then also output is same?
how how va_start exactly locate startingg point
in ap?



--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>


gyan wrote:

Why it is that following program behaving like this:

1 #include <stdlib.h>
2 #include <stdarg.h>
3 #include <stdio.h>
4
5 void comd(char *p,int a, ...);
6 void f(void);
7
8 main(){ int main(void) { // Read FAQ to know why 9 f(); return 0; // Read FAQ to know why 10 }
11
12 void f(void) {
13 char *p = (char *)malloc(10); // need to include stdlib.h // Read FAQ to know why
char *p = malloc(10); // Read FAQ to know why 14 comd(p,5,''p'',''a'',''d'',''h'',''j'');
15
16 }
17 void comd(char *p,int a, ...)
18 {
19 int i;
20
21 char *q;
22
23 va_list ap;
24 va_start(ap,a);
25 q=p;
26 for(i=0;i<=a;i++) for(i=0;i<a;i++) // to add a number of elements..
// you were adding a+1 27 {
28 *p++ = va_arg(ap,char);
29 } *p = ''\0''; // null terminate the string 30 printf("\n p = %s\n",q);
31 va_end(ap);
32 }
33
above program give output as
p = padhj
Now if i change line nos 14 to
comd(p,4,''p'',''a'',''d'',''h'',''j'');
then also output is same?
how how va_start exactly locate startingg point
in ap?



gyan wrote:

Why it is that following program behaving like this: [....] how how va_start exactly locate startingg point
in ap?
That is not your problem.

No one knows why your program does as it does; you have no reason to
expect it to run at all.

This line might well cause your program to abort: 28 *p++ = va_arg(ap,char); All the arguments are ints.

This line leads to trying to get more arguments than there are 26 for(i=0;i<=a;i++)
and your failure to terminate the string properly means that 30 printf("\n p = %s\n",q); leads to pssibly disastrous outcomes.

Compare your code to the following, paying attention to the lines marked
with /* mha */:

#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>

void comd(char *p, int a, ...);
#define BIGENOUGH 1024

int /* mha */ main()
{
char p[BIGENOUGH];
comd(p, 5, ''p'', ''a'', ''d'', ''h'', ''j'');
comd(p, 4, ''p'', ''a'', ''d'', ''h'', ''j'');
return 0;
}
void comd(char *p, int a, ...)
{
int i;
char *q;
va_list ap;

printf("\n comd called with a = %d\n", a);
va_start(ap, a);
q = p;
for (i = 0; i < /* mha */ a; i++)
*p++ = va_arg(ap, int); /* mha */
*p = 0; /* mha */
printf("p = %s\n", q);
va_end(ap);
}
comd called with a = 5
p = padhj

comd called with a = 4
p = padh

[OP''s code follows, with those obnoxious line numbers] 1 #include <stdlib.h>
2 #include <stdarg.h>
3 #include <stdio.h>
4
5 void comd(char *p,int a, ...);
6 void f(void);
7
8 main(){
9 f();
10 }
11
12 void f(void) {
13 char *p = (char *)malloc(10);
14 comd(p,5,''p'',''a'',''d'',''h'',''j'');
15
16 }
17 void comd(char *p,int a, ...)
18 {
19 int i;
20
21 char *q;
22
23 va_list ap;
24 va_start(ap,a);
25 q=p;
26 for(i=0;i<=a;i++)
27 {
29 }
30 printf("\n p = %s\n",q);
31 va_end(ap);
32 }
33



这篇关于va_start问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆