G
gyan
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?
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?