3 Replies - 480 Views - Last Post: 21 November 2011 - 05:56 AM Rate Topic: -----

#1 classic89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 03-November 11

Incompatible Pointer Error in Linklist in C

Posted 21 November 2011 - 01:22 AM

Attached is the errors that I get when compiling.

I'm having trouble figure out the syntax of pointers I think. I'm not really sure what I'm doing wrong with pointers.

/*


A linked list is a Data Structure that allows you to insert values at any place in the list without
having to rearrange the other elements on the list. The basic struct in the linked list is the Node
where every node contains a value and a reference (pointer) to the node that comes after it.

The head of the linked list is nothing more than a pointer to the first node in the list. 
*/

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

//Create a Node struct
struct Node
{
	int value;
	struct Node *next;
};
//Create a LinkedList struct
struct LinkedList
{
	struct Node *head;	
};

//Create a function to print the list as a comma separated list (for example2, 4, 5, 30, 40)
void printNode( struct Node *head);
//Instead of writing the insertion in main, create functions that allows insertion of an element into the list in order. 
//This means if I insert the values in the following order: 40, 23, 1, 46, 89, 6, 
//Then the linked list will look like: 1, 6, 23, 40, 46, 89.
void insertNode( int value, struct Node *head);
//Create a function that returns true if a value given by user is in the list, and false if the value is not in the list.
bool search(int value, struct Node *head);

main()
{
//In main, test your functions:
//Insert 20 random numbers into a linked list you create and then print the list to make sure it is indeed in order.
//Now, Repeatedly ask the user for a value to search in the list. 
//Let the user know whether the value is in the list or not. 
//After every search ask the user if they want to continue (c) or quit (q).
	struct Node *head = NULL;
	int v;
	int value;
	int i;
	char yesNo;
	bool ValidValue;
	
	for(i=0; i < 20; i++)
	{
		printf("Please input number: \n");
		scanf("%i", &v);		
	
		insertNode( v, &head);
		printNode( head );
		printf("\n");
	}
	printf("Would you like to search for a variable? (y/n)");
	scanf("%c", &yesNo);
	scanf("%c", &yesNo);
	if (yesNo=='y'|| yesNo=='N')
	{
		do
		{
			printf("What value would you like to search for?");
			scanf("%i", &value);
			ValidValue= search(value, &head);
			if(ValidValue==true)
			{
				printf("Your value %i, was found.", value);
			}
			else
			{
				printf("Value was not found.");
			}
			printf("Would you like to search for a variable? (c(continue)/q(quit))");
			scanf("%c", &yesNo);
			scanf("%c", &yesNo);
		}while(yesNo=='c'|| yesNo=='C');
	}

}

void insertNode(int value, struct Node *head)
{
	struct Node *ptrToHead;										
	ptrToHead= *head; //problem starts HERE!!
	if((*ptrToHead).value==NULL)
	{
		*ptrToHead=malloc(sizeof(struct Node));
		*ptrToHead.value=value;						 	
		*ptrToHead->next=NULL;							
	}
	else	
	{
		struct Node *temp, *prevNode;					
		temp=*ptrToHead;								
			while(temp.value<value)						
			{											//should temp and prevNode have a star before them as well? i.e. *temp and *prevNode
				prevNode=temp;							
				temp=temp.next;						
			}
		
		struct Node *newNode->value=v; 
		*newNode=temp;
		prevNode->next=newNode;
	}
}

void printNode(struct Node *head)
{
	 if( head == NULL)
	 {
		 printf("LinkList is empty!\n");
	 }
	else
	{
		while(head->value !=NULL)
		{
			printf("%i", head->value);
			head=head->next;
		}
	}
}
//Create a function that returns true if a value given by user is in the list, and false if the value is not in the list.
bool search(int value, struct Node *head)
{
	struct Node *ptrToHead;									
	*ptrToHead= *head;										//*ptrToHead?
	while(*ptrToHead!=NULL)
	{
		if(*ptrToHead==value)
		{
			return true;
		}
		else	
		{
			return false;
		}
	}
}


Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Incompatible Pointer Error in Linklist in C

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: Incompatible Pointer Error in Linklist in C

