[英]Why is this exception thrown in the visual studio C compiler?
I am trying to get more adept and my C programming and I was attempting to test out displaying a character from the input stream while inside of the loop that is getting the character. 我正在尝试使自己的C语言编程更加熟练,并且试图在获取字符的循环内部测试从输入流显示字符的过程。 I am using the getchar
method. 我正在使用getchar
方法。
I am getting an exception thrown at the time that the printf
statement in my code is present. 我的代码中存在printf
语句时,我抛出异常。 (If I comment out the printf
line in this function, the exception is not thrown). (如果我在此函数中注释了printf
行,则不会引发异常)。
Exception: Unhandled exception at 0x611c91ad (msvcr90d.dll) in firstOS.exe: 0xC0000005: Access violation reading location 0x00002573. 异常:firstOS.exe中的0x611c91ad(msvcr90d.dll)未处理的异常:0xC0000005:访问冲突读取位置0x00002573。
Here is the code... Any thoughts? 这是代码...有什么想法吗? Thank you. 谢谢。
PS. PS。 I am using the stdio.h
library. 我正在使用stdio.h
库。
/*getCommandPromptNew - obtains a string command prompt.*/
void getCommandPromptNew(char s[], int lim){
int i, c;
for(i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i){
s[i] = c;
printf('%s', c);
}
} }
Try changing: 尝试更改:
printf('%s', c);
to 至
printf("%c", c);
If you wish to print the entire string at the end of the loop you need to terminate it with a NULL
char as: 如果希望在循环结束时打印整个字符串,则需要使用NULL
char终止它,如下所示:
s[i] = 0;
and then you can print it as: 然后可以将其打印为:
printf("%s", s);
First thing that you should check is: are you allocated memory for s[] or not. 您应该检查的第一件事是:是否为s []分配了内存。
Second: printf("%c", c); 第二个:printf(“%c”,c); // I can suppose that %s - is waiting for null terminated string. //我可以假设%s-正在等待以null结尾的字符串。
Third: maybe problem with "" vs '' in printf(). 第三:也许printf()中的“” vs“存在问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.