Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 07-12-2006, 09:21 AM   #1 (permalink)
ron76
Registered User
 
Join Date: Jul 2006
Posts: 9
ron76 is on a distinguished road
Post pointer error

Hey ...
I just typed this code, but I am getting pointer errors. This is a very basic program in pointer. Please help.

The pointers I am getting are:
* assignment from incompatible pointer type
* incompatible types in assignment

This is the code. I have highlighted the lines that have error. This is the main file.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "LinkedList.h"


#define MAX 10000

int main()
{
	char string[WORD_LEN];
	char fileName[WORD_LEN];
	Node *List[MAX];
	char token[WORD_LEN];
	int i = 0;
    int result=0;
    int bucket=0;
	int key =0;
	int linenum=0;
	char input_file[MAX];
    const char seperator[] = " !@#$%^&*()-<>-,./\{}?~`:;'\"";
	Node *temp = NULL;
	FILE *fp;
	strcpy(fileName,"words.txt");

	fp = fopen(fileName,"r");


	if (fp == NULL)
	{
		printf("Cannot open file for reading\n");
		return EXIT_FAILURE;
	}

	else
	{
		while(fgets(string, WORD_LEN, fp))
		{
			key = 0;
			for (i=0;i<strlen(string);i++)
			{
				key += (int) string[i];
			}
			bucket = (int) key % MAX;
			temp = List[bucket];
			if (List[bucket] == NULL)
				temp=addhead(string,temp);
    		else
    	    	List[bucket]=add(string,temp);
		}
	}

	fclose(fp);
	printf("The dictionary has been loaded into the Table array  \n");

	while (scanf("%s", input_file)!=EOF)
	{
		++linenum;
		token = strtok(input_file,seperator);

		key=0;

		for(i=0;i<strlen(token);i++)
		{
			key += (int) token[i];
		}

		bucket = key % MAX;

		i=0;
		temp = List[bucket];
		result = lookupDictionary(token, temp);


		if (result == 0)
			printf("Spelling error \n word: %s  line: %d",token,linenum);

	}
	printf("\n Search complete \n");
	printf("Program completed successfully\n");
	return 0;

}

Node *newNode(char *string)
{
	Node *newNode;

	strcpy(newNode->string,strtok(string, " "));
	newNode->next = NULL;
	return(newNode);
}

Node *add(char *string, Node *temp)
{
	Node *new = newNode(string);

	temp->next = new;
	return(new);
}

Node *addhead(char *string, Node *temp)
{
	Node *new = newNode(string);

	Node *head = NULL;

    head = new;
	return (new);
}

int lookupDictionary (char *word, Node *temp)
{
	while (temp->string != word)
	{
		temp = temp->next;
	}

	if (temp->string == word)
		return TRUE;
	else
		return FALSE;
}
This is the header file.


Code:
#ifndef _LINKEDLIST
#define _LINKEDLIST
#define TRUE 1
#define FALSE 0

typedef struct NODE Node;
#define WORD_LEN 15

struct NODE
{
	char string[WORD_LEN];
	struct Node *next;

};

Node *newNode(char *string);
Node *add(char *string, Node *temp);
Node *addhead(char *string, Node *temp);
int lookupDictionary (char word[0], Node *temp);
#endif
Please assist. I have just these 3 errors. Pointers drive me cuckoo..
ron76 is offline   Reply With Quote
Old 07-12-2006, 09:27 AM   #2 (permalink)
ron76
Registered User
 
Join Date: Jul 2006
Posts: 9
ron76 is on a distinguished road
Also ...
THis program is supposed to input a list that is into the array of linked lists[I am trying to create a dynamic table].After that it takes a txt file and looks up the dictionary, which all words are presnt in the dictionary.
ron76 is offline   Reply With Quote
Old 07-12-2006, 09:35 AM   #3 (permalink)
ron76
Registered User
 
Join Date: Jul 2006
Posts: 9
ron76 is on a distinguished road
Site wouldnt allow

The site wouldnt allow me to edit my post, so I could provide explanation.

So here it is with explanation.


Hey ...
I just typed this code, but I am getting pointer errors. This is a very basic program in pointer. Please help.

The pointers I am getting are:
* assignment from incompatible pointer type
* incompatible types in assignment

This is the code. I have highlighted the lines that have error. This is the main file.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "LinkedList.h"


#define MAX 10000 // maximum letters in a string