Posted 21 November 2011 - 05:10 AM

In this code most of the errors are related to this pointer assignment, comparison and passing pointer as parameter here.

Take a note of this.

if you have a pointer to something... let's say
struct Node * head;
It means it will point to a struct Node object. Which also means it will store address of that object as a value in pointer's memory. e.g.
struct Node obj;
struct Node *head;
//then assignment to pointer is
head = obj;
//and not 
*head = obj;



Now if you access it as
head
This will let you access the object pointed by pointer "head". if you use it for assignment, it will give you the memory address of object it is pointing to. if you use it to access the object then it's possible using -> operator. e.g. head->value

*head
This will give you value from base address of object. for e.g. if you have
int i = 0;
int *ptr = &i;


then ptr gives you address of i and *ptr gives you 0 the value stored in i.

now when you want to send an object to function that accepts a struct Node pointer. Then you are supposed to send an address of object as parameter. as you assign address to pointer there. So sending *head won't work. it's the value stored at address as we seen above. but sending head would work as value at head is the address of object right? So just follow these things and most of your errors will go away.

I hope this will help you. :)
Was This Post Helpful? 1
  • +
  • -

#3 classic89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 03-November 11

Re: Incompatible Pointer Error in Linklist in C

Posted 21 November 2011 - 05:52 AM

Thank! That was very helpful. My program finally compiles. :D I've made a lot of changes to it.

/*

A linked list is a Data Structure that allows you to insert values at any place in the list without
having to rearrange the other elements on the list. The basic struct in the linked list is the Node
where every node contains a value and a reference (pointer) to the node that comes after it.

The head of the linked list is nothing more than a pointer to the first node in the list. More on
linkedlists will be discussed in class next week.
*/
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <stdlib.h>

//Create a Node struct
struct Node
{
	int value;
	struct Node *next;
};
 
//Create a LinkedList struct
struct LinkedList
{
	struct Node *head;	
};

//Create a function to print the list as a comma separated list (for example2, 4, 5, 30, 40)
void printLink( struct Node *head);
//(Extra Credit) Instead of writing the insertion in main, create functions that allows insertion of an element into the list in order. 
//This means if I insert the values in the following order: 40, 23, 1, 46, 89, 6, 
//Then the linked list will look like: 1, 6, 23, 40, 46, 89.
void insertNode( int valueToInsert, struct Node **head);
//Create a function that returns true if a value given by user is in the list, and false if the value is not in the list.
bool search(int value, struct Node **ptrToHead);

main()
{
//In main, test your functions:
//Insert 20 random numbers into a linked list you create and then print the list to make sure it is indeed in order.
//Now, Repeatedly ask the user for a value to search in the list. 
//Let the user know whether the value is in the list or not. 
//After every search ask the user if they want to continue (c) or quit (q).
	struct Node *head = NULL;
	int valueToInsert;
	int i;
	int value;
	char yesNo;
	bool ValidValue;
	
	//let's insert 20 random values into the Link
	//every time I insert a value I will print the Link to make sure it's still correct. For now I'll just enter 5 values.
	for(i=0; i < 5; i++)
	{
		
		printf("Please input number: \n");
		scanf("%i", &valueToInsert);		

		//generate a random number
		//valueToInsert = rand() % 50;<-- Eventually I'd rather use a random number generator
		//insert it into the Link
		insertNode( valueToInsert, &head);
		//print the Link
		printLink( head );
		printf("\n");
	}
	printf("Would you like to search for a variable? (y/n)\n");
	scanf("%c", &yesNo);
	scanf("%c", &yesNo);
	if (yesNo=='y'|| yesNo=='N')
	{
		do
		{
			printf("What value would you like to search for?\n");
			scanf("%i", &value);
			
			ValidValue= search(value, &head);
			if(ValidValue==true)
			{
				printf("Your value %i, was found.\n", value);
			}
			else
			{
				printf("Value was not found.\n");
			}
			printf("Would you like to search for a variable? (c(continue)/q(quit))\n");
			scanf("%c", &yesNo);
			scanf("%c", &yesNo);
		}while(yesNo=='c'|| yesNo=='C');
	}
}

