C program - Seg fault, cause of

Posted by resonant_fractal on Stack Overflow See other posts from Stack Overflow or by resonant_fractal
Published on 2012-12-08T10:57:40Z Indexed on 2012/12/08 11:05 UTC
Read the original article Hit count: 198

Filed under:
|

Running this gives me a seg fault (gcc filename.c -lm), when i enter 6 (int) as a value. Please help me get my head around this. The intended functionality has not yet been implemented, but I need to know why I'm headed into seg faults already.

Thanks!

#include<stdio.h>
#include<math.h>
int main (void)
{
  int l = 5;
  int n, i, tmp, index;
  char * s[] = {"Sheldon", "Leonard", "Penny", "Raj", "Howard"};
  scanf("%d", &n);

  //Solve Sigma(Ai*2^(i-1)) = (n - k)/l     

  if (n/l <= 1)
    printf("%s\n", s[n-1]); 
  else
    {
      tmp = n;
      for (i = 1;;)
    {
      tmp = tmp - (l * pow(2,i-1));
      if (tmp <= 5) 
        {
          // printf("Breaking\n");
          break;
        }
      ++i;
    }
      printf("Last index = %d\n", i);   //  ***NOTE***

      //Value lies in next array, therefore
      ++i;

      index = tmp + pow(2, n-1);
      printf("%d\n", index);

    }
  return 0;
}

© Stack Overflow or respective owner

Related posts about c

    Related posts about segmentation-fault