【发布时间】:2014-02-13 01:50:24
【问题描述】:
我的代码给了我一个分段错误。我 99% 确定故障源于我糟糕的代码构造。
#include <stdio.h>
#include <assert.h>
#include <string.h>
int decToBit(unsigned int I, char *str){
str = "";
int currentVal = I;
do{
if(I%2 == 0)
strcat(str,"0");
else
strcat(str,"1");
} while(currentVal > 0);
return(0);
}
【问题讨论】:
-
通过
str = "";,decToBit()会忽略str的原始值。可能想要str[0] = '\0'; -
@chux 哇很棒的建议,你是一个救生员。我的代码终于运行了
-
@user2460844 许多人做出了贡献。一旦达到 15 个以上的代表点,请考虑对所有有用的答案进行投票。
标签: c