Skip to content

Instantly share code, notes, and snippets.

@sojohnnysaid
Created November 7, 2018 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sojohnnysaid/0a350ce24f7966039e8d67d8a76a9134 to your computer and use it in GitHub Desktop.
Save sojohnnysaid/0a350ce24f7966039e8d67d8a76a9134 to your computer and use it in GitHub Desktop.
#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