//A Function to print the Link  
void printLink( struct Node *head)
{
	 if( head == NULL)
	 {
		 printf("Link is empty!!\n");
	 }
	else
	{
		//print next node
		printLink( head->next );
		printf("%i, ", head->value);
	}
}


//Function to insert a value into the Link
//Notice that ptrToHead is a pointer to the head pointer (pointer to pointer)
// This is done so I can change the head in the function
// valueToInsert is the value I am inserting into the Link
void insertNode(int valueToInsert, struct Node **ptrToHead)
{
	//First check if the head is NULL, if that's the case then create a new node
	//using malloc and have the head point to it.s
	if( *ptrToHead == NULL )
	{
		//allocate enough space for a LinkNode and have head point to it
		*ptrToHead = malloc( sizeof(struct Node) );
		
		//Set the value of the node to the value we want to insert
		(*ptrToHead)->value = valueToInsert;
		(*ptrToHead)->next=NULL;

	}
	//otherwise the Link is not empty
	else
	{
		//Create a pointer to the head now and another pointer
		//to the node that comes before it.
		struct Node *temp, *prevNode, *newNode;
		
		//At first both pointer point to head
		temp = *ptrToHead;
		//prevNode = *ptrToHead;
		
		//Continue to advance until we reach NULL (outside the Link)
		//Always maintain prevNode to point to the node before you advance
		while( temp->value < valueToInsert)
		{
			//make prveNode point to the temp
			prevNode = temp;
			//Then advance temp to the next
			temp = temp->next;
		}
		newNode->value=valueToInsert;
		newNode=temp;
		prevNode->next=newNode;
	}	
	
}

//Create a function that returns true if a value given by user is in the list, and false if the value is not in the list.
bool search(int value, struct Node **ptrToHead)
{
	struct Node *temp;
	temp = *ptrToHead;									
	while( *ptrToHead != NULL )
	{
		if(temp->value==value)
		{
			return true;
		}
		else	
		{
			return false;
		}
	}
}



It's great until I get a runtime error that says: Segmentation fault (core dumped)

I'm looking over insertNode() and printLink() for a logical error.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Incompatible Pointer Error in Linklist in C

Posted 21 November 2011 - 05:56 AM

Having the LinkedList struct is good, it will make your life easier. Now use it! Also, typedef is your friend.

I'd start with something like:
typedef struct NodeStruct {
	int value;
	struct NodeStruct *next;
} Node;

typedef struct {
	Node *head;
} LinkedList;

//Create a function to print the "list" as a comma separated list (for example2, 4, 5, 30, 40)
void printList(LinkedList *);
void insertList(LinkedList *, int value);
//Create a function that returns true if a value given by user is in the list, and false if the value is not in the list.
bool search(LinkedList *, int value);

int getRandomNumber();

//In main, test your functions:
int main() {
	char yesNo;
	LinkedList list = { NULL };
	
	//Insert 20 random numbers into a linked list you create and then print the list to make sure it is indeed in order.
	// why, then are you asking for numbers?
	for(i=0; i < 20; i++) { insertNode(getRandomNumber(), &list); }
	printList(&list);
	
	do {
		int value;
		
		//Now, Repeatedly ask the user for a value to search in the list. 
		printf("What value would you like to search for?");
		scanf("%i", &value);
		
		//Let the user know whether the value is in the list or not. 
		if(search(&list, value) {
			printf("Your value %i, was found.", value);
		} else {
			printf("Value was not found.");
		}
		
		//After every search ask the user if they want to continue (c) or quit (q).
		printf("Would you like to search for a variable? (c(continue)/q(quit))");
		scanf("%c", &yesNo);
	} while(yesNo=='c'|| yesNo=='C');
	
	return 0;
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1