Thread: dynamic link in C

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    1

    Talking dynamic link in C

    can anyone please help me.
    i really can't understand the logic of this program.
    i just want to know your opinion by visualy inspecting the code.
    because i really can't understand the for loop part downwards.
    please help and explain. thanks.

    Code:
    #include "stdio.h" /* this is needed only to define the NULL */
    #define RECORDS 6
    main( )
    {
    struct animal {
    char name[25]; /* The animals name */
    char breed[25]; /* The type of animal */
    int age; /* The animals age */
    struct animal *next; /* a pointer to another record of this type */
    } *point, *start, *prior; /* this defines 3 pointers, no variables */
    int index;
    /* the first record is always a special case */
    start = (struct animal *)malloc(sizeof(struct animal));
    strcpy(start ->name,"general");
    strcpy(start ->breed,"Mixed Breed");
    start->next = NULL;
    prior = start;
    /* a loop can be used to fill in the rest once it is started */
    for (index = 0;index < RECORDS;index++) {
    point = (struct animal *)malloc(sizeof(struct animal));
    strcpy(point->name,"Frank");
    strcpy(point->breed,"Laborador Retriever");
    point->age = 3;
    point->next = point; /* point last "next" to this record */
    point->next = NULL; /* point this "next" to NULL */
    prior = point; /* this is now the prior record */
    }
    /* now print out the data described above */
    point = start;
    do {
    prior = point->next;
    printf("%s is a %s,and is %d years old.\n", point->name,
    point->breed, point->age);
    point = point->next;
    } while (prior != NULL);
    /* good programming practice dictates that we free up the */
    /* dynamically allocated space before we quit */
    point = start; /* first block of group */
    do {
    prior = point->next; /* next block of data */
    free(point); /* free present block */
    point = prior; /* point to next */
    } while (prior != NULL); /* quit when next is NULL */
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The first thing to do is to add some indentation to that code. I ran your code through indent with default options and then through codeform:
    Code:
    #include "stdio.h"		/* this is needed only to define the NULL */
    #define RECORDS 6
    main ()
    {
      struct animal
      {
        char name[25];		/* The animals name */
        char breed[25];		/* The type of animal */
        int age;			/* The animals age */
        struct animal *next;	/* a pointer to another record of this type */
      }
       *point, *start, *prior;	/* this defines 3 pointers, no variables */
      int index;
    /* the first record is always a special case */
      start = (struct animal *) malloc (sizeof (struct animal));
      strcpy (start->name, "general");
      strcpy (start->breed, "Mixed Breed");
      start->next = NULL;
      prior = start;
    /* a loop can be used to fill in the rest once it is started */
      for (index = 0; index < RECORDS; index++)
        {
          point = (struct animal *) malloc (sizeof (struct animal));
          strcpy (point->name, "Frank");
          strcpy (point->breed, "Laborador Retriever");
          point->age = 3;
          point->next = point;	/* point last "next" to this record */
          point->next = NULL;	/* point this "next" to NULL */
          prior = point;		/* this is now the prior record */
        }
    /* now print out the data described above */
      point = start;
      do
        {
          prior = point->next;
          printf ("&#37;s is a %s,and is %d years old.\n", point->name,
    	      point->breed, point->age);
          point = point->next;
        }
      while (prior != NULL);
    /* good programming practice dictates that we free up the */
    /* dynamically allocated space before we quit */
      point = start;		/* first block of group */
      do
        {
          prior = point->next;	/* next block of data */
          free (point);		/* free present block */
          point = prior;		/* point to next */
        }
      while (prior != NULL);	/* quit when next is NULL */
    }
    [edit] Okay, now I can read it.

    malloc() shouldn't be casted, and <stdlib.h> should be included so that without the cast you don't get a warning.

    You should also include <string.h>, because of strcpy().

    Also see MacGyver's post below. [/edit]
    Last edited by dwks; 07-12-2007 at 08:03 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Unless you copied it incorrectly, whoever wrote this program needs to learn a thing or two about proper indention. Also, they could stand to learn a lesson about writing correct, portable code.

    All they are doing is allocating memory for a linked list, inserting, printing, and deleting nodes. I'm guessing you just want help on linked lists for the most part:

    http://en.wikipedia.org/wiki/Linked_list
    http://www.cprogramming.com/tutorial/c/lesson15.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ runtime, dynamic link, mingw.
    By 39ster in forum C++ Programming
    Replies: 1
    Last Post: 12-16-2007, 12:00 PM
  2. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  3. Replies: 19
    Last Post: 01-12-2006, 11:04 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Win32 API - Dynamic Link Library Loading - behind the scenes
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 04-05-2002, 08:31 AM