I have a structure node
which is used to create a binary search tree. Inside each node I am storing a integer KEY
and a corresponding string value
. I am performing some searches within the tree and wish to return arrays containing key value pairs of only specific nodes.
TO do so I am passing arrays by reference and saving the integer KEY
to that array. This works fine, however when I try to the the same with the string I am getting poor results.
In the below code I am trying to copy the string inside root[count].value;
to p_value_arr[*p_unique_count]
which is a char array.
Struct definition:
typedef struct node {
int KEY;
char *value;
int node_count;
struct node *left, *right;
int unique_count;
} node;
Function to traverse graph and copy unique key value pairs. KEY
is being copied correctly to an array while value
is not.
void unique_key(node *root, int *p_unique_count, int p_unique_arr[], char *p_value_arr[]) {
int count = 0;
//unique *temp = (unique *)malloc(n * sizeof(unique));
if (root != NULL)
{
unique_key(root->left, p_unique_count, p_unique_arr, p_value_arr);
if (root->node_count == 1) {
root[count].unique_count = *p_unique_count;
p_unique_arr[*p_unique_count] = root[count].KEY;
printf("%s\n", root[count].value);
//"warning: assignment makes integer from pointer without a cast"
strcpy(p_value_arr[*p_unique_count],root[count].value);
printf("%d(%d) -> %s %d\n", root->KEY, root->node_count, root->value, root->unique_count);
(*p_unique_count)++;
count++;
}
unique_key(root->right, p_unique_count, p_unique_arr, p_value_arr);
}
}
A utility function to insert a new node with given key in BST
node* insert_node(node* node, int key, char *value)
{
/* If the tree is empty, return a new node */
if (node == NULL)
return newNode(key,value);
// If key already exists in BST, icnrement count and return
if (key == node->KEY)
{
(node->node_count)++;
// return node;
}
/* Otherwise, recur down the tree */
if (key < node->KEY)
node->left = insert_node(node->left, key, value);
else
node->right = insert_node(node->right, key, value);
/* return the (unchanged) node pointer */
return node;
}
node *newNode(int KEY, char *value)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->KEY = KEY;
strcpy(temp->value, value);
temp->left = temp->right = NULL;
temp->node_count = 1;
return temp;
}
Main driver code
int main() {
int unique_count = 0;
int in_count = 0;
int unique_arr[10]; /
char *value_arr[10]; // an array of pointers
/* Let us create following BST. Passing values along with key */
node *root = NULL;
//this is for storing commands
root = insert_node(root, 2, "Hello");
root = insert_node(root, 3, "Thanks");
printf("\nkeys of the given tree \n");
unique_key(root, &unique_count, unique_arr, *value_arr);
for(int i = 0; i < 10; i++) {
printf("%d %s\n", unique_arr[i], value_arr[i]); //Mismatching the argument type "char" and conversion specifier "s" and nothing prints here
}
}
Output:
Hello
keys of the given tree
Segmentation fault
Any suggestions on how I can effectively copy a string inside a struct member to an array of chars?
EDIT:
Full code: https://pastebin.com/CB4Gp0gY
Since char *value_arr[10];
is an array of pointers I followed chapter 5.6 of K&R The C programming language to pass the array of pointers to the function. I get no warnings now, but the seg fault persists.
I also have more warnings
set on my NetBeans 8.2.
Output from debugger:
/cygdrive/C/Users/****/AppData/Roaming/NetBeans/8.2/bin/nativeexecution/dorun.sh: line 71: 16516 Segmentation fault (core dumped) sh "${SHFILE}"