问:所以我的问题是为什么 strcpy 不能抓取这个 {argv[1]} 字符并将其写入 buf?
你可以。您可能遇到的唯一问题是 ifargc
小于 2,或者 argv[1] 大于 255 字节(加上字符串终止字符)。
问:另外,为什么 argc = 1?
在大多数系统上,argv[] 数组的布局具有相同的布局。例如,假设从命令行执行了一个程序:
>./myprog cookie monster
argv[0] Contains the path where the executing program resides in the filesystem.
So the actual value is something like: '/home/mahonri/test/test'
This value is provided by the operating system.
argv[1] Will contain the string: 'cookie'
This value is provided (optionally) by the user.
argv[2] will contain the string: 'monster'
This value is provided (optionally) by the user.
argc will be '3', because there are three argv elements; 0, 1 and 2.
在问题代码的情况下,如果argc是'1',那么只有argv[0]被初始化;如果代码随后尝试访问 argv[1] 或 argv[2],就会发生不可预知的事情。