If you find this article contains errors or problems rendering it unreadable (missing images or files, mangled code, improper text formatting, etc) please contact the editor so corrections can be made. Thank you for helping us improve this resource
In the first article, we discussed briefly how you would go about referencing and dereferencing pointers. Now that we have that basis, we can continue on our quest to create a Linked-List.
Structures are considered complex data types. They are considered complex because they are more than just a single simple data type. The simple data types are those that are native to the C/C++ language in the form of int, float, double, char, long, etc. and pointers. A structure usually consists of simple data types and sometimes of functions as well (though I will not go into function pointers for the sake of simplicity).
A simple definition of a structure goes along these lines:
Now that we have a simple structure to play with, you are probably asking how to get to those variables. Well, it is quite simple really. To address a member of a structure you follow this simple procedure:
So if I wanted to access Name, Address or PhoneNo then I would need to go
You can then use simple string modifiers (like 'strcpy', 'strcat', 'strcmp' in the 'string.h' header file) to change the values in this structure. You might be wondering when I defined the structure why I defined a pointer to that structure called MyAddressPtr. Well, pointers to structures act a little differently, so we shall cover them so that when we come to linked lists you won't be surprised with all of the strange symbols.
If you have a pointer to a structure and you wish to address the members of that structure, then you must follow a different procedure than that in the simple structure member selection. First, you must make sure that your pointer is pointing to an already defined (or 'malloc'ed) structure. Otherwise you will have memory errors and these can have undefined results, most often resulting in either program termination or the computer crashing/hanging. To make sure that our pointer is referencing something, we go like this:
MyAddressPtr = &MyAddress;
Just like in our pointers example. Now, if we want to access the members of the MyAddress structure then we will need to use the structure member reference. This is not the '.' that we used in the previous example, but the '->' which looks, not coincidentally, like an arrow. This is a member selector for structure/class pointers. Used in the same way as the simple member selector we go
You may be asking why we even bother with pointers for this. It is true that in this example that pointers don't actually do much for us. When it comes to linked lists, though, structure pointers are always helpful (and necessary). I will cover dynamic memory allocation in the linked lists part of this example, but for now I will go on to defining another structure with the same type as the first.
You have already defined your structure above and the structure tag was defined as strAddresses. The 'str' on the beginning of the tag is just something that I presonally use to determine that I am refering to a structure. Other people use 'struc' or 'struct' as their tag prefix or '_s' as their suffix. It really is up to personal choice, and it is not necessary, but it makes finding your variables a bit easier. Now, to define another structure that is the same as the last we do this:
As you can see, we redefine using the 'struct' keyword and specify what kind of structure it is using the 'strAddresses' tag and define the new structure variable 'NewAddress'. Fairly simple huh? OK, there is a simpler way than having to go through writing struct all of the time. It is time that you were introduced to typedefs.
Typedefs are just definitions of types. A type is what you use to create a variable. Int, char, float, double etc are all types. They are built in, but you can define new types by using the typedef keyword. A common typedef is as follows:
This defines a new type called 'BYTE' which is the same type as an 'unsigned char'. This really doesn't do anything other than make it easier to define your own custom type names for the simple types. If we then wanted to define a new 'BYTE' variable then we would go
You can use the same idea for structures to make it simpler to create new instances of a certain type of structure. I will be using typedefs all through the linked list part of this series of articles, so you are going to have to get used to them. Here is how we would define a new 'Address_t' and 'AddressPtr_t' type from the structure above.
Then, you could have defined the variables 'MyAddress' and 'MyAddresPtr' like so:
This makes it much easier to define new variables of the same type, because you can just go . Notice that I used the '_t' suffix for my type definition. This becomes useful when you want to create variables which would have the same name as the types (ie, if you wanted an 'Address' variable) because I never use '_t' in a variable name.
I hope that introduced you to the wonderful world of structures and typedefs. In the next section we get our hands dirty with linked lists. Look forward to seeing you then.