Metal Guitarist Forums banner

C HALP

801 Views 5 Replies 3 Participants Last post by  Max
im pretty new with c, but im getting a segmentation fault just trying to read in a value with scanf, and i dunno why. Can anybody halps?
Code:
#include <stdio.h>
#include <stdlib.h>

struct node{
	int value;
	int count;
	struct node *next;
};

int main(){
	int n = 0;
	int readin;
	int worked;
	struct node readlist;
	struct node refnode;
	worked = scanf("%d", &readin);

	printf("checkpoint 1");
	
	if(!worked){
		fprintf(stderr, "INVALID INPUT VALUE");
		return -1;
	}
	if(worked == EOF){
		return 0;
	}
	readlist.count = 1;
	readlist.value = readin;
	readlist.next  = NULL;
	n = 1;
	//
	//   
	//ERRORS?!?!
	//(next line)	
	//
	worked = scanf("%d", &readin);
	
	while(worked != EOF){
		if(!worked){
			fprintf(stderr, "INVALID INPUT VALUE");
			return -1;
		}
		refnode = readlist;
		printf("reading %d", readin);
		int i;
		int found = 1;
		for (i=0; (i<n) && (refnode.value<readin); i++){
			refnode = *refnode.next;
			if(i<n){
				found = 0;
			}
		}
		if (found ==0){
			
			refnode = readlist;
			for(i=1; i <n ; i++){
				refnode = *refnode.next;
			}
			struct node temp;
			temp.value = readin;
			temp.next = NULL;
			refnode.next = &temp;
			n++;
		}
		else if (refnode.value == readin){
			refnode.count++;
		}
		else{
			struct node temp = refnode;
			refnode = readlist;
			while((*refnode.next).value!=temp.value){
				refnode = *refnode.next;
			}
			struct node temp2;
			temp2.value = readin;
			temp2.next = &temp;
			refnode.next = &temp;
		}
	}
	refnode = readlist;
	while(refnode.next !=NULL){
		printf("%d %d\n", refnode.value, refnode.count);
		refnode = *refnode.next;
	}
	printf("%d %d\n", refnode.value, refnode.count);
	return 0;
}
1 - 6 of 6 Posts
Odd :scratch:

That line looks exactly the same as a previous line that isn't throwing an error. .?????
Wirelessly posted (Hivemind: Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B117 Safari/6531.22.7)

I know!

Ima go nuts :lol:
I hate seemingly random errors :lol:
Code:
Program received signal SIGSEGV, Segmentation fault.
0x00000000004007ac in main () at test.c:50
50                              refnode = *refnode.next;
A NULL-pointer dereference makes it segfault. There's only one node in the linked list, and you're trying to access the next one which doesn't exist. If you're using gcc, you can pass the -ggdb flag to include debug symbols in the executable and use gdb to find out exactly where it's crashing. ;)

I'm not exactly sure of what you're trying to do, but you will need to use dynamic memory allocation (malloc and free) to make a linked list. Creating and attaching a new node will look something like this:
Code:
list_node *newnode = malloc(sizeof(struct list_node));
newnode->value = somevalue;
newnode->next = listhead;
listhead = newnode;
Hope that helps.
See less See more
yeah, i fixed it. I wasnt allocating space properly, and my traversals were getting sketchy :lol:

And i just tried to end that line with a semicolon :rofl:
1 - 6 of 6 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top