So from what I've read from other seg fault errors, it deals with accessing memory you shouldn't have access to (bad memory, or didn't use malloc()). For some reason there is no seg fault error when I make 2 nodes, but anything else gets a seg fault error. If someone one could please explain where I am going wrong in this program that would be extremely helpful. Thanks.
#include "header.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(int argc, char *argv[]) {
int N;
//int oddsonly = (BITS_PER_SEG*2) - 1;
int NSegs;
int numOfPrimes;
if (argc == 2) sscanf(argv[1],"%d",&N);
else scanf("%d",&N);
NSegs = (int)ceil(N/(float)odds_only);
printf("This is the number of segments made: %d\n", NSegs);
//this is how we make a doubly linked list
//void makelinkedlist(){
//this is how we make a doubly linked list
//void makelinkedlist(){
int i;
seg *node;
seg *current;
//head = (seg*)malloc(sizeof(seg));
head = NULL;
for(i = 1; i < NSegs; i++ ) {
if(i == 1) {
node = (seg*)malloc(sizeof(seg));
node->prev = NULL;
node->next = NULL;
head = node;
}//if
else{
current = (seg*)malloc(sizeof(seg));
current = head;
while(current->next != NULL){
current = current->next;
}//while
node = current->next;
node->prev = current;
node->next = NULL;
}//else
}//for
printf("Done allocating %d nodes\n",i); '
and my header file has this:
#ifndef EXTERN
#define EXTERN extern
#endif
#define SIZE_OF_SEG 256 // 256 int per segment
#define odds_only 16383
#define BITS_PER_SEG (8*SIZE_OF_SEG*sizeof(int))
typedef struct _seg { /* definition of new type "seg" */
int bits[256];
struct _seg *next,*prev;
}seg ;
EXTERN int NSegs;
EXTERN seg *head; // This variable will point to the
// start of the linked list of segments !!!
//EXTERN void clearAll( ); // Uses head to find the "bit array" (list)
EXTERN void sieveOfE( int N ); // Uses head to find the "bit array" (list)
EXTERN int countPrimes( int N ); // Uses head to find the "bit array" (list)
EXTERN void printPrimes( int N ); // Uses head to find the "bit array" (list)