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;
}