Character as a node

It errors when i pointer the character to a node how to fix this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  struct Node
{
	char story[1000];
	struct Node *left, *right;
};


struct node *newNode(char letter[1000])//this part errors
{
	struct node *node = (struct node*)sizeof(struct node));
	strcpy(node->story, letter);
	strcpy(node->left, NULL);
	strcpy(node->right, NULL);
	return node;
}
I am honestly not sure what is wrong with your code.
I used this http://www.geeksforgeeks.org/given-a-binary-tree-print-out-all-of-its-root-to-leaf-paths-one-per-line/

as an example to design your function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct node
{
    char story[10000];
    struct node* left;
    struct node* right;
};


struct node* newNode(char* data)
{
    struct node* node = (struct node*)
    malloc(sizeof(struct node));
    strcpy(node->story, data);
    node->left = NULL;
    node->right = NULL;
    return node;
}
Last edited on
I tried the code you sent and how to fix the error in the
1
2
3
4
5
6
void printPathsRecur(struct node* node, int path[], int pathLen)
{
	if (node == NULL) return;

	strcpy_s(path[pathLen] = node->data);
	pathLen++;
part?
I used
1
2
3
4
5
6
struct node
{
	char data[1000];
	struct node* left;
	struct node* right;
};
I fixed it and i run it. and it says strcpy_s does not take 2 arguments
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void printPathsRecur(struct node* node, char path[], int pathLen)
{
	if (node == NULL) return;

	strcpy_s(&path[pathLen], node->data);
	pathLen++;

	if (node->left == NULL && node->right == NULL)
	{
		printArray(path, pathLen);
	}
	else
	{
		printPathsRecur(node->left, path, pathLen);
		printPathsRecur(node->right, path, pathLen);
	}
}
Worth a read:
http://www.cplusplus.com/forum/beginner/118771/
"_s" version is safer because you pass in the length too.
Thank you! I have another problem but i think it's off topic so i'll just add another topic :)
Topic archived. No new replies allowed.