4 Replies - 582 Views - Last Post: 03 May 2011 - 04:21 PM Rate Topic: -----

#1 KonigGeist  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-May 11

Segmentation Fault and Dereferencing to Incomplete Type Error

Posted 03 May 2011 - 03:52 PM

I have been working on an assignment for the past few hours, and I believe that I am completing it mostly correctly. This assignment requires me to create a linked list from an input file and do various things to the file. So far, I am stuck on the part where I must read the file. The first error to appear when compiling was "dereferencing pointer to incomplete type" in the lines that I have marked below. Whenever I comment out these lines(marked with asterisks), the code compiles, but when I run it, I receive a segmentation fault. I used gdb to locate the source of the error, and it pointed to feof in the while loop (also marked). I have tried searching for what could be the cause of the errors, but I have not found a case similar enough to provide help for either of them. Thanks in advance for any help.

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

struct data {				/* struct data holds the students information */
	char 	name [10];
	int		age;
	int		weight;	
};

typedef struct data DATA; 	/* defines struct data as DATA */

struct linked_list {		/* linked_list enables the creation of a linked list containing DATA */
	DATA				d;	
	struct linked_list	*next;
};

typedef struct linked_list ELEMENT;	
typedef struct ELEMENT *LINK;

/*********************************************************************************************************************/

LINK create_list_from_file(const char *);					/* function to create a linked list from file input */ 
LINK lookup(const DATA, LINK);						/* looks up data in the linked list */
int count(LINK, const int, const int); 					/* counts the number of people meeting certain qualities */
void print_list_to_file(LINK, const char *, const char *);	/* prints the contents of the list to an out file */

/*********************************************************************************************************************/

LINK create_list_from_file(const char *in_file_name)
{
	LINK student = NULL, head = NULL, cptr = NULL ;	
	FILE *ifp = fopen(in_file_name, "r");

	if (ifp = NULL) printf("ERROR: FILE COULD NOT BE READ");
	else {
		while(!feof(ifp)) { /****************************************/ 
			student = malloc(sizeof(LINK));
			if (student != NULL) {
				fscanf(ifp, "%s %d %d", student->d->name, student->d->age, student->d->weight);  /**************************************/
				student->next = NULL;
				if (head == NULL) 
					head = student;
				else 
					cptr->next = student;
				cptr = student;
			}
			else
				printf("ERROR: SPACE CANNOT BE ALLOCATED");		
		}
	}			
}

/*********************************************************************************************************************/

LINK lookup(const DATA c, LINK head)
{
}

/*********************************************************************************************************************/

int count(LINK head, const int age_limit, const int weight_limit)
{
}

/*********************************************************************************************************************/

void print_list_to_file(LINK head, const char *o_f_name, const char *open_mode)
{
}

/*********************************************************************************************************************/

int main(void)
{
	char *in_file_name;
	in_file_name = "infile";
	create_list_from_file(in_file_name);
	return 0;
}


This post has been edited by KonigGeist: 03 May 2011 - 03:59 PM
Reason for edit:: took out color tags, they do no good inside code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Segmentation Fault and Dereferencing to Incomplete Type Error

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,710
  • Joined: 03-August 09

Re: Segmentation Fault and Dereferencing to Incomplete Type Error

Posted 03 May 2011 - 04:00 PM

typedef struct linked_list ELEMENT;	
typedef struct ELEMENT *LINK;

please don't typedef pointers, it makes it terribly hard to read your code.

if (student != NULL) {/*Error appears here*/
				fscanf(ifp, "%s %d %d", student->d->name, student->d->age, student->d->weight); 


so if student is NULL then how do you expect "student->d->name" to exist? the answer is it dosn't.
Was This Post Helpful? 2
  • +
  • -

#3 KonigGeist  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-May 11

Re: Segmentation Fault and Dereferencing to Incomplete Type Error

Posted 03 May 2011 - 04:03 PM

Thank you. I used typedef on the pointers because we are required to use certain elements from a sample file that was given by our teacher.

I added the NULL as some similar code that I looked at had it, but I experienced the error before adding it.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Segmentation Fault and Dereferencing to Incomplete Type Error

Posted 03 May 2011 - 04:17 PM

When I compile your code I get these errors:

Quote

main.c||In function ‘create_list_from_file’:|
main.c|34|warning: suggest parentheses around assignment used as truth value|
main.c|39|error: dereferencing pointer to incomplete type|
main.c|39|error: dereferencing pointer to incomplete type|
main.c|39|error: dereferencing pointer to incomplete type|
main.c|40|error: dereferencing pointer to incomplete type|
main.c|44|error: dereferencing pointer to incomplete type|
main.c||In function ‘lookup’:|
main.c|55|warning: unused parameter ‘c’|
main.c|55|warning: unused parameter ‘head’|
main.c||In function ‘count’:|
main.c|61|warning: unused parameter ‘head’|
main.c|61|warning: unused parameter ‘age_limit’|
main.c|61|warning: unused parameter ‘weight_limit’|
main.c||In function ‘print_list_to_file’:|
main.c|67|warning: unused parameter ‘head’|
main.c|67|warning: unused parameter ‘o_f_name’|
main.c|67|warning: unused parameter ‘open_mode’|
||=== Build finished: 5 errors, 9 warnings ===|


Also look at this line:
	if (ifp = NULL) printf("ERROR: FILE COULD NOT BE READ");


This line is line 34 you are assigning NULL to ifp. You should be using the comparison operator (==) here.


Jim
Was This Post Helpful? 1
  • +
  • -

#5 KonigGeist  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-May 11

Re: Segmentation Fault and Dereferencing to Incomplete Type Error

Posted 03 May 2011 - 04:21 PM

Well, that solved the segmentation fault. I can't believe I didn't catch that .
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1