0

Hi
Im rather new to this but I want to ask how to re-define the array
argv[] after the program main(int argc, char *argv[]) have been called?

example

main(int argc, char *argv[])

//assume the program is called by: program
//this would give argc=1 and argv[0]= program

//but know I want to reddefine the argv inside
//the main() after the program have been called to the
//following e.g.

argv={"aa", "bb"};

//so when Im using e.g. argv[1] it should give bb,
//argv[1][0] should give b
//despite the program was called by program and not by program aa bb

grateful for detailed answer and examples

3
Contributors
4
Replies
6
Views
7 Years
Discussion Span
Last Post by Narue
0

argv is for giving arguments to your program. You're wanting to return arguments so:

int main(int argc, char **argp)
{
	// create a new 2D array to return value from
	int **ret;
	
	// dynamically allocate memory here (AKA malloc)
	
	// return our ret array
}

This should start you off in the right direction.

0

The point is that I dont want to return the **ret array
but instead use a modified **argv, (your **argp) array which has been
extended by some elements.

0

The argv array is writable, but the size of the array and the size of each element are set in stone. Why not just re-create argv in another variable then use that variable?

int main(int argc, char **argv)
{
    char **temp = malloc(2 * sizeof *temp);
    int i;

    /* No error checking for brevity */
    temp[0] = "aa";
    temp[1] = "bb";

    /* Optional if you really want to use the argv identifier */
    argv = temp;

    /* ... */

    free(temp);
}

I still fail to see why you would want to change argv to something completely different.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.