Remove elements from an array

I'm writing a brainf*ck interpreter (bored) in C and I've written myself into a corner. My main function branches off in one of two directions - interactive mode or script mode. For the script mode, it passes argc and argv to a function. In the script function, argv[0] is ignored (as argv[0] will never point to a brainf*ck script). However, I want to add a feature to interactive mode to allow the user to load a file:
1
2
3
4
5
6
/* Load file */
case 'l':
	char* path = get_input(PS2 " ");
	script(1, &path);
	free(path);
	break;

The problem here is that in the script() function, argv[0] is ignored, so nothing will happen to the loaded script.

What I was thinking of doing was instead of having script() skip argv[0], I would have it removed in the main() function.

How would I go about removing the first element of the argv array?
Last edited on
You could make a single version of the code that takes it's input from a stream; then you pass stdin (zero) for interactive mode, and the handle returned by open for interactive mode, and even a socket for server mode.
I'm not following you. What does argv[0] have to do with allowing the user to load a file? And if ignoring argv[0] causes a problem in your script function -- why are you ignoring it?


At any rate, if you want to remove it -- the easiest way would just to be pass it a pointer to argv[1] instead of argv[0]:

1
2
3
4
5
6
7
8
9
10
11
12
void somefunc(char* args)
{
  //...
}

int main(int argc,char* argv[])
{
  // somefunc(argv);  // instead of this

  somefunc(argv + 1); // do this
  somefunc(&argv[1]); // or this
}
@kbw,
That's an excellent idea!

@Disch,
My main function was passing argc and argv to script(), which would iterate over argv to find scripts to run (passed on the command-line). It ignored argv[0] because argv[0] would point back to the interpreter, which obviously is not a brainf*ck script. The problem with that was that when I wanted to allow the user to load a script during interactive mode, I would just pass a pointer to the path of the file to the script() function, which would result in the script not being run (since argv[0] was skipped). I know it's confusing. Your method would solve that (and I can't believe I didn't think of it, since I've used it before) but I'm going to go with kbw's suggestion.
Topic archived. No new replies allowed.