Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 4
struct node{
char word[LENGTH+1];
struct node *next;
};
typedef struct node* node_ptr;
int main(void) {
node_ptr array[1];
// create space on the heap for a node's worth of data
node_ptr node_address = malloc(sizeof(struct node));
// access the word property and give it the value "John\0"
strcpy(node_address->word,"John\0");
// create another node
node_ptr another_node = malloc(sizeof(struct node));
// assign the first node's next property to the address of
// the second node after it is created
node_address->next = another_node;
// access the word property and give it the value "Jane\0"
strcpy(another_node->word,"Jane\0");
// we are not stringing along any other nodes so mark
// the second node's next value as NULL
another_node->next = NULL;
array[0] = node_address;
// we have an array on the stack
// with a single index, holding the address
// of our first node_address.
// can we somehow get the data within the second node?
// We totally can!
printf("%s is here and so is %s! Whoa!", array[0]->word, array[0]->next->word);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment