Skip to content

Instantly share code, notes, and snippets.

@nikAizuddin
Created December 11, 2014 14:05
Show Gist options
  • Save nikAizuddin/c666f9229135e1d8feab to your computer and use it in GitHub Desktop.
Save nikAizuddin/c666f9229135e1d8feab to your computer and use it in GitHub Desktop.
ANSI-C: Nested structure in Linked List.
/**
* ANSI-C: Nested structure in Linked List.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct subject_t {
char name[32];
} subject_t;
typedef struct student_t {
subject_t *subject;
struct student_t *next;
} student_t;
student_t *head_ptr;
student_t *student_ptr;
int main(void)
{
/** Create student 1 */
student_ptr = (student_t *)malloc(sizeof(student_t));
student_ptr->subject = (subject_t *)malloc(sizeof(subject_t));
strcpy( student_ptr->subject->name, "Programming Technique" );
student_ptr->next = NULL;
head_ptr = student_ptr;
/** Create student 2 */
student_ptr->next = (student_t *)malloc(sizeof(student_t));
student_ptr = student_ptr->next;
student_ptr->subject = (subject_t *)malloc(sizeof(subject_t));
strcpy( student_ptr->subject->name, "Data Structure" );
student_ptr->next = NULL;
/** Test, print the value */
student_ptr = head_ptr;
printf( "Student 1 subject = %s\n", student_ptr->subject->name );
student_ptr = student_ptr->next;
printf( "Student 2 subject = %s\n", student_ptr->subject->name );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment