命令行参数的问题
题目是:编写一个程序,该程序用命名行参数读取两个二进制字符串,并打印对每个数使用~运算符的结果以及对这个数使用&、|和^运算符的结果。使用二进制字符串形式显示结果这是我的代码:
#include <stdio.h>
#define MAXSIZE 1000
int bintoi (char *);
void cal (int, int, char * []);
char * itobs (int, char *);
void show_bstr (const char *);
int main (int argc, char * argv[])
{
int num1;
int num2;
num1 = bintoi (argv[1]);
num2 = bintoi (argv[2]);
cal(num1, num2, argv);
return 0;
}
int bintoi (char * pbin)
{
int count;
int index;
int n;
int total = 0;
for (count = 0; *pbin != '\0'; count++, pbin++)
;
*pbin--;
index = count;
while (count)
{
if(*pbin != '0')
{
n = *pbin - '0';
n <<= index - count;
total += n;
}
pbin--;
count--;
}
return total;
}
void cal (int num1, int num2, char * argv[])
{
int ans;
char bin_str[MAXSIZE];
printf ("~%s is", argv[1]);
show_bstr(itobs(~num1, bin_str));
putchar ('\n');
printf ("~%s is ", argv[2]);
show_bstr(itobs(~num2, bin_str));
putchar ('\n');
ans = num1 & num2;
printf ("%s & %s is ", argv[1], argv[2]);
show_bstr(itobs(ans, bin_str));
putchar ('\n');
ans = num1 | num2;
printf ("%s | %s is ", argv[1], argv[2]);
show_bstr(itobs(ans, bin_str));
putchar ('\n');
ans = num1 ^ num2;
printf ("%s ^ %s is ", argv[1], argv[2]);
show_bstr(itobs(ans, bin_str));
putchar ('\n');
}
char * itobs (int n, char * ps)
{
int i;
static int size = 8 * sizeof (int);
for (i = size - 1; i >= 0; i--, n >>= 1)
ps[i] = (01 & n) + '0';
ps[size] = '\0';
return ps;
}
void show_bstr (const char * str)
{
int i = 0;
while (str[i])
{
putchar(str[i]);
if (++i % 4 == 0 && str[i])
putchar (' ');
}
}
就是编译的时候跳出对话框“XXX.exe遇到问题需要关闭。。。”
而不用命令行参数是可以编译通过的。
这是哪里出错了??