Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assign argc & argv within program

Status
Not open for further replies.

taz75

Programmer
Apr 17, 2001
15
GB
I am writing some unit tests and would like to be able to assign values to argc & argv in the code, rather than pass them through the command line.

I have tried
Code:
int argc = 4;
char* argv[] = {"prog_main", "str1=data", "ps=Y", "file=hj.srt"};

prog_main(argc, argv);

However within prog_main it uses
Code:
memset(argv[i],'\0',(int)strlen(argv[i]));

which gives the following error:

First-chance exception at 0x00615505 in unit_test.exe: 0xC0000005: Access violation writing location 0x006551a8.

Any thoughts?

Thanks
Laura
 
Shouldn't you use:

memset([red]&[/red]argv,'\0',(int)strlen(argv));

?

Lee
 
Yes, you can't write to string constants, they're read-only.

Writing to argv[] anyway is probably a bad idea anyway.



--
 
The 2nd (optional!) parameter of the main is an array of pointers to char (i.e. array of pointers to char arrays). The last element (with index argc from the 1st optional parameter of main) must be (char*)0 (famous NULL). The 1st element must refer to exe module path (the name used to invoke the program or ""). That's all.

If your unit test calls tested module via prog_main entry point, no matter what names have 1st and 2nd arguments of prog_main (it's not usual main, it's unit test startded with main). So make an usual char* array, initialize its elements and pass to prog_main - for example:
Code:
char* testarg[] =
{
  "prog_main".
  "sttr1=data",
  "ps=Y",
  "file=hj.srt",
  0
};
...
prog_main(sizeof testarg/sizeof testarg[0],testarg);
And go on...
 
I have now defined a char array for each input parameter string and then populated the argv array with the pointers to these and that has worked.

Code:
int argc = 5;
char p0[50] = "\0";
char p1[50] = "str1=data";
char p2[50] = "ps=Y";
char p3[50] = "file=hj.srt";
char p4[50] = "\0";
char* argv[] = {p0, p1, p2, p3, p4};

prog_main(argc, argv);

The
Code:
memset(argv[i],'\0',(int)strlen(argv[i]));
remains the same.

Thanks for your help,
Laura
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top