An array and the command line arguments in C programming
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info: This program must be run at command prompt
To do: Testing the strcpy_s() function execution and storing input in an array
To show: Array and the command line arguments C program example
// Command line arguments
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
// reserve 5 byte of buffer, should allocate 8 bytes = 2 double words, to overflow, need more than 8 bytes...
// so, if more than 8 characters input by user, there will be access violation, segmentation fault etc
char mybuffer[5];
// a prompt how to execute the program...
if(argc < 2)
{
printf("strcpy_s() NOT executed....\n");
printf("Syntax: %s <characters>\n", argv[0]);
exit(0);
}
//copy the user input to mybuffer...
strcpy_s(mybuffer, 5, argv[1]);
printf("mybuffer content= %s\n", mybuffer);
printf("strcpy_s() executed...\n");
return 0;
}
Output example:
(run at the command prompt)
F:\vc2005project\myaddr\debug>myaddr
strcpy_s() NOT executed....
Syntax: myaddr <characters>
F:\vc2005project\myaddr\debug>myaddr text
mybuffer content= text
strcpy_s() executed...
F:\vc2005project\myaddr\debug>