d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Structures And Malloc'ing Pointers C
Add Reply New Topic New Poll
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Mar 25 2013 11:13am
Say I have the following struct: (Note the use of linked lists is required, that's why I structured it like I did)

A node is a linked list of nodes, that can contain a linked list of sub-nodes and/or a linked list of categories.

The design of the system is in a picture at the bottom of the thread.

Code
typedef struct node{

char * name[100];
node * sub_node;
node* next;
node* prev;
categories* first;

}Node;

typedef struct categories{

char* cat_name[100];
category* next;
category* prev;
char* category_description;

}Categories;


My question is mostly about mallocing.

How do I malloc the pointers to the next and prev categories? Would creating a file look like this?

new->prev and new->next will be used when I insert the node into an already created linked list, as pointers to the prev node and the next node (depending on where I insert it).

Code
Category* new;
new = malloc(sizeof(struct category)):
new->cat_name = malloc(sizeof(char) * 100);
new->next = malloc(sizeof(struct category));
new->prev = malloc(sizeof(struct category));


And creating a new node?

new->prev and new->next will be used when I insert the node into an already created linked list.

Code
Node* new;
new = malloc(sizeof(struct node)):
new->name = malloc(sizeof(char) * 100);
new->next = malloc(sizeof(struct node));
new->prev = malloc(sizeof(struct node));
new->sub_node = malloc(sizeof(struct node));
new->first = malloc(sizeof(struct category));


All sub_directories in the same parent directory are connected, all files in the same parent directory are connected.



This post was edited by lopelurag on Mar 25 2013 11:32am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll