Skip to content

Commit 85198f5

Browse files
author
Joshua Goller
committed
const correct crossref
1 parent f8661e4 commit 85198f5

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

ch-6/crossref/include/crossref.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ typedef struct word_node {
1111
} word_node;
1212

1313
// io.c
14-
int getword(char* word);
14+
int getword(char* const word);
1515

1616
// node.c
17-
int add_line(word_node* node, size_t line_no);
18-
void display_lines(word_node* node);
17+
word_node* create_node(const char* const word);
18+
int add_line(word_node* const node, const size_t line_no);
19+
void display_lines(const word_node* const node);
1920

2021
// tree.c
21-
word_node* create_node(char* word);
22-
word_node* tree_insert(char* word, word_node* node);
23-
word_node* tree_search(char* word, word_node* node);
24-
void tree_walk(word_node* head);
22+
word_node* tree_insert(const char* const word, word_node* const node);
23+
word_node* tree_search(const char* const word, const word_node* const node);
24+
void tree_walk(const word_node* const head);
2525
void tree_cleanup(word_node* head);

ch-6/crossref/src/io.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static int is_noise_word( char* word, word_node* noise_words) {
1515

1616
// getword(): get the next word from input. Returns the last
1717
// char read, or EOF
18-
int getword(char* word) {
18+
int getword(char* const word) {
1919
int i = 0;
2020
int c = 0;
2121
c = getchar();

ch-6/crossref/src/node.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "crossref.h"
77

88
// create_node(): allocate a new node
9-
word_node* create_node(char* word) {
9+
word_node* create_node(const char* const word) {
1010
if (word == NULL) {
1111
printf("create_node() | cannot create node for null string.\n");
1212
return NULL;
@@ -47,7 +47,7 @@ word_node* create_node(char* word) {
4747
}
4848

4949
// add_line(): add a line number to a node's list of lines
50-
int add_line(word_node* node, size_t line_no) {
50+
int add_line(word_node* const node, const size_t line_no) {
5151
if (node == NULL) {
5252
return -1;
5353
}
@@ -76,7 +76,7 @@ int add_line(word_node* node, size_t line_no) {
7676
}
7777

7878
// display_line(): display all lines in line buffer
79-
void display_lines(word_node* node) {
79+
void display_lines(const word_node* const node) {
8080
printf("%s (n: %lu, max: %lu): ", node->word, node->lines_n, node->lines_max);
8181
for (size_t i = 0; i < node->lines_n; i++) {
8282
printf("%lu ", node->lines[i]);

ch-6/crossref/src/tree.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
#include <stdlib.h>
33
#include <string.h>
44

5+
#pragma clang diagnostic ignored "-Wcast-qual"
6+
57
#include "crossref.h"
68

79
// tree_insert(): insert word in tree
8-
word_node* tree_insert(char* word, word_node* node) {
10+
word_node* tree_insert(const char* const word, word_node* const node) {
911
if (node == NULL) {
1012
return NULL;
1113
}
@@ -36,15 +38,15 @@ word_node* tree_insert(char* word, word_node* node) {
3638
}
3739

3840
// tree_search(): return node in tree storing word, or NULL
39-
word_node* tree_search(char* word, word_node* node) {
41+
word_node* tree_search(const char* const word, const word_node* const node) {
4042
if (node == NULL) {
4143
return NULL;
4244
}
4345

4446
// walk tree based on comparison to word
4547
int result = strcmp(node->word, word);
4648
if (result == 0) {
47-
return node;
49+
return (word_node*)node;
4850
} else if (result < 0) {
4951
return tree_search(word, node->left);
5052
} else {
@@ -53,7 +55,7 @@ word_node* tree_search(char* word, word_node* node) {
5355
}
5456

5557
// tree_walk(): post-order walk the tree, printing strings and line buffers
56-
void tree_walk(word_node* head) {
58+
void tree_walk(const word_node* const head) {
5759
if (head == NULL) {
5860
return;
5961
}

0 commit comments

Comments
 (0)