[英]String literal not printing out correctly
I am trying to build a string in C, and then store that string in a struct. 我试图在C中构建一个字符串,然后将该字符串存储在结构中。 However when I build my string and print it out for testing, I get extra unintelligible characters after the correct string.
但是,当我构建字符串并打印出来进行测试时,在正确的字符串之后会出现一些额外的难以理解的字符。 Any suggestions would be great.
任何建议都很好。
My code 我的密码
typedef struct
{
char *context;
int numOnes;
int numZeros;
}Pattern;
void getPrediction(int t,int *stream){
int end = beg;
int count = 0;
char *pred, str[t+1];
pred = str;
int n = t;
while(count<t){
//printf("%d\n", stream[end]);
if(stream[end] == 1){
pred[count]= '1';
}
else{
pred[count]='0';
}
end--;
n--;
count++;
}
printf("%s\n",pred);
beg--;
Pattern newPat={pred,1,0}; //testing purposes
printf("%s\n",newPat.context); //testing purposes
}
The printf("%s"
expects a string , which is defined as a series of characters followed by a null character. You didn't supply the null character. printf("%s"
需要一个字符串 ,该字符串定义为一系列字符,后跟一个空字符。您未提供该空字符。
To fix this, do pred[count] = 0;
要解决此问题,请执行
pred[count] = 0;
before the printf
. 在
printf
之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.