[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Is it perfectly to pass argv[] this way to function ?



Hi Deepak,

"Deepak Kotian" <dkotian3@vsnl.net> writes:

> Hi,
> 
> Please check the code segment.
> /*************code start ****/
> #include <stdio.h>
> int func1(char **a_argv){
>  while (*a_argv!=3DNULL)
>  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=20
> on stack, so it works probably.
> Do anyone see any problem, if one passes argv[] this way ? Will there be =
> portability issues, like=20
> on other UNIX flavors,etc.

Why not just do:

#include <stdio.h>

int func1(char **argv) {
  while(**argv != NULL) {
    printf("%s\n", *(argv++));
  }
}

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

You are passing the pointer-pointer by value, but that's fine. The
only thing that is troubling is the (**argv != NULL) which assumes
that the last string is a NULL string (the first character is NULL),
as opposed to using the count (argc), which may not be the case (it's
not here). But this is certainly fine and standard C. Here's how I
would do it:

#include <stdio.h>

int func1(int argc, char **argv) {
  int i;
  for(i = 1; i < argc; i++) {
    printf("%s\n", *(argv + i));
  }
}

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


Elizabeth


-- 
To UNSUBSCRIBE, email to debian-user-request@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org



Reply to: