Thread: pointers to struct - help please

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    1

    pointers to struct - help please

    hi,
    i'm really new to c, so this might be trivial, but i'll ask anyway.

    i have to implement a simple malloc/free. due to some restrictions (the 'memory' is array of ints, no static variables (all the housekeeping inside the array itself)) i went for what seemed as the most obvious - my malloc now puts headers in front of the data, in the form of a couple of indexes to the array. the code looks awful with all those [i + 1 + smth] and similar indexes.
    now i'm thinking if i could possibly make it prettier with structs? say, if i were given a pointer to an int (which would point to beginning of the header bit), can i cast it to struct header *, so that instead of having all those array indexes i could just say h.next, h.prev, h.size and so on? if so, could you show some code snippet? because i've been trying around, but i'm not quite sure what would be the right way. or, if you have any better ideas how to make my malloc better, please share them as well

    thanks a lot

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    9
    Google data encapsulation and ADT.

    Structures are used in C similarly to how classes are used in C++. You use them when you want to capture a real world object.

    If you want to represent a bike. You would have a structure called bike, and have members such as wheels, handle bars etc.

    Just create a global structure that represents whatever it is you are trying to do. And create pointers to that structure. You can dereference the pointer and have access to your struct.

    In terms of prettier code - it takes a lot of experience, not just at mastering the logistics of programming, i.e pointers, memory etc. But the actual engineering behind decomposing a problem and solving it in the best possible way.

    If you interested in good programming, I know there are a lot of books about data abstraction, encapsulation etc.

    Good luck

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Perhaps linked list is what you have in mind. The FAQ has a nice writeup on it http://faq.cprogramming.com/cgi-bin/...&id=1073086407

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  4. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM
  5. Pointers on pointers to pointers please...
    By Morgan in forum C Programming
    Replies: 2
    Last Post: 05-16-2003, 11:24 AM