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

Recommended Answers

All 4 Replies

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.

Why do you want to do this?

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.