1

I've an issue with passing argv to a function. Basically, I want to pass &argv[1] to the function and work from that. However, I don't understand why the following does work:

    void fun(char**&argv, const int& argc);

    int main(int argc, char** argv)
    {
            fun(++argv, --argc);
            return 0;
    }

yet the following doesn't work:

    void fun(char**&argv, const int& argc);

    int main(int argc, char** argv)
    {
            fun(&argv[1], --argc);
            return 0;
    }
Jean-Luc
  • 3,563
  • 8
  • 40
  • 78
  • 2
    "doesn't work" is like a person trying to enter a car by rubbing himself against a fender, and then unable to articulate the specifics when he reports his failure. probably your code #2 does not compile. but what the heck does "doesn't work" mean for your code #1? wtf? – Cheers and hth. - Alf Sep 28 '11 at 06:36
  • Why exactly do you want to pass around references to pointers? – arne Sep 28 '11 at 06:37
  • I think you mean why the first one does work and the second one doesn't. – Keith Irwin Sep 28 '11 at 06:45
  • 1
    Why would you pass it by reference? Passing an `int` by const reference is already (practically) meaningless. – UncleBens Sep 28 '11 at 06:45
  • 1
    [Do not pass built-ins per `const` reference.](http://stackoverflow.com/questions/2139224/how-to-pass-objects-to-functions-in-c/2139254#2139254) – sbi Sep 28 '11 at 06:49

2 Answers2

1

Prototype:

void myfunc (char **argv, int argc)

Example usage:

  myfunc (argv, argc)

If you wanted to pass ONLY one string (argv[1]):

void myfunc (char *arg, int argc)
...
myfunc (argv[1], argc)

And if you wanted to pass an array of strings starting at argv[1]:

void myfunc (char **arg, int argc)
...
myfunc (&argv[1], argc)
paulsm4
  • 114,292
  • 17
  • 138
  • 190
1

The error I get when I try this is that argv[1] is a temporary value and you can't assign that to a non-const reference. If you instead do this:

void fun( char** const&argv, const int& argc);

Then either ++argv or &argv[1] will work.

Obviously, if you planned to actually change the value of argv itself, this won't work, but I presume that you aren't planning to do this since you're not passing a reference to argv itself but rather to the memory location of the second entry in argv.

The reason that the first one works and the second doesn't is that the ++ operator at the front modifies the variable and then returns not just its value after the modification, but the variable itself. As a result, the variable can then be passed into fun as a reference. But the &argv[1] is just the value of a memory location. It's a computed value, not a variable in its own right, so it can't be passed in as a non-const reference.

If you're planning to try to change the value of argv itself (to point it to a different array of character arrays), then &argv[1] doesn't work for that anyway, since it's not argv. ++argv, however, is argv, just after it's had its value adjusted.

You could also handle all of this without references at all, which would make things much easier, but that's up to you. If it were my code, though, I wouldn't be using references here. The const reference to an int could just be passed by value (which would be easier) and passing a reference to a double pointer doesn't save you any null checking (as it could still well be null). So there isn't much point to it. But if you like your code to be complex, feel free.

Keith Irwin
  • 5,628
  • 22
  • 31
  • 1
    If he's not planning to change the pointer, wouldn't it then be more meaningful to take the arguments by value? – UncleBens Sep 28 '11 at 07:00
  • It would probably be a better idea, yes. I wasn't necessarily trying to find the ideal code for the situation, rather I was just trying to find minimal changes which would make the second example work. This is definitely not necessarily the best thing to actually do in your code. But I didn't want to argue with the original poster about what he should do in his code. – Keith Irwin Sep 28 '11 at 07:04
  • I've added additional notes to this effect and more explanation to answer the basic question of why one works and the other doesn't. – Keith Irwin Sep 28 '11 at 07:18