Deepak Kotian wrote:

> Please check the code segment.
> /*************code start ****/
> #include <stdio.h>
> int func1(char **a_argv){
>  while (*a_argv!=NULL)
>  printf("\n%s", *a_argv++);
>  return(0);
> }
> 
> int main(int argc, char *argv[]){
>  func1(&argv[0]);
> }
> /*************code end ****/
> 
> This func1 function prints all the arguments with no problem, because
> main() is probably always on stack, so it works probably.
> Do anyone see any problem, if one passes argv[] this way ? Will there
> be portability issues, like on other UNIX flavors,etc.

I think the convention that argv[argc] == NULL is common enough that
this should be acceptable in practice, but I'm not sure it's actually in
the C89 standard, so it might not be justifiable in terms of good
engineering practices. It might be better to pass argc to func1 as well,
and use that to control the loop:

   void func1(int argc, char** argv) {
      while (--argc) {
         printf("\n%s, *argv++);
   }

   int main(int argc, char** argv) {
      func1(argc, argv);
      return 0;
   }

Note in this example that I take advantage of the fact that argv == &argv[0]
to simplify your invocation of func1.

Also, I have not actually compiled, much less tested, the above code, but
offhand, I believe it should be functionally equivalent to yours.

Craig

Attachment: pgp1ID2Fgds21.pgp
Description: PGP signature

Reply via email to