Thread: need help with code!! please!!

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    1

    Post need help with code!! please!!

    What does this code do?? can anyone explain with comments??.. i make out some of it butttttttt we never really learned the struct part. And whats the size of a struct?? We also never learned the (->) is that another way of using a pointer?? Thanks


    Code:
    #include "includes.h"
    
    /* 
    struct foo2 {
       int          val;
       struct foo2   *next;
    };
    */
    
    
    void function2 (int arg1)
    {
       int          ii;
       struct foo2   *head = NULL;
       struct foo2   *item;
    
       for (ii = 0; ii < 16; ii++) {
          item = (struct foo2 *) malloc(sizeof(struct foo2));
    
          if ( (arg1 >> ii) & 0x1)
             item->val = 1;
          else
             item->val = 0;
    
          if (head == NULL) {
             head = item;
             item->next = NULL;
          }
          else {
             item->next = head;
             head = item;
          }
       }
    
       printf("%d: ", arg1);
    
       item = head;
       while (item != NULL) {
          printf("%d", item->val);
          item = item->next;
       }
       printf("\n");
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    #include "includes.h"   // Some library that someone created that
                            // I assume (and I shudder) contains any standard libraries 
                            // they need
    
    struct foo2 {           // Declares a struct name foo2 which contains...
       int          val;    // an integer...
       struct foo2   *next; // and a pointer to another foo2 object.
    };                      // This, by the way, is common format for most data
                            // structures, in this case a linked list
    
    void function2 (int arg1)
    {
       int          ii;
       struct foo2   *head = NULL;   // Declares a pointer to a foo2 object and initializes to NULL
       struct foo2   *item;          // ...again... no initialization
    
       for (ii = 0; ii < 16; ii++) {
          item = (struct foo2 *) malloc(sizeof(struct foo2)); // Allocates memory for the item pointer
                                        // This memory is equal to the size of a foo2 object (see below)
          if ( (arg1 >> ii) & 0x1)      // Some bitwise operations to determine
             item->val = 1;             // the value of the foo2's member val
          else      
             item->val = 0;
    
          if (head == NULL) {           // If the pointer head is pointing to null
             head = item;               // point to the newly allocated item
             item->next = NULL;         // point it's next member to null
          }
          else {                        // otherwise...
             item->next = head;         // point it's next member to head
             head = item;
          }
       }
    
       printf("%d: ", arg1);
    
       item = head;                     // set your current item to head (the beginning)
       while (item != NULL) {           // while item doesn't point to NULL (which the last member does)
          printf("%d", item->val);      // output
          item = item->next;            // point to your next member.
       }
       printf("\n");
    }
    The size of a struct is equal to the size of it's members and inline functions. The -> operator is the same as a . operator except that your object is a pointer.

    Code:
    struct foo {
       int blah;
    };
    
    int main() {
       struct foo bar;      // A regular object
       struct foo *baz;     // A pointer
    
       *baz = (struct foo *) malloc(sizeof(struct foo)); // Allocate for the pointer
    
       bar.blah = 5;  // . operator for regular objects
       baz->blah = 6; // -> operator for dynamically allocated objects
    
      return 0;
    }
    Last edited by SlyMaelstrom; 02-22-2006 at 02:07 AM.
    Sent from my iPad®

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > The size of a struct is equal to the size of it's members and inline functions.
    C doesn't have inline or member functions, and they're not part of the size of the struct anyway, even if it was C++.

    > item = (struct foo2 *) malloc(sizeof(struct foo2));
    Do not cast malloc in C, see the FAQ

    Other than that, a good explanation

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Salem
    > The size of a struct is equal to the size of it's members and inline functions.
    Yeah, I got that from a teacher last semester and I've written it on so many tests, it's become habitual to write that, sorry. And about casting malloc, agreed, I just kinda copied it from his code without thinking.

    ...and by the way, to the OP, that code is naughty because it never frees the pointers before it leaves the function.
    Last edited by SlyMaelstrom; 02-22-2006 at 02:10 AM.
    Sent from my iPad®

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    5
    Hey here someone is trying to write a program to implement Linked List.
    Actually Linked list is a type of data structure. There are other data structures too like Queue,Graph etc.
    A data structure is a logical structure in which we can store information efficiently.
    Information stored in a data structure can be retrieved efficiently and fastly. There are many other advantages of using a data structure.
    To implement a data structure in a c program you should have enough knowledge about how to use pointers, Dynamic memory allocation and structures.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    And your point is what RoshanGautam?

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    5
    What i want to say is that you should have a detailed description of what you want to do otherwise it will be of no use, isn't it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM