Home > OS >  Comparing strings in a linked list - Update
Comparing strings in a linked list - Update

Time:03-04

I am trying to read through the file spice.din and make a linked list with the value of each line. If the line already exists in the list it shouldn't create a new node and rather update the counter in that node. I had this working with integers but need to make it work as a string because the file stores things like 1000fff0, or 40bc90. I am learning how bad I am at using pointers and C tonight. Thanks to anyone that can help.

This is the main error I am getting : error: array type 'char [100]' is not assignable new_node->value = value;

I am not sure how to assign the string so that I can compare it in this line : if (current_node != NULL && current_node->value == result)

UPDATE: I am now having trouble comparing the two strings on the lines that say

    char current_value[] = current_node->value;
    int same = strcmp(current_value, result);
    if (current_node != NULL && (same == 0))
      update_node_occurrence(current_node);

I tried initializing the current value given the compiler warnings but that didn't seem to work

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

typedef struct node_t {
  char value[100];
  int occurrences;
  struct node_t *next;
} node;

void update_node_occurrence(node *passed_in_node){
  passed_in_node->occurrences  = 1;
}

node* create_new_node(char *value) {
  node* new_node = malloc(sizeof(node));
  strcpy(new_node->value , value);
  new_node->next = NULL;
  new_node->occurrences = 1;
  return new_node;
}

int main() {
  node *head = NULL;
  node *current_node = head;
  FILE *file = fopen("spice.din", "r");
  char line_in_file[100];
  char result[100];
  if(file == NULL){
    perror("Could not read the file");
    exit(1);
  }

  while(fgets(line_in_file, sizeof(line_in_file), file)) {
    char *result = line_in_file   2;
    char current_value[] = current_node->value;
    int same = strcmp(current_value, result);
    if (current_node != NULL && (same == 0))
      update_node_occurrence(current_node);
    else {
      node *new_node = create_new_node(result);
      if (current_node == NULL)
        head = new_node;
      else
        current_node->next = new_node;
      current_node = new_node;
    }
  }

  for (node *p = head; p != NULL; p = p->next)
    printf("Found %d occurrence(s) of value %s\n", p->occurrences, p->value);
  return 0;
}

CodePudding user response:

Warnings are there to help you, your compiler surely warned you about this line

new_node->value = value;

My compiler wont even compile it

Warning C4047 '=': 'char [100]' differs in levels of indirection from 'char' ConsoleApplication1 C:\work\ConsoleApplication1\ConsoleApplication1.cpp 17

Error C2106 '=': left operand must be l-value ConsoleApplication1 C:\work\ConsoleApplication1\ConsoleApplication1.cpp 17

This line also gets warnings

    printf("Found %s occurrence(s) of value %d\n", p->occurrences, p->value);

Warning C4477 'printf' : format string '%s' requires an argument of type 'char *', but variadic argument 1 has type 'int'

Warning C4313 'printf': '%s' in format string conflicts with argument 1 of type 'int' ConsoleApplication1 C:\work\ConsoleApplication1\ConsoleApplication1.cpp 50

Warning C4477 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 2 has type 'char *' ConsoleApplication1 C:\work\ConsoleApplication1\ConsoleApplication1.cpp 50

The compiler is trying to help you

The main error is fixed like this

node* create_new_node(char *value) {
    node* new_node = malloc(sizeof(node));
   strcpy(new_node->value , value);

CodePudding user response:

New answer for this chunk of code

while (fgets(line_in_file, sizeof(line_in_file), file)) {
    char* result = line_in_file   2;
    char current_value[] = current_node->value;

#1 You already have a variable called result are you sure you want a new one, it will work but my compiler warns me.

Severity Code Description Project File Line Suppression State Warning C4456 declaration of 'result' hides previous local declaration ConsoleApplication1 C:\work\ConsoleApplication1\ConsoleApplication1.cpp 36

looking at the code you dont need the first one, take it out

#2

this line

 char current_value[] = current_node->value;

doesnt compile

Severity Code Description Project File Line Suppression State Error C2075 'current_value': initialization requires a brace-enclosed initializer list ConsoleApplication1 C:\work\ConsoleApplication1\ConsoleApplication1.cpp 37

char * current_value = current_node->value;

the strcmp is fine

EDIT:

Nope the code is still bad.

node* head = NULL;  <<<<==== head is NULL
node* current_node = head;   << =========now current_node is NULL
FILE* file = fopen("spice.din", "r");
char line_in_file[100];
//char result[100];
if (file == NULL) {
    perror("Could not read the file");
    exit(1);
}

while (fgets(line_in_file, sizeof(line_in_file), file)) {
    char* result = line_in_file   2;
    char *current_value = current_node->value; <<<=== still NULL => code go bang here
    int same = strcmp(current_value, result);

look at current_node above, you are always going to fail (my compiler warned me about that too)

  • Related