tags:

views:

765

answers:

6

Can anyone provide me a very good tutorial for structures in C?

I have made google search, but I find normal information. I am looking for structures in detail. Kindly let me know. Thanks

+6  A: 

You can always read Brian W. Kernighan's Tutorial, which is quite good. As for "detail" what exactly do you mean? Structures are fairly simple, after all. The above link covers commonly encountered issues, typedefs, pointers to struct, etc. If you're still not clear on structs, perhaps you should try posting some more specific questions.

dwc
+1  A: 

A structure is basically a collection of one or more variables, possibly of different types.

Illustration:

struct foo {
  int a;
  int b;
};
  • Variable names in a structure are called members.

Edit:

A member of a structure is referred to as:

structure_variable.member
structure_pointer->member
(*structure_pointer).member    // Fiddly; use -> instead

E.g.:

struct foo test;
struct foo *ptr = &test;
test.a = 1;
ptr->b = 2;

You can do other things, such as:

  • Pointers to structures
  • Arrays of structures

Note:

  • If the structure is quite large, it is more efficient to pass a pointer to the structure, instead of the structure itself

Great book: C Programming Language, 2nd Edition (Prentice Hall Software)

  • Excellent introduction to structures
Aaron
Nitpick: struct members in the definition are terminated with ";" not ","
dwc
Thanks for pointing this out - i'll changes this...
Aaron
Why down vote???
Aaron
I would say that the comment "a member of a structure is referred to as structure-name.member" is mildly misleading. You use structure_variable.member or structure_pointer->member; the difference is that it is the variable name, not the structure type name that is used. Also (*pointer).member?
Jonathan Leffler
Thanks Jonathan good point.
Aaron
+2  A: 

One thing to keep in mind with structures in C (not C++) is that you have to use the word struct when declaring them like so:

struct Point location;

Because of this, you will often see them declared with a typedef like this:

typedef struct tagPoint {
    int x;
    int y;
    } Point;

Then you can declare a point as you would other types:

Point location;

This notation can be confusing when you first see it.

Steve Rowe
Good point Steve...
Aaron
+2  A: 

I usually advice (and was always advised) to use official guides. For the case of ANSI C you can't get any more detailed and official than K&R2.

Luca Matteis
anon
Dale Hagglund
Plus I don't think C99 is supported by all compilers. Correct me if I'm wrong.
Luca Matteis
Well, it's not supported by Java compilers, or Fortran compilers, or C89 compilers, so you're not wrong. But it is supported by all C99 compilers. There may be platforms which don't have a C99 compiler and do have a compiler for a different language, for example C89.
Pete Kirkham
+1  A: 

As others have already said, a structure is simply a grouping of variables. Depending on your target you might need to take padding or packing into account if you want to access the elements via low-level functions (f.e. with pointer arithmetics): The structure elements are usually aligned at 4 Byte boundaries (on 32 bit). Thus, a structure that includes elements of different size might need padding

struct foo {
    int a;
    char b;
    int c;
    char d;
}

In this example (assuming that int is 4 Bytes and char 1 Byte and the CPU aligns at 32bit boundaries) you need 3 padding Bytes after b to get the structure aligned. In that case it can be more efficient to sort the structure differently. Note that this won't change its usage as this doesn't change the member names. Not all processors need the structure menbers to be aligned, but using packed structure members, which would save some space, can result in a speed penalty. In most cases you don't have to worry about this though.

As for typedefs around struct, you can even use the same name for the typedef and the struct, i.e. something like this:

typedef struct foo {
  int a;
  int b;
} foo;

This makes it possible to use

struct foo variable;

or

foo variable;

for declaring a variable of the type struct foo.

From my point of view typdef-ing a structure is a bad idea as that hides the information that the typedef (like foo) is a structure, and writing the keyword struct isn't really additional work (especially if those typedefs get a postfix added to indicate them being a structure -- I've seen this quite often).

Edit: added missing typedef in second example.

bluebrother
Umm ... you forgot the keyword typedef for the second code snippet, else the meaning of 'foo' is changed. foo alone is a variable of type 'struct foo'. So you cannot declare 'struct foo type' variables using just 'foo' as it itself is a variable. The statement 'foo variable;' won't even compile.
xk0der
A: 

Check the book Algorithms in C from Sedgewick. There you an see quite a lot of different Datastructues at work. Another interesting read is the Gobject Model from gtk+. Because there they build an infrastructure for object-oriented programming in C it's extremly much Datastructure handling. Another thing worth a look is the COM model which you can translate to one C structure, with a certain layout. The glib contains tons of useful datastructures as does e.g libapr or libnspr.

You can check nearly any "scripting" language and will see "datastructures at work" ;-)

Regards Friedrich

Friedrich