2 Replies - 670 Views - Last Post: 28 November 2009 - 05:11 PM Rate Topic: -----

#1 Quantum Mecha  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 12-November 09

Inserting items into linked list, error when compiling, C language

Posted 28 November 2009 - 01:30 PM

I am working on a program for a project, here is part of my code. This function is supposed to add items to my linked list.

Due to the nature of my project, I cannot change the function prototype at all, my job is to write the code within the function braces
i.e., I am given:

void insertItem(list1** head, list1* new_item) 
	{ 

	}



and have to write the code within the braces.

Here is what I have written:
 void insertItem(list1** head, list1* new_item)
	{
	list1 *newNode;
	list1 *nextNode;
	list1 *prevNode;
	
	/*
	prevNode -> next = new_item;			
	new_item -> next = nextNode;
	*/
		/*Different method, currently unused*/

	/*assert(head -> next == nextNode);*/	
	
	head -> next = new_item;
	new_item -> next = nextNode;

	}



I am using the GNU compiler (gcc) and I get the following error in the function "insertItem" when compiling:

"error: request for member next in something not a structure or union"
on the line
head -> next = new_item;


If I use assert(head -> next == nextNode);
 void insertItem(list1** head, list1* new_item)
	{
	list1 *newNode;
	list1 *nextNode;
	list1 *prevNode;
	
	/*
	prevNode -> next = new_item;			
	new_item -> next = nextNode;
	*/

	assert(head -> next == nextNode);

	head -> next = new_item;
	new_item -> next = nextNode;

	}



I get the same error as before, but on two lines, one on the line that contains
assert(head -> next == nextNode);
and the other on the line that contains
head -> next = new_item;


Thanks for the help!

This post has been edited by Quantum Mecha: 28 November 2009 - 02:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Inserting items into linked list, error when compiling, C language

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5846
  • View blog
  • Posts: 12,703
  • Joined: 16-October 07

Re: Inserting items into linked list, error when compiling, C language

Posted 28 November 2009 - 04:29 PM

 
// we'll have to assume "list1" is some kind of node type struct
void insertItem(list1** head, list1* new_item) {
	// I don't know why you're doing this
	list1 *newNode; // this is an uninitialized pointer
	list1 *nextNode; // this is an uninitialized pointer
	list1 *prevNode; // this is an uninitialized pointer
	
	// head is a pointer pointer
	// so this won't work
	// assert(head->next == nextNode);
	// unfortunately, this still probably won't work
	// since you never initalized nextNode
	assert(*head->next == nextNode);

	// so now, if there was a head->next you've replaced it with new_item; welcome to memory leaks
	head->next = new_item;
	
	// nextNode has still never been initialized, this is simply meaningless.
	new_item->next = nextNode;
}



I'm sorry if this is harsh, but the only thing clear about this code is that you need to actually crack a book on this stuff.

Good luck.
Was This Post Helpful? 0
  • +
  • -

#3 Quantum Mecha  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 12-November 09

Re: Inserting items into linked list, error when compiling, C language

Posted 28 November 2009 - 05:11 PM

View Postbaavgai, on 28 Nov, 2009 - 03:29 PM, said:

 
// we'll have to assume "list1" is some kind of node type struct
void insertItem(list1** head, list1* new_item) {
	// I don't know why you're doing this
	list1 *newNode; // this is an uninitialized pointer
	list1 *nextNode; // this is an uninitialized pointer
	list1 *prevNode; // this is an uninitialized pointer
	
	// head is a pointer pointer
	// so this won't work
	// assert(head->next == nextNode);
	// unfortunately, this still probably won't work
	// since you never initalized nextNode
	assert(*head->next == nextNode);

	// so now, if there was a head->next you've replaced it with new_item; welcome to memory leaks
	head->next = new_item;
	
	// nextNode has still never been initialized, this is simply meaningless.
	new_item->next = nextNode;
}



I'm sorry if this is harsh, but the only thing clear about this code is that you need to actually crack a book on this stuff.

Good luck.


Thanks. I'm on another computer now, but when I go to my original computer, I'll post the entire program. I do have a "struct list1" earlier in the program. Some of this code is from a book, but yea...I'll look back at it

Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1