Detailed Guide to Structures in C

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

I did a Google search, but I find normal information. I am looking for structures in detail. Please let me know.

+6
c structure
source share
7 answers

You can always read the Brian W. Kernighan Tutorial , which is not bad. As for "detailing," what exactly do you mean? In the end, the structures are pretty simple. The above link covers common problems, typedefs, pointers to struct, etc. If you still do not understand the structure, perhaps you should try posting a few more specific questions.

+8
source share

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

struct Point location; 

Because of this, you will often see that they are declared using typedef as follows:

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

Then you can declare a point, like other types:

 Point location; 

This post can be confusing when you first see it.

+4
source share

I usually advised (and always advised) to use official manuals. For the ANSI C case, you cannot get more detailed and official than K & R2 .

+3
source share

Structure represents a set of one or more variables , possibly from different types .

Illustration:

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

Edit:

The structure element is called as follows:

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

For example:

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

You can do other things, for example:

  • Pointers to structures
  • Arrays of Structures

Note:

  • If the structure is large enough, it’s more efficient to pass a pointer to the structure, rather than the structure itself

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

  • Great introduction to structures
+1
source share

As others have said, a structure is simply a grouping of variables. Depending on your purpose, you may need to consider registration or packaging if you want to access elements through low-level functions (for example, using pointer arithmetic): structure elements are usually aligned at 4 byte boundaries (32 bits). Thus, a structure that includes elements of different sizes may be required to complement

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

In this example (assuming int is 4 bytes and char is 1 byte, and the CPU is aligned with 32-bit boundaries), you need 3 bytes of padding after b to align the structure. In this case, it is more efficient to sort the structure in different ways. Please note that this will not change its use, since it does not change the names of participants. Not all processors need a structure that needs to be aligned, but using elements of a packaged structure to save some space can result in a speed penalty. In most cases, you have nothing to worry about.

As with typedef around a structure, you can use the same name for typedef and struct, i.e. something like that:

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

This allows you to use

 struct foo variable; 

or

 foo variable; 

to declare a variable of type struct foo .

From my point of view, typing a structure is a bad idea, as it hides the information that typedef (e.g. foo ) is a structure, and writing the struct keyword is not really an additional work (especially if these typedefs add a postfix so that indicate that they are a structure - I saw this quite often).

Edit : in the second example, there is no typedef .

+1
source share

Check out book algorithms in C from Sedgewick. There you see a lot of different datastructues at work. Another interesting read is the gobject model from gtk +. Because there they build the infrastructure for object-oriented programming in C, which Datastructure handles a lot. Another thing worth noting is the COM model, which you can translate to a single C structure with a specific layout. Glib contains many useful data structures, such as libapr or libnspr.

You can check almost any “scripting” language and see “data structures at work”; -)

Relations Friedrich

0
source share

It's hard to argue against the awesomeness of Nick Parlante's linked list repository:

http://cslibrary.stanford.edu/

For structures to use in linked lists, the referenced link is unsurpassed.

What you will not cover is the aspect of using functions within structures, but if you can survive it, everything else will be a breeze.

0
source share

All Articles