反转用户输入的整数字符串 (C)
我想要做的是反转用户输入的一串数字。发生的情况是它编译并运行,直到我在 scanf
之后按 Enter 键。然后我收到一些 Microsoft 运行时错误...出了什么问题???
注意:这是家庭作业,但我已经弄清楚了逻辑。令我困惑的是这个错误。
#include <stdio.h>
int main()
{
unsigned int giveStr = 0;
char* charIt;
printf("Enter a number to be reversed.\t");
scanf("%d", &giveStr);
fflush(stdin);
sprintf(charIt, "%d", giveStr);
revStr(giveStr);
getchar();
return 0;
}
revStr(unsigned int n)
{
char buffer[100];
int uselessvar, counter = 0;
for (; n > 0;)
{
uselessvar = sprintf(&buffer[counter], "%d", n);
counter++;
}
for (counter = 0; counter > 0;)
{
printf("%c", buffer[counter]);
counter--;
}
return 0;
}
编辑:刷新换行符的标准输入:/以及图像这里只是不那样程序。和我的。
What I want to do is reverse a string of numbers that the user enters. what happens is it compiles and runs till i hit enter after the scanf
. then I get some Microsoft runtime error... what's going wrong???
NOTE: this is homework, but i've got the logic figured out. what baffles me is this error.
#include <stdio.h>
int main()
{
unsigned int giveStr = 0;
char* charIt;
printf("Enter a number to be reversed.\t");
scanf("%d", &giveStr);
fflush(stdin);
sprintf(charIt, "%d", giveStr);
revStr(giveStr);
getchar();
return 0;
}
revStr(unsigned int n)
{
char buffer[100];
int uselessvar, counter = 0;
for (; n > 0;)
{
uselessvar = sprintf(&buffer[counter], "%d", n);
counter++;
}
for (counter = 0; counter > 0;)
{
printf("%c", buffer[counter]);
counter--;
}
return 0;
}
EDIT: flushing stdin for newlines :/ and also image here just not with that program. with mine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在尝试访问未分配的内存:
将
char* charIt;
更改为char charIt[50];
并且一切都应该很好(嗯,至少分段错误部分)另外...将
charIt
传递给revStr
,因为charIt
包含带有我们号码的字符串。然后,revStr 中的一个简单的 for 循环就可以解决问题(第二个循环的目的是什么?)
这将打印我们的字符表示从最后一个到第一个的每个字符。您应该阅读有关
for
循环的更多内容。You are trying to access memory which is not allocated in:
Change
char* charIt;
tochar charIt[50];
and all should be well (well, at least the segmentation fault part)Also... pass
charIt
torevStr
, ascharIt
contains the string with our number.Then, a simple for loop in
revStr
will do the trick (what was the purpose of the second one, anyway?)This will print each char our char representation has from the last one till the first one. You should read more on
for
loops.对于家庭作业问题,如果您有K&R 书,请转阅读第 3.5 节并仔细阅读。
请注意函数
reverse()
和itoa()
。他们应该为您提供如何解决问题的好主意。For your home work problem, if you have the K&R book, turn to section 3.5 and read it thoroughly.
Note the functions
reverse()
anditoa()
. They should give you a pretty good idea on how to solve your problem.你的程序如何跳出 for (; n > 0;) 循环?计数器不会简单地增加直到出现总线错误吗?
ED:
恕我直言,我认为“我已经弄清楚了逻辑”的说法有点乐观。 :^) 毫无疑问有人会按照应该做的方式发布
当我写完这篇文章时,但可能值得引起人们注意出了什么问题(除了其他地方提到的内存分配问题):
您的第一个循环“for (; n > 0;)”很奇怪,因为您将整个数字 n 打印到计数器的缓冲区中。那么为什么您需要多次执行此操作呢?如果您选择单个数字,您可能会选择,但您不会,显然您知道如何执行此操作,因为您已经使用过“sprintf(charIt, “%d”, GiveStr);”。 [旁白:对于无符号整数变量来说,giveStr 并不是一个好名字!]
你的第二个循环也有奇怪的条件:你将 counter 设置为 0,将条件设置为 counter > > 。 0,然后减少里面的计数器。这显然不会按照您想要的方式循环字符。假设您认为第一个循环是逐个字符的,那么您可能会考虑从 counter-1 向下循环到 0?
How does your program get out of the for (; n > 0;) loop? Won't counter simply increase until you get a bus error?
ED:
Respectfully, I think the claim that "i've got the logic figured out" is a little optimistic. :^) Doubtless someone will post the way it should have been done
by the time I'm done writing this, but it's probably worth drawing attention to what went wrong (aside from the memory allocation problems noted elsewhere):
Your first loop, "for (; n > 0;)", is strange because you're printing the entire number n into the buffer at counter. So why would you need to do this more than once? If you were selecting individual digits you might, but you're not, and obviously you know how to do this because you already used "sprintf(charIt, "%d", giveStr);". [Aside: giveStr isn't a great name for an unsigned integer variable!]
Your second loop also has strange conditions: you set counter to 0, set the condition that counter > 0, and then decrease counter inside. This obviously isn't going to loop over the characters in the way you want. Assuming you thought the first loop was character-by-character, then maybe you were thinking to loop down from counter-1 to 0?