int main()
{
	char string[WORD_LEN];
	char fileName[WORD_LEN];
	Node *List[MAX];//array of linked list
	char token[WORD_LEN];// token to tokenize
	int i = 0;
    int result=0;
    int bucket=0;  // bucket for hashing
	int key =0;
	int linenum=0;
	char input_file[MAX];
    const char seperator[] = " !@#$%^&*()-<>-,./\{}?~`:;'\"";
	Node *temp = NULL;
	FILE *fp;
	strcpy(fileName,"words.txt");

	fp = fopen(fileName,"r");


	if (fp == NULL)
	{
		printf("Cannot open file for reading\n");
		return EXIT_FAILURE;
	}

	else
	{
		while(fgets(string, WORD_LEN, fp))
		{
			key = 0;
			for (i=0;i<strlen(string);i++) // producing hash key     
                                                                                for the string
			{
				key += (int) string[i];
			}
			bucket = (int) key % MAX;   //pucket number for the 
                                                                                string
			temp = List[bucket];
			if (List[bucket] == NULL)
				temp=addhead(string,temp);// if that array 
                                                                                   node is empty
    		else
    	    	List[bucket]=add(string,temp);
		}
	}

	fclose(fp);
	printf("The dictionary has been loaded into the Table array  \n");

	while (scanf("%s", input_file)!=EOF)
	{
		++linenum;
		token = strtok(input_file,seperator);

		key=0;

		for(i=0;i<strlen(token);i++) //hashing
		{
			key += (int) token[i];
		}

		bucket = key % MAX;

		i=0;
		temp = List[bucket];
		result = lookupDictionary(token, temp);


		if (result == 0)
			printf("Spelling error \n word: %s  line: %d",token,linenum);

	}
	printf("\n Search complete \n");
	printf("Program completed successfully\n");
	return 0;

}

Node *newNode(char *string)
{
	Node *newNode;

	strcpy(newNode->string,strtok(string, " "));
	newNode->next = NULL;
	return(newNode);
}

Node *add(char *string, Node *temp)
{
	Node *new = newNode(string);

	temp->next = new;
	return(new);
}

Node *addhead(char *string, Node *temp)
{
	Node *new = newNode(string);

	Node *head = NULL;

    head = new;
	return (new);
}

int lookupDictionary (char *word, Node *temp)
{
	while (temp->string != word)
	{
		temp = temp->next;
	}

	if (temp->string == word)
		return TRUE;
	else
		return FALSE;
}
This is the header file.


Code:
#ifndef _LINKEDLIST
#define _LINKEDLIST
#define TRUE 1
#define FALSE 0

typedef struct NODE Node;
#define WORD_LEN 15

struct NODE
{
	char string[WORD_LEN];
	struct Node *next;

};

Node *newNode(char *string);
Node *add(char *string, Node *temp);
Node *addhead(char *string, Node *temp);
int lookupDictionary (char word[0], Node *temp);
#endif

This program is supposed to input a list into the array of linked lists[I am trying to create a dynamic table].After that it takes a txt file and looks up the dictionary, which all words are presnt in the dictionary.



Please assist. I have just these 3 errors. Pointers drive me cuckoo..
ron76 is offline   Reply With Quote
Old 07-12-2006, 11:51 AM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
For your strtoc() question I would suggest you read a thread here which will show you, that you shouldn't allocate memory for your token, since it's just a pointer to the memory you're searching through ie. input_file.
For explanation on how strtok() works, you might want to read this thread.

