|
 |
|
 |
07-12-2006, 09:21 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Jul 2006
Posts: 9
|
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..
|
|
|
07-12-2006, 09:27 AM
|
#2 (permalink)
|
|
Registered User
Join Date: Jul 2006
Posts: 9
|
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.
|
|
|
07-12-2006, 09:35 AM
|
#3 (permalink)
|
|
Registered User
Join Date: Jul 2006
Posts: 9
|
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..
|
|
|
07-12-2006, 11:51 AM
|
#4 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,705
|
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.
Last edited by redhead; 07-12-2006 at 12:09 PM.
|
|
|
07-13-2006, 07:39 AM
|
#5 (permalink)
|
|
Registered User
Join Date: Jul 2006
Posts: 9
|
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.
|
|
|
07-13-2006, 07:44 AM
|
#6 (permalink)
|
|
Registered User
Join Date: Jul 2006
Posts: 9
|
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
|
|
|
07-13-2006, 08:49 AM
|
#7 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,705
|
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.
|
|
|
07-13-2006, 09:15 AM
|
#8 (permalink)
|
|
Registered User
Join Date: Jul 2006
Posts: 9
|
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.
|
|
|
07-13-2006, 09:50 AM
|
#9 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,705
|
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 should work..
|
|
|
07-13-2006, 10:22 PM
|
#10 (permalink)
|
|
Registered User
Join Date: Jul 2006
Posts: 9
|
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?
|
|
|
07-14-2006, 07:00 AM
|
#11 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,705
|
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.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
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:13 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|