Skip to content

Instantly share code, notes, and snippets.

@geekskick
Created December 2, 2017 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geekskick/f853ed9d71421e32e4dd514f383062be to your computer and use it in GitHub Desktop.
Save geekskick/f853ed9d71421e32e4dd514f383062be to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
enum{TITLE_MAX = 1024};
typedef struct Story
{
char title[TITLE_MAX];
char file_a[PATH_MAX];
char file_b[PATH_MAX];
struct Story *right;
struct Story *left;
char *text;
}Story;
struct Story * initTree(char*,char*,char*,char*);
struct Story * addRight(struct Story * root,char*,char*,char*,char*);
struct Story * addLeft(struct Story * root,char*,char*,char*,char*);
int main()
{
struct Story *root = initTree("Chapter 1", "Chapter 2", "Chapter 3", "text");
// You have assigned another chunk of memory here to point to a story ...
//struct Story **head = (struct Story **) malloc(sizeof( struct Story *));
//
//if(head == NULL)
//{
// printf("memory not allocated\n");
//}
struct Story **head;
// ... then immediately lost the pointer by assigning it the address of something else
head = &root;
// Now Current points to address of root.
struct Story *current = *head;
current = addRight(current, "Chapter 2", "Chapter 4", "Chapter 5", "text");
current = addLeft(current, "Chapter 3", "Chapter 6", "Chapter 7", "text");
//current = *head->right; //( ERROR: head is a pointer...)
current = (*head)->right;
current = addRight(current, "Chapter 4", "Chapter 8", "Chapter 9", "text");
current = addLeft(current, "Chapter 5", "Chapter 10", "Chapter 11", "text");
//current = *head->left;
current = (*head)->left;
current = addRight(current, "Chapter 6", "Chapter 12", "Chapter 13", "text");
current = addLeft(current, "Chapter 7", "Chapter 14", "Chapter 15", "text");
// This give an error because head contains the address of root
// free(head);
printf("I am freeing %p\n", *head);
free(*head);
}
struct Story * addLeft(struct Story * current, char t[TITLE_MAX], char fa[PATH_MAX], char fb[PATH_MAX], char *text)
{
if(current == NULL)
return NULL;
struct Story * left = (struct Story*) malloc(sizeof(struct Story));
printf("addLeft:\tI have allocated %p on the heap\n", left);
if(left == NULL)
{
printf("memory not allocated(addLeft)\n");
return NULL;
}
strcpy(left->title,t); // bilo : left->title = t; ERROR
left->text = text;
left->right = NULL;
left->left = NULL;
strcpy(left->file_a,fa);
strcpy(left->file_b,fb);
current->left = left;
return current;
}
struct Story * initTree(char t[TITLE_MAX], char f_a[PATH_MAX], char f_b[PATH_MAX], char *text)
{
struct Story* story = (struct Story*) malloc(sizeof(struct Story));
printf("initTree:\tI have allocated %p on the heap\n", story);
if(story == NULL )
{
printf("memory not allocted (initTree)\n");
return NULL;
}
strcpy(story->title, t);//story->title = t; ERROR
story->text = text;
story->right = story;
story->left = story;
strcpy(story->file_a,f_a); // bilo : story->file_a = f_a; ERROR
strcpy(story->file_b,f_b);
return story;
}
struct Story * addRight(struct Story *current, char t[TITLE_MAX], char fa[PATH_MAX], char fb[PATH_MAX], char *text)
{
// Is this in the right place?
if(current == NULL)
{
printf("memory not allocated (addRight)\n");
return NULL;
}
struct Story * right = (struct Story*) malloc(sizeof(struct Story));
printf("addRight:\tI have allocated %p on the heap\n", right);
strcpy(right->title, t);
right->text = text;
right->right = NULL;
right->left = NULL;
strcpy(right->file_a,fa);
strcpy(right->file_b,fb);
current->right = right;
return current;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment