c - 我该如何处理 "segmentation fault(core dumped)"错误?(没有在其他地方找到答案)

标签 c segmentation-fault

我在运行下面的程序时遇到了这个问题。一切都编译得很好,但是当我运行它并输入一些东西时,我收到了段错误消息。它不是完整的代码,只是其中的一部分,它应该将输入文件中的一些字符串(地址)写入字符串数组,然后在数组中找到搜索字符串(作为参数输入)的可能匹配项。

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

int main(int argc, char *argv[])
{
  char *entry1 = argv[1], *adress[100], *possibleAdress[100], c;
  int i,j,k,possible = 0;

  while(c = getchar()!=EOF)          //write addresses to array
  {
    while(c = getchar()!=13)
      {
        adress[i][j] = c;
        j++;
      }
      i++;
  }

  i = 0;

  while(adress[i]!=0)        //find adresses matching with search enrty
  {
    j = 0;
    while(adress[i][j]!=0)
    {
      while(entry1[j]==adress[i][j])
        j++;
      if(j==strlen(entry1))        //check if the whole search entry is matching
      {
        possibleAdress[k] = adress[i];
        k++;
      }
      i++;
    }
  }
  return 0;
}

最佳答案

我可以清楚地看到不正确的一件事是,在 while 循环中使用之前,您没有在 main 中初始化 i 和 j 的值。

关于c - 我该如何处理 "segmentation fault(core dumped)"错误?(没有在其他地方找到答案),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46778427/

相关文章:

c - 将 size_t 变量设置为 -1

c - 段错误 C gdb 给出错误的行

c - 为什么以下代码会产生段错误?

检查 C 中的命令行参数

c++ - CUDA 将 GpuMat 的 c 数组传递给内核

c - adjtime(x) 调整时间很慢

c - 在 gcc ver 4.6.3 中按值将 Struct 作为参数传递给函数时出错

c - C中的百分比计算

c - malloc/结构指针数组段错误

c++ - gcc 根据优化创建崩溃代码