C Programming/Complex types

From Wikibooks, open books for an open world
Jump to: navigation, search
Previous: Strings Index Next: Networking in UNIX

In the section C types we looked at some basic types. However C complex types allow us greater flexibility in managing data in our C program.

Data structures[edit]

A data structure ("struct") contains multiple pieces of data. Each piece of data (called a "member") can be accessed by the name of the variable, followed by a '.', then the name of the member. (Another way to access a member is using the member operator '->'). The member variables of a struct can be of any data type and can even be an array or a pointer.


Pointers[edit]

Pointers are variables that don't hold the actual data. Instead they point to the memory location of some other variable. For example,

   int *pointer = &variable;

defines a pointer to an int, and also makes it point to the particular integer contained in variable.

The '*' is what makes this an integer pointer. To make the pointer point to a different integer, use the form

   pointer = &sandwiches;

Where & is the address of operator. Often programmers set the value of the pointer to NULL (a standard macro defined as 0 or (void*)0 ) like this:

   pointer = NULL;

This tells us that the pointer isn't currently pointing to any real location.

Additionally, to dereference (access the thing being pointed at) the pointer, use the form:

   value = *pointer;

Structs[edit]

A data structure contains multiple pieces of data. One defines a data structure using the struct keyword. For example,

   struct mystruct
   {
       int int_member;
       double double_member;
       char string_member[25];
   } variable;

variable is an instance of mystruct. You can omit it from the end of the struct declaration and declare it later using:

struct mystruct variable;

It is often common practice to make a type synonym so we don't have to type "struct mystruct" all the time. C allows us the possibility to do so using a typedef statement, which aliases a type:

typedef struct
{
  ...
} Mystruct;

The struct itself has no name (by the absence of a name on the first line), but it is aliased as Mystruct. Then you can use

Mystruct structure;

Note that it is commonplace, and good style to capitalize the first letter of a type synonym. However in the actual definition we need to give the struct a tag so we can refer to it: we may have a recursive data structure of some kind. For trees or chained lists, we need a pointer to the same data type in the struct. During compilation, the type synonym is not known to the compiler and there will be an error. To avoid this, it is necessary to let the compiler know the name right from the start (Note that the struct keyword is used only inside the structure! After the declaration, the compiler knows that the type synonym refers to a struct):

typedef struct Mystruct
{
  ...
  struct Mystruct * pMystruct
} Mystruct;

Unions[edit]

The definition of a union is similar to that of a struct. The difference between the two is that in a struct, the members occupy different areas of memory, but in a union, the members occupy the same area of memory. Thus, in the following type, for example:

union {
    int i;
    double d;
} u;

The programmer can access either u.i or u.d, but not both at the same time. Since u.i and u.d occupy the same area of memory, modifying one modifies the value of the other, sometimes in unpredictable ways.

The size of a union is the size of its largest member.

Type modifiers[edit]

For "register", "volatile", "auto" and "extern", see C Programming/Variables#Other_Modifiers.

Previous: Strings Index Next: Networking in UNIX