For your lookupDictionary(), you might want to rethink it...
Code:
while (temp->string != word)
	{
Just read it again, and rethink that....
Is this C or is it C++, from what I can read, it is highly C, and not relying on the std::string container from C++.
Might I suggest a call to strcmp() or strncmp() for buffer-overflow security, or perhaps strcasecmp() if it isn't a casesensitive search. I know the last two aren't ANSI/ISO specific, but from what you describe it seems you're in a POSIX environment, where they are available from the strings.h header.

The thread mentioned earlier, also uses alot of pointer-arithmetic, so you might learn a wee bit from reading that code, in order to understand derefferencing pointers.

In regards to the "editing earlier posts", this has been an issue in the past, where people would simply delete or edit the first post, when they found the answer, so you wouldn't know what problem orriginaly spawned the thread..
To prevent that we had to specify that a normal user is only allowed to edit their post within 10 minuts from first submitting.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 07-12-2006 at 12:09 PM.
redhead is offline   Reply With Quote
Old 07-13-2006, 07:39 AM   #5 (permalink)
ron76
Registered User
 
Join Date: Jul 2006
Posts: 9
ron76 is on a distinguished road
ok I think where my problem is coming. Here is the thing. this is an array of linked lists.
So when I go to that array node. and pass a string to add to that node. Im getting the following erroe when the function accepts the node as input argument.

Let me show what I mean:

Code:
// while decalring
Node *List[MAX]

// call from main
//if that linked list in that array element is empty 
if (List[bucket] == NULL)
    addhead(string,);

Node *addhead(char *string, Node *temp)
{
	Node *new = newNode(string);

	Node *head = NULL;

    printf("\n node /t %s", temp);

    head = new;
    printf("\n List /t %s", new);	
     return (new);
}

Node *newNode(char *string)
{
	Node *newNode;

	strcpy(newNode->string,strtok(string, " "));
	newNode->next = NULL;
	return(newNode);
}
This is the warning I get for the bold lines above:
warning: char format, Node arg (arg 2)

If I were to print what is the content of List[bucket] from the main class, I do get its contents. But from the function it does not work.

Please help.
ron76 is offline   Reply With Quote
Old 07-13-2006, 07:44 AM   #6 (permalink)
ron76
Registered User
 
Join Date: Jul 2006
Posts: 9
ron76 is on a distinguished road
I also did cast it into a char and a string.
But I was getting the following warning:
Warning: cast from pointer to integer of different size
ron76 is offline   Reply With Quote
Old 07-13-2006, 08:49 AM   #7 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
Well... '%s' will assume the argument is a string, or more closely a char*, since you're passing a Node* which is a container class, you can't expect printf() to knwo, what to do with it..
Why not make it wee bit more simple like:
Code:
printf("\n node /t %s", temp->string);
Since you know your Node struct has a member called string, which is a char*.
Byt the way what good is this ??
Code:
Node *addhead(char *string, Node *temp)
{
    Node *new = newNode(string);
    Node *head = NULL;
    printf("\n node /t %s", temp);
    head = new;
    printf("\n List /t %s", new);	
     return (new);
}
Since you create the new container, which is pointing to a copy of the Node struct which has (or should have) initialy set it's member string to the value of argument string yet it is practicaly doing nothing...

Then you assign a new Node* called head with the same memory address.. yet when the function returnes it is only the new member which is returned, which effectively will kill your head container...
I'm suspecting you wanted something like:
Code:
Node *addhead(char *string, Node *temp)
{
    Node *new = newNode(string);
    temp->next = new;
    new->next = NULL;
    return (new);
}
For the printing of things I can't see what you're trying to do.. You create a form of linked list, yet only have a pointer to the next item, in order to print the entire list, you need to know teh initial list starting container, and have a dedicated printing function which would run through the list, given the starting point, printing whatever it encounters...
Perhaps something like:
Code:
int print_list(Node* start)
{
    Node* tmp=start;
    int i=0, j;
    while(tmp){
         printf("> %s\n", tmp->string);
         for(j=0; j < i; ++j)
             printf(" ");
         tmp = tmp->next;
         ++i;
    }
    return i;
}
Not only will this print every string container in your list, but it will also return how many items your list is holding.

But if you plan on doing some serius mengling with this, I would suggest somethign like a double linked list, so you can search forward or backwards through it, given teh required action.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 07-13-2006, 09:15 AM   #8 (permalink)
ron76
Registered User
 
Join Date: Jul 2006
Posts: 9
ron76 is on a distinguished road
THanks...

The reason I did 2 print statements was to see where the error was. The thought about head being dead did not occur to me.

But I have a question now; how will I check if the array-node at List[bucket] is empty.

I tried to use a condition with if List[bucket]->next == null

Its compiling fine. But I am getting a segmentation fault.

Please help again.
Thanks again.
ron76 is offline   Reply With Quote
Old 07-13-2006, 09:50 AM   #9 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
hmm... sorry I can't follow your code here...
But from what I can guess, it seems that you never actualy assign anything into your List, befor quering it's containers... Relying on the compiler setting every instance to NULL uppon declaration...
Befor using it, you might want to do something like:
Code:
memcpy(List, NULL, MAX);
To nullify it befor you actualy start examining it for previus assignments.. then your
Code:
if(List[bucket] == NULL)
or
Code:
if(!List[bucket])
should work..
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 07-13-2006, 10:22 PM   #10 (permalink)
ron76
Registered User
 
Join Date: Jul 2006
Posts: 9
ron76 is on a distinguished road
Im sorry tht was a mistake i made.
for memory ive used malloc.

I figure, the program is crashing when I am adding a node ,

I have a question. Since I am assigning temp to the function add. If I have to check something in that particular slot array node. will all the node additions be visible?
ron76 is offline   Reply With Quote
Old 07-14-2006, 07:00 AM   #11 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
Since you're using single linked lists, it is only possible to search forward, which means in your add function you will see the item at hand, and the next one, which most likely will be either NULL or the newly added item.

I must boldly admit, I havn't taken the time, to closely read your code, so I have no generel knowledge of what exactly it is you're trying todo...
If I do get the time, I might try and come up with my solution to your problem during this weekend.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is this error? john_tran Standard C, C++ 4 10-20-2004 08:30 PM
C++ compile error Amaranthine Standard C, C++ 5 05-05-2004 08:23 PM


All times are GMT -8. The time now is 04:07 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting