打印时出现分段错误

发布于 2025-01-17 05:57:08 字数 656 浏览 2 评论 0原文

#include <stdlib.h>
#include <stdio.h>  
#include "string.h"
//This function tries to calculate result of the floor function for floats <= 9999.
int main(int argc, char* argv[]) {
int i, j;
float k;
int x[10000];

for(i = 0; i < 10000; ++i){
    x[i] = i;
}

printf("Enter a float in 0..9999: ");
scanf("%f", &k);

tester(x, k);
 }

int tester(int* c, int k) {
printf("x[%d] = %d\n", k, c[k]);
}  

当我运行该程序时,它在这里给我分段错误:

printf("x[%d] = %d\n", k, c[k]);

任何人都可以看到问题到底是什么?

你可以看到截图: printf 中的分段错误

#include <stdlib.h>
#include <stdio.h>  
#include "string.h"
//This function tries to calculate result of the floor function for floats <= 9999.
int main(int argc, char* argv[]) {
int i, j;
float k;
int x[10000];

for(i = 0; i < 10000; ++i){
    x[i] = i;
}

printf("Enter a float in 0..9999: ");
scanf("%f", &k);

tester(x, k);
 }

int tester(int* c, int k) {
printf("x[%d] = %d\n", k, c[k]);
}  

When I run the program it gives me segmentation fault in here:

printf("x[%d] = %d\n", k, c[k]);

Can anyone see the what problem really is?

You can see the screenshots:
segmentation fault in printf

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼睛会笑 2025-01-24 05:57:09

您的代码中有两个主要问题。

  1. 您从用户 (scanf) 获得作为 float 的输入,但实际上将其用作 int 将其传递到函数中并作为数组 x.
  2. 您应该始终检查来自终端(或任何地方)的用户输入。在这种情况下,如果实际输入在 0 到 9999 之间,则至少应该进行检查。

改进版本:

#include <stdlib.h>
#include <stdio.h>  
#include "string.h"

void tester(int* c, int k) {
    printf("x[%d] = %d\n", k, c[k]);
}  

//This function tries to calculate result of the floor function for floats <= 9999.
int main(int argc, char* argv[]) {
    int i;
    int k;
    int x[10000];
    
    for(i = 0; i < 10000; ++i){
        x[i] = i;
    }
    
    printf("Enter a float in 0..9999: ");
    scanf("%d", &k);
    if (k >= 0 && k < 10000) {
        tester(x, k);
    } else
    {
        printf("Sorry, your input %d was invalid.", k);
    }
}

可能这会解决您的分段错误问题。

There are two major problems in your code.

  1. You get input from the user (scanf) as float, but actually use it as int to pass it into the function and as index for the array x.
  2. You should ALWAYS check user input from the terminal (or wherever). In this case the check should be at least, if the actual input is between 0 and 9999.

Improved version:

#include <stdlib.h>
#include <stdio.h>  
#include "string.h"

void tester(int* c, int k) {
    printf("x[%d] = %d\n", k, c[k]);
}  

//This function tries to calculate result of the floor function for floats <= 9999.
int main(int argc, char* argv[]) {
    int i;
    int k;
    int x[10000];
    
    for(i = 0; i < 10000; ++i){
        x[i] = i;
    }
    
    printf("Enter a float in 0..9999: ");
    scanf("%d", &k);
    if (k >= 0 && k < 10000) {
        tester(x, k);
    } else
    {
        printf("Sorry, your input %d was invalid.", k);
    }
}

Probably that will fix your Segmentation Fault problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文