Comparing a pointer with integer - c

I am making a binary search tree program and I want to traverse to the node whose left or right child is to be selected for inserting the element. Whereas in the while condition I am getting warning of comparison of pointer with integer. Help me
while(temp1->data != NULL)
{
temp2=temp1;
if(value<=temp1->data)
{
temp1=temp1->lchild;
}
else
{
temp1=temp1->rchild;
}
}

Your while loop test is wrong. It should be testing for a null pointer to a node, like this:
while(temp1 != NULL) // <-- this line was wrong
{
temp2=temp1;
if(value<=temp1->data)
{
temp1=temp1->lchild;
}
else
{
temp1=temp1->rchild;
}
}

Related

does this (existe element ) function work properly using recursion

i created a function to detect if a word does exist in a linked list with recursion.
i wanted to know if its correct or not .
bool does_exist_in_list(index *head,char word[25]){
while( head != NULL ){
//to detect the first element
if(strcmp(word,head->word) == 0)
return true;
else{
//to go to the next element
return does_exist_in_list(head->next,word);
}
}
}
because im using it in a long code so i dont know if there is a problem in it
The while loop is misleading, as the function always returns during the first iteration.
Additionally, if head is NULL, the function fails to return a value at all, invoking Undefined Behaviour.
bool does_exist_in_list(index *head, char word[25])
{
if (!head)
return false;
if (0 == strcmp(word, head->word))
return true;
return does_exist_in_list(head->next, word);
}

Returning an empty item in a C function call

I want to return the last in a stack. Something like the following:
Item* get_last_item()
{
if (item_stack_size == 0) {
// return <nothing> ?
} else {
return ItemStack[item_stack_size-1];
}
}
What is the suggested practice when returning the equivalent of a null value if the stack is empty? Should this usually issue a hard error? Something like a value of (Item*) 0, or what's the suggested practice for doing something like this? My first thought was to do something like this, but I'm not sure if there's a better way:
Item* get_last_item()
{
return (item_stack_size != 0) ? ItemStack[item_stack_size-1] : (void*) 0;
}
For functions returning a pointer, a NULL pointer can be used as an out of band value( the caller of course should check the pointer for NULL, before dereferencing it):
Item *pop_last_item()
{
if (!item_stack_size) {
return NULL;
} else {
return ItemStack[--item_stack_size];
}
}

Segmentation Fault. Runtime error

I'm writing a code that should read data from a file and then add them to a linked list , following is the code to read from the file:
void readFromFile(List l)
{
system("cls");
FILE *eqFile;
string readFile,line;
printf("\n\t\t\tEnter the title of the file to read equations from\n\t\t\t");
scanf("\t\t\t%s",readFile);
eqFile=fopen(readFile,"r");
/*To make sure that the file does exist*/
while (eqFile == NULL)
{
printf("\n\t\t\tERROR!! The file you requested doesn't exist. Enter the title correctly please\n\t\t\t");
scanf("\t\t\t%s",readFile);
eqFile=fopen(readFile,"r");
}
while (fscanf(eqFile,"%s",line) != EOF)
{
addNode(l,line);//create a new node and fill it with the data
count++; //Counter to count the number of nodes in the list
}
fclose(eqFile);
system("cls");
printf("\n\t\t\tDATA READ SUCCESSFULLY!\n\t\tPress any key to return to the menu.\t\t\t");
getch();
menu();
}
But it gives runtime error when I worked in the debug mode I found out that the problem is in the "addNode" function, following is the function :
/*Function that adds a node to the list*/
void addNode(List l, string s)
{
position temp,temp2;
temp2=l;
temp = (position)malloc(sizeof(struct node));
if(temp == NULL)
printf("\n\t\t\tSORRY! MEMORY OUT OF SPACE!!\n");
else
{
strcpy(temp->eq,s);
while(temp2->next != NULL)
{
temp2=temp2->next;
}
(temp2)-> next= temp;
}
}
The error happens at this statement :
while(temp2->next != NULL)
{
temp2=temp2->next;
}
I looked for the reason of the error and I found that it occurs when I try to access something that can't be accessed by memory. But I have used this statement several times before in different codes and it didn't cause any problem. Could any one help me please telling me what's wrong with my code? Or how can I avoid this error?
For linked list, addition of new node must have it's next as null, if you are adding the node at the end.
I personally do not like this syntax.
while(temp2->next != NULL)
{
temp2=temp2->next;
}
The following seems a lot safer to me.
for (position p = temp2; p != NULL; p = p->next)
{
}
The second version would've prevented the segmentation fault in your code.

Returning error when traversing through a tree

