[英]Why is this C Program not running on Microsoft Visual Studio?
I am tried to run after building this without any warnings or errors in Visual Studio.我试图在构建它之后运行,而在 Visual Studio 中没有任何警告或错误。
#include <stdio.h>
#include <stdlib.h>
int e = 0;
int num = 0;
int a = 0;
int n = 0;
int c = 0;
static int factorial();
int factorial(int e){
if (e == 0)
return 1;
else if (e == 1)
return 1;
else
return e * factorial(e - 1);
}
int main() {
int k = 0;
int i = 0;
int j = 0;
int f = 0;
int r = 0;
printf_s(" Enter a number for length of string");
scanf_s("%d", &n);
printf_s(" Enter a number for choice of string");
scanf_s("%d", &k);
f = factorial(n) / (factorial(n - 2) * 2);
char **arr = (char **)malloc(n * sizeof(char *));
for (i = 0; i < n; i++){
arr[i] = (char*)malloc(f * sizeof(char));
}
for (i = 0; i < n; i++){
for (j = 0; j < f; j++) {
*(*(arr + i) + j) = '\0';
}
}
for (i = 1; i < n; i++){
num = 0;
for (j = 0; j < i; j++){
while (num <= n){
if ((num == j) || (num == i))
arr[r][num] = 'b';
else
arr[r][num] = 'a';
}
}
r++;
}
for (i = 0; i < n; i++) {
for j = 0; j < f; j++) {
printf_s("%c ", arr[j][i]);
}
}
}
The control goes out of control after taking the inputs.输入后控制失控。 Whitespace characters, new line, any characters, strings or gibberish doesn't make any difference.空白字符、换行符、任何字符、字符串或乱码都没有任何区别。 I also have my doubts about my calling the factorial function like an expression.我也对像表达式一样调用阶乘 function 有疑问。
I know this should be a comment but since I am lacking on reputation points, I can't comment.我知道这应该是评论,但由于我缺乏声誉点,我无法发表评论。 But from what I understand this code snippet:但据我了解这段代码片段:
'for (i = 0; i < n; i++) {
for j = 0; j < f; j++) {
printf_s("%c ", arr[j][i]);
}
}
}'
has an error;有错误; there should be a opening bracket in the second for loop.在第二个 for 循环中应该有一个左括号。 How did you manage to run it in spite of a compilation error to get a run time error I don't know.尽管出现编译错误,但您是如何设法运行它以获得我不知道的运行时错误。 I think you pasted an older version of your code.我认为您粘贴了旧版本的代码。 Apart from that I admit I don't even know about printf_s() function.除此之外,我承认我什至不知道 printf_s() function。 and from what I ran on my system it is throwing a undefined reference to both printf_s() and scanf_s().从我在系统上运行的内容来看,它抛出了对 printf_s() 和 scanf_s() 的未定义引用。 If its a built in function make sure you have included its header file.如果它是内置的 function 确保您已包含其 header 文件。 Apart from that in this code snippet:除了此代码段中的内容:
for (i = 1; i < n; i++){
num = 0;
for (j = 0; j < i; j++){
while (num <= n){
if ((num == j) || (num == i))
arr[r][num] = 'b';
else
arr[r][num] = 'a';
}
}
r++;
}
in the while loop num
and n
do not change so it is an infinite loop.在 while 循环中num
和n
不会改变,所以它是一个无限循环。 I think that is the main reason for your error.我认为这是您错误的主要原因。
factorial() is ok, but there is no point in writing else after return factorial()可以,但是return后写else没有意义
int factorial(int e)
{
if (e == 0) return 1;
if (e == 1) return 1;
return e * factorial(e - 1);
};
sure you can also write a single if() using ||当然你也可以用||写一个if()
Your prototype for factorial is missing the parameter as in int factorial(int);
您的阶乘原型缺少int factorial(int);
中的参数。
A left '(' is missing at line 56, the for j = 0 as pointed by others第 56 行缺少左'(' ,其他人指出的for j = 0
In this code在这段代码中
for (i = 0; i < n; i++) {
arr[i] = (char*)malloc(f * sizeof(char));
}
for (i = 0; i < n; i++) {
for (j = 0; j < f; j++) {
*(*(arr + i) + j) = '\0';
}
}
You can initialize the strings in the first loop.您可以在第一个循环中初始化字符串。 No need to go again looping into them无需 go 再次循环进入它们
Note that the safe version of scanf()
expect a size parameter for each specifier of type %c %s or %[ ] (not the case here, you are using %d only).请注意,安全版本的scanf()
期望每个%c %s 或 %[ ]类型的说明符都有一个大小参数(这里不是这种情况,您只使用 %d )。
Could you write a bit more on the logic of the program?你能多写一点程序的逻辑吗?
The problem is indeed in using the factorial function to compute f
:问题确实在于使用阶乘 function 来计算f
:
f = factorial(n) / (factorial(n - 2) * 2);
factorial
grows very quickly. factorial
增长非常快。 You implementation exceeds the range of type int
for n = 13
.您的实现超出了n = 13
的int
类型的范围。 Technically you get undefined behavior, in practice the value computed for f
is meaningless, it could even be negative and the division will crash if factorial(n - 2)
computes to 0
, which will happen for any value of n
larger than 35
.从技术上讲,您会得到未定义的行为,实际上为f
计算的值是没有意义的,它甚至可能是负数,如果factorial(n - 2)
计算为0
,除法将崩溃,这将发生在任何大于35
的n
值。
Here is a much simpler expression which is valid up to n = 46340
:这是一个更简单的表达式,直到n = 46340
才有效:
f = n * (n - 1) / 2;
There are other problems in the code:代码中还有其他问题:
scanf_s()
failure to read numbers您不测试scanf_s()
读取数字失败arr[i] = calloc(f, sizeof(char));
使用arr[i] = calloc(f, sizeof(char));
would prevent the need for the initialization loops as the indirect 2D matrix would already be initialized to all bits 0.将避免初始化循环的需要,因为间接二维矩阵已经初始化为所有位 0。num
is not modified in the loop while (num <= n)
, which makes it an infinite loop, another good explanation for your observations num
未在循环中修改while (num <= n)
,这使其成为无限循环,这是您观察的另一个很好的解释k
is not used anywhere k
不在任何地方使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.