[英]Segmentation fault using strcat using a single character
Here is my code: 这是我的代码:
int min = 0, i, z;
char star[18][100] = {0};
int temp = 0;
char TheStar[2];
TheStar[0] = '*';
TheStar[1] = '\0';
for(i = 0; i < 17; i++){
if(min == 0 && PerHundredThousand > 0)
min = PerHundredThousand[i];
if(PerHundredThousand[i] < min)
min = PerHundredThousand[i];}
for(z = 0; z < 17; z--){
if(PerHundredThousand[z] > 0)
temp = PerHundredThousand[z] / min;
while(temp > 0){
strcat(star[z], TheStar);
temp = temp - 1;}
} }
As you can see i'm trying to use strcat to add a '*' as long as temp is higher than 0 but I get a segmentation fault on: 如您所见,只要temp高于0,我就试图使用strcat添加一个'*',但是在以下方面我遇到了段错误:
strcat(star[z], TheStar);
Any help is appreciated. 任何帮助表示赞赏。 Thanks in advance. 提前致谢。
The second loop appears wrong: 第二个循环出现错误:
for(z = 0; z < 17; z--){
Should probably be: 应该可能是:
for(z = 0; z < 17; z++){
Otherwise it would loop a lot more times than intended (as well as write to invalid array positions). 否则,它将循环的次数比预期的要多(以及写入无效的数组位置)。
In the first loop there is a mistake 在第一个循环中有一个错误
if(min == 0 && PerHundredThousand > 0)
should be 应该
if(min == 0 && PerHundredThousand[i] > 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.