C 语言的计算单元转换器
我正在学习 C,并且编写了一个简单的程序(只是晒黑)。输入时,您传递两个参数(行和列),输出时您会获得该单元格的 Calc(或 Excel)代码。 例如:
Input: 3 1 Output: A3
Input: 1 27 Output: AA1
代码:
#include <stdio.h>
char kol[7] = "";
unsigned int passes=0, nr;
int powa(unsigned int lv)
{
if(passes < nr)
{
if(kol[lv] == '\0')
{
kol[lv] = 'A';
kol[lv+1] = '\0';
} else
{
kol[lv]++;
if(kol[lv] == 'Z'+1)
{
kol[lv] = 'A';
powa(lv+1);
return 0;
}
}
passes++;
if(lv != 0)
{
powa(lv-1);
} else
{
powa(lv);
}
}
}
int main(void)
{
unsigned int wier;
int i, len=0;
scanf("%u %u", &wier, &nr);
powa(0);
while(kol[len] != '\0')
{
len++;
}
for(i=len-1;i>=0;i--)
{
putchar(kol[i]);
}
printf("%u", wier);
return 0;
}
但是如果我传入一个更大的值(例如 300000000),我会收到分段错误错误。为什么?
I'm learning C and I have written a simple program (just a tanning). On input you pass two arguments (row and column) and output you get a Calc (or Excel) code for this cell.
For example:
Input: 3 1 Output: A3
Input: 1 27 Output: AA1
The code:
#include <stdio.h>
char kol[7] = "";
unsigned int passes=0, nr;
int powa(unsigned int lv)
{
if(passes < nr)
{
if(kol[lv] == '\0')
{
kol[lv] = 'A';
kol[lv+1] = '\0';
} else
{
kol[lv]++;
if(kol[lv] == 'Z'+1)
{
kol[lv] = 'A';
powa(lv+1);
return 0;
}
}
passes++;
if(lv != 0)
{
powa(lv-1);
} else
{
powa(lv);
}
}
}
int main(void)
{
unsigned int wier;
int i, len=0;
scanf("%u %u", &wier, &nr);
powa(0);
while(kol[len] != '\0')
{
len++;
}
for(i=len-1;i>=0;i--)
{
putchar(kol[i]);
}
printf("%u", wier);
return 0;
}
But if I pass in a larger value (such as 300000000) I get a segmentation fault error. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只为
kol
分配 7 个字节。您试图写入超出数组的范围。You're only allocating 7 bytes for
kol
. You're trying to write beyond the bounds of the array.您正在尝试递归吗?我不认为我会使用递归解决方案。您可能也不应该使用尽可能多的全局变量。
假设递归至关重要,那么总的来说,我认为我希望使用如下解决方案:
请注意,修改后的 powa() 返回一个指向其数据末尾的 null 的指针格式化。理论上,我应该检查
snprintf()
的返回结果以确保没有缓冲区溢出。由于我现在已经编译了它,并测试并更正了它(修正是将递归调用...bogus...
不是有效的 C,你可以看出我没有编译它,但是powa(div, buffer)
替换为powa(div - 1, buffer)
,这是必要的更改,因为计算需要处理 0 与1 作为递归方案的起点。对我来说似乎更简单(一个递归调用,而不是代码中的三个递归调用)。下面是处理从上面的代码派生的扫描和格式化的代码:
Are you experimenting with recursion? I don't think I'd be using a recursive solution. You should probably not be using as many global variables as you are, either.
Assuming recursion is crucial, then in outline, I think I'd expect to use a solution such as:
Note that the revised
powa()
returns a pointer to the null at the end of the data it has formatted. Theoretically, I should check the return fromsnprintf()
to ensure no buffer overflow.SinceI have now compiled this, and tested and corrected it (the correction being to replace the recursive call...bogus...
is not valid C, you can tell I've not compiled this, butpowa(div, buffer)
withpowa(div - 1, buffer)
, a change necessary because the calculation needs to deal with 0 versus 1 as the starting point for counting. The recursion scheme seems simpler to me (a single recursive call instead of three of them in your code).Here is code to handle both scanning and formatting derived from the code above: