0

I have this problem when I run the program below. Everything compiles just fine, but when I run it and input something, i get the Segmentation fault message. Its not a full code just a part of it which is supposed to write some strings(adresses) from an input file to an array of strings then find possible matches for the searched string(input as an argument) in the array.

#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;
}
  • i j and k are all used without being initialised, the program invokes undefined behaviour. – George Oct 16 '17 at 20:22
  • 1
    Also, you haven't allocated any memory for `adress` and `possibleAdress`, so you're writing values into random addresses. A segfault is exactly the right response. – Lee Daniel Crocker Oct 16 '17 at 20:26
  • just change your arrays so that they arent pointers. Otherwise you will have to do as @LeeDanielCrocker suggests and actually allocate the memory for your arrays explicitly using malloc char entry1 = argv[1], adress[100], possibleAdress[100], c; – victor Oct 16 '17 at 20:28
  • note that `int i,j,k,possible = 0;` only initializes `possible`, you need to do this: `int i = 0, j = 0, k = 0, possible = 0;`. – bruceg Oct 16 '17 at 20:45
  • While it doesn't affect the code, the word is spelled `address`. – Christian Gibbons Oct 16 '17 at 21:30

3 Answers3

2

One thing I can clearly see incorrect is that you haven't initialized the value of i and j in main before you use in the while loops.

Shubs
  • 90
  • 1
  • 7
0

What do I do with “segmentation fault(core dumped)” error?

A previous question on what segfaults are: What is a segmentation fault?

What you do with them is break out a debugger, gdb for example. You are then able to step through the code line by line, instruction by instruction and understand the cause of the issue.

Other answers are correct, i & j are uninitialized. Debugging will allow you to inspect these variables and your memory accesses.

Chris
  • 2,655
  • 2
  • 18
  • 22
0

You need lots of correction. To increment a pointer you need to allocate adequate amount of memory to it. Your while loop is totally wrong where you are trying to increment the pointer value without allocation of memory in it and that is the reason it is giving you sig fault error.

first change this line : int i=0,j=0,k=0,possible = 0;

now the while loop

while(c = getchar()!=EOF)          //write addresses to array
{
  j = 0;
  char *p = malloc(30);// size should be change as per your requirement
  address[i] = p; //store the address of p here
  while(c = getchar()!=13)
  {
    *((adress[i])+j) = c; //it is just like *p++ = c
    j++;
  }
  i++;
 }
Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17