I am trying to calculate the frequency of each node as I add them to the tree, instead of inserting a new element. For some reason when a comparing a new key to every element in the current tree, the if statement will not return 1 if they are both identical. BUT, the function will still add 1 to the frequency of the existing node. This is very puzzling to me, as I don't know why it would skip over the return 1, and continue searching through the tree. Thank you for help/advice in advance.
struct:
typedef struct node {
char* key;
struct node *left;
struct node *right;
int height;
int frequency;
}node;
This is my parsing function:
while(fgets(str, 100, textFile)) {
token = strtok(str," \n");
while (token != NULL)
{
key = strdup(token);
if((sameFrequency(root, key)==1)&&root!=NULL) {
printf("%s", key);
free(key);
token = strtok (NULL, " \n");
}
else {
root = insert(root, key);
//printf("%s\n", key);
free(key);
token = strtok (NULL, " \n");
}
}
if(ferror(textFile))
{
printf("you done messed up a-a-ron");
return(0);
}
}
Function to check the frequency of each node:
int sameFrequency(node *node, char* key) {
if (node != NULL) {
if(strcmp(key, node->key)==0){ //This statement is true in some cases, but will not return the 1
node->frequency = node->frequency+1;
printf("%d\n",node->frequency);
return 1;
}
sameFrequency(node->left, key);
sameFrequency(node->right, key);
}
else return 0;
}
Input would look something like this:
wrn69 flr830 flr662 flr830 flr830
flr231
The output (after printing in preOrder):
key: wrn69, frequency: 1
key: flr830, frequency: 3
key: flr662, frequency: 1
key: flr231, frequency: 1
key: flr830, frequency: 1
key: flr830, frequency: 1
I want this to print everything shown, but I don't want the same key to be inserted into the tree, just incremement its frequency by 1.
TL;DR: Function skipping over return value, but still running code in the if statement, have no idea whats wrong, even after debugging.
I'm not sure what your code is trying to do, since you do not define your node struct, however your function int sameFrequency(node *node, char* key) has an obvious bug: not all code paths return a value. Reformatting a bit for clarity, you can see that if strcmp(key, key)!=0 then the return is undefined:
int sameFrequency(node *node, char* key) {
if (node != NULL) {
if(strcmp(key, node->key)==0){
node->frequency = node->frequency+1;
printf("%d\n",node->frequency);
return 1;
}
else {
sameFrequency(node->left, key);
sameFrequency(node->right, key);
// Continue on out of the "if" statements without returning anything.
}
}
else {
return 0;
}
// NO RETURN STATEMENT HERE
}
My compiler generates a warning for this:
warning C4715: 'sameFrequency' : not all control paths return a value
Surely yours must be doing so as well, unless you intentionally disabled them. Such warnings are important, and should always be cleared up before finishing your code.
I'm guessing you want to do something like this, perhaps?
int sameFrequency(node *node, char* key) {
if (node != NULL) {
if(strcmp(key, node->key)==0){
node->frequency = node->frequency+1;
printf("%d\n",node->frequency);
return 1;
}
else {
int found;
if ((found = sameFrequency(node->left, key)) != 0)
return found;
if ((found = sameFrequency(node->right, key)) != 0)
return found;
return 0;
}
}
else {
return 0;
}
}
This clears the compiler warning.
Incidentally, the following if statement is probably in the wrong order:
if((sameFrequency(root, key)==1)&&root!=NULL) {
Since && statements in C execute left to right the following makes more sense:
if(root!=NULL && (sameFrequency(root, key)==1)) {

How do i check that the next element in the list isnt out of bounds?

So i am trying to prevent the segfault that is occuring in the following code. It is occuring because i am trying to access the next element in the list that does not exist yet. How do i prevent this?
while (vm->item_list.head->data !=NULL) {
printf("%-10s",vm->item_list.head->data->id);
printf("%-20s",vm->item_list.head->data->name);
printf("%-70s",vm->item_list.head->data->description);
printf("%-15d",vm->item_list.head->data->on_hand);
printf("%s","$");
printf("%d",vm->item_list.head->data->price.dollars);
printf("%s",".");
printf("%02d",vm->item_list.head->data->price.cents);
printf("\n");
vm->item_list.head = vm->item_list.head->next;
}
printf("\n");
}
use something like while ((vm->item_list.head != NULL ) && (vm->item_list.head->data !=NULL))
In the initialization phase of the list, you have to declare the end of the list, in this case your next pointer, with NULL. If you do this, than you ensure that this pointer doesn't point in a undefined region.
So than you can check like this:
while (vm->item_list.head->data !=NULL) {
printf("%-10s",vm->item_list.head->data->id);
printf("%-20s",vm->item_list.head->data->name);
printf("%-70s",vm->item_list.head->data->description);
printf("%-15d",vm->item_list.head->data->on_hand);
printf("%s","$");
printf("%d",vm->item_list.head->data->price.dollars);
printf("%s",".");
printf("%02d",vm->item_list.head->data->price.cents);
printf("\n");
if(vm->item_list.head->next != NULL){
vm->item_list.head = vm->item_list.head->next;
}else{
break;
}
}
Or you can do it like this:
ItemNode* node = vm->item_list.head;
while (node->data !=NULL || node->next != NULL) {
printf("%-10s",node->data->id);
printf("%-20s",node->data->name);
printf("%-70s",node->data->description);
printf("%-15d",node->data->on_hand);
printf("%s","$");
printf("%d",node->data->price.dollars);
printf("%s",".");
printf("%02d",node->data->price.cents);
printf("\n");
node = node->next;
}

Resources