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
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
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.
A structure is basically a collection of one or more variables, possibly of different types.
Illustration:
struct foo {
int a;
int b;
};
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:
Note:
Great book: C Programming Language, 2nd Edition (Prentice Hall Software)
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.
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.
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.
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