Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Structures

C Structures

C arrays allow you to define variables that store data items of the same type,structuresis another user-defined data type available in C programming that allows you to store data items of different types.

Data members in a structure can be basic data types (such as int, float, char, etc.), as well as other structure types, pointer types, and so on.

A structure is used to represent a record. Suppose you want to track the dynamics of books in a library; you may need to track the following attributes of each book:

Defining a Structure

A structure definition consists of the keyword struct and a structure name, which you can define as needed.

The struct statement defines a new data type containing multiple members. The format of the struct statement is as follows:

struct

tag

{

    member

list

    member

list

    member

list  

    ...

}

variable

list

;

tag is the structure tag.

member-list is a standard variable definition, such as int i; or float f;, or other valid variable definitions.

variable-list Structure variables are defined at the end of the structure, before the final semicolon; you can specify one or more structure variables. Here is how to declare the Book structure:

struct

Books

{

   

char

 title

[

50

]

;

   

char

 author

[

50

]

;

   

char

 subject

[

100

]

;

   

int

  book_id

;

}

book

;

 

In general,tag, member-list, variable-list at least 2 of these 3 parts must be present. Here is an example:

// This declaration declares a structure with 3 members: a of type int, b of type char, and c of type double

// At the same time, the structure variable s1 is declared

// This structure has no tag specified

struct

{

   

int

a

;

   

char

b

;

   

double

c

;

}

s1

;

// This declaration declares a structure with 3 members: a of type int, b of type char, and c of type double

// The structure tag is named SIMPLE, with no variables declared

struct

SIMPLE

{

   

int

a

;

   

char

b

;

   

double

c

;

}

;

// Variables t1, t2, and t3 are additionally declared using the structure tagged SIMPLE

struct

SIMPLE t1, t2

[

20

]

,

t3

;

// You can also use typedef to create a new type

typedef

struct

{

   

int

a

;

   

char

b

;

   

double

c

;

}

Simple2

;

// Now you can use Simple2 as a type to declare new structure variables

Simple2 u1, u2

[

20

]

,

u3

;

In the declarations above, the first and second declarations are treated by the compiler as two completely different types, even though their member lists are identical; assigning t3=&s1 would be illegal.

A structure member can contain other structures, and can also contain a pointer to its own structure type; such pointers are usually used to implement more advanced data structures such as linked lists and trees.

// This structure declaration contains other structures

struct

COMPLEX

{

   

char

string

[

100

]

;

   

struct

SIMPLE a

;

}

;

// This structure declaration contains a pointer to its own type

struct

NODE

{

   

char

string

[

100

]

;

   

struct

NODE

next_node

;

}

;

If two structures contain each other, you need to make an incomplete declaration of one of them, as shown below:

struct

B

;

   

// Incomplete declaration of structure B

// Structure A contains a pointer to structure B

struct

A

{

   

struct

B

partner

;

   

//other members;

}

;

// Structure B contains a pointer to structure A; after A is declared, B is declared as well

struct

B

{

   

struct

A

partner

;

   

//other members;

}

;


Initialization of structure variables

Like variables of other types, structure variables can be given initial values at definition.

Example

#include

struct

Books

{

   

char

 title

[

50

]

;

   

char

 author

[

50

]

;

   

char

 subject

[

100

]

;

   

int

  book_id

;

}

book

=

{

"C Language"

,

"RUNOOB"

,

"Programming Language"

,

123456

}

;

int

main

(

)

{

   

printf

(

"title : %s\nauthor: %s\nsubject: %s\nbook_id: %d\n"

, book.

title

, book.

author

, book.

subject

, book.

book_id

)

;

}

The execution output is:

title : C 语言

author: RUNOOB

subject: 编程语言

book_id: 123456

Accessing Structure Members

To access the members of a structure, we use themember access operator (.). The member access operator is a period between the structure variable name and the structure member we want to access. You can use the struct keyword to define a variable of a structure type. The following example demonstrates the use of structures:

Example

#include

#include

 

struct

Books

{

   

char

 title

[

50

]

;

   

char

 author

[

50

]

;

   

char

 subject

[

100

]

;

   

int

  book_id

;

}

;

 

int

main

(

)

{

   

struct

Books Book1

;

       

/* Declare Book1 of type Books */

   

struct

Books Book2

;

       

/* Declare Book2 of type Books */

 

   

/* Book1 details */

   

strcpy

(

Book1.

title

,

"C Programming"

)

;

   

strcpy

(

Book1.

author

,

"Nuha Ali"

)

;

   

strcpy

(

Book1.

subject

,

"C Programming Tutorial"

)

;

   Book1.

book_id

=

6495407

;

   

/* Book2 details */

   

strcpy

(

Book2.

title

,

"Telecom Billing"

)

;

   

strcpy

(

Book2.

author

,

"Zara Ali"

)

;

   

strcpy

(

Book2.

subject

,

"Telecom Billing Tutorial"

)

;

   Book2.

book_id

=

6495700

;

 

   

/* Print Book1 information */

   

printf

(

"Book 1 title : %s\n"

, Book1.

title

)

;

   

printf

(

"Book 1 author : %s\n"

, Book1.

author

)

;

   

printf

(

"Book 1 subject : %s\n"

, Book1.

subject

)

;

   

printf

(

"Book 1 book_id : %d\n"

, Book1.

book_id

)

;

   

/* Print Book2 information */

   

printf

(

"Book 2 title : %s\n"

, Book2.

title

)

;

   

printf

(

"Book 2 author : %s\n"

, Book2.

author

)

;

   

printf

(

"Book 2 subject : %s\n"

, Book2.

subject

)

;

   

printf

(

"Book 2 book_id : %d\n"

, Book2.

book_id

)

;

   

return

0

;

}

When the code above is compiled and executed, it produces the following result:



Book 1 title : C Programming

Book 1 author : Nuha Ali

Book 1 subject : C Programming Tutorial

Book 1 book_id : 6495407

Book 2 title : Telecom Billing

Book 2 author : Zara Ali

Book 2 subject : Telecom Billing Tutorial

Book 2 book_id : 6495700

Structures as Function Parameters

You can use a structure as a function parameter; the way of passing parameters is similar to other types of variables or pointers. You can use the approach in the example above to access structure variables:

Example

#include

#include

 

struct

Books

{

   

char

 title

[

50

]

;

   

char

 author

[

50

]

;

   

char

 subject

[

100

]

;

   

int

  book_id

;

}

;

/* Function declaration */

void

printBook

(

struct

Books book

)

;

int

main

(

)

{

   

struct

Books Book1

;

       

/* Declare Book1 of type Books */

   

struct

Books Book2

;

       

/* Declare Book2 of type Books */

 

   

/* Book1 details */

   

strcpy

(

Book1.

title

,

"C Programming"

)

;

   

strcpy

(

Book1.

author

,

"Nuha Ali"

)

;

   

strcpy

(

Book1.

subject

,

"C Programming Tutorial"

)

;

   Book1.

book_id

=

6495407

;

   

/* Book2 details */

   

strcpy

(

Book2.

title

,

"Telecom Billing"

)

;

   

strcpy

(

Book2.

author

,

"Zara Ali"

)

;

   

strcpy

(

Book2.

subject

,

"Telecom Billing Tutorial"

)

;

   Book2.

book_id

=

6495700

;

 

   

/* Print Book1 information */

   printBook

(

Book1

)

;

   

/* Print Book2 information */

   printBook

(

Book2

)

;

   

return

0

;

}

void

printBook

(

struct

Books book

)

{

   

printf

(

"Book title : %s\n"

, book.

title

)

;

   

printf

(

"Book author : %s\n"

, book.

author

)

;

   

printf

(

"Book subject : %s\n"

, book.

subject

)

;

   

printf

(

"Book book_id : %d\n"

, book.

book_id

)

;

}

When the code above is compiled and executed, it produces the following result:



Book title : C Programming

Book author : Nuha Ali

Book subject : C Programming Tutorial

Book book_id : 6495407

Book title : Telecom Billing

Book author : Zara Ali

Book subject : Telecom Billing Tutorial

Book book_id : 6495700

Pointers to Structures

You can define a pointer to a structure in a similar way to defining a pointer to variables of other types, as shown below:



struct Books *struct_pointer;

Now, you can store the address of a structure variable in the pointer variable defined above. To find the address of a structure variable, place the & operator before the structure name, as shown below:



struct_pointer = &Book1;

To access the members of the structure using a pointer to it, you must use the -> operator, as shown below:



struct_pointer->title;

Let us rewrite the example above using structure pointers; this will help you understand the concept of structure pointers:

Example

#include

#include

 

struct

Books

{

   

char

 title

[

50

]

;

   

char

 author

[

50

]

;

   

char

 subject

[

100

]

;

   

int

  book_id

;

}

;

/* Function declaration */

void

printBook

(

struct

Books

book

)

;

int

main

(

)

{

   

struct

Books Book1

;

       

/* Declare Book1 of type Books */

   

struct

Books Book2

;

       

/* Declare Book2 of type Books */

 

   

/* Book1 details */

   

strcpy

(

Book1.

title

,

"C Programming"

)

;

   

strcpy

(

Book1.

author

,

"Nuha Ali"

)

;

   

strcpy

(

Book1.

subject

,

"C Programming Tutorial"

)

;

   Book1.

book_id

=

6495407

;

   

/* Book2 details */

   

strcpy

(

Book2.

title

,

"Telecom Billing"

)

;

   

strcpy

(

Book2.

author

,

"Zara Ali"

)

;

   

strcpy

(

Book2.

subject

,

"Telecom Billing Tutorial"

)

;

   Book2.

book_id

=

6495700

;

 

   

/* Print Book1 information by passing the address of Book1 */

   printBook

(

&

Book1

)

;

   

/* Print Book2 information by passing the address of Book2 */

   printBook

(

&

Book2

)

;

   

return

0

;

}

void

printBook

(

struct

Books

book

)

{

   

printf

(

"Book title : %s\n"

, book

title

)

;

   

printf

(

"Book author : %s\n"

, book

author

)

;

   

printf

(

"Book subject : %s\n"

, book

subject

)

;

   

printf

(

"Book book_id : %d\n"

, book

book_id

)

;

}

When the code above is compiled and executed, it produces the following result:



Book title : C Programming

Book author : Nuha Ali

Book subject : C Programming Tutorial

Book book_id : 6495407

Book title : Telecom Billing

Book author : Zara Ali

Book subject : Telecom Billing Tutorial

Book book_id : 6495700

Calculating the size of a structure

In C, we can use the sizeof operator to calculate the size of a structure;sizeof it returns the size in bytes of a given type or variable.

For a structure,sizeof returns the total number of bytes of the structure, including the sizes of all member variables and any possible padding bytes.

The following example demonstrates how to calculate the size of a structure:

Example

#include

struct

Person

{

   

char

name

[

20

]

;

   

int

age

;

   

float

height

;

}

;

int

main

(

)

{

   

struct

Person person

;

   

printf

(

"Size of structure Person: %zu bytes\n"

,

sizeof

(

person

)

)

;

   

return

0

;

}

In the example above, we defined a structure named Person which contains a character array name, an integer age and a floating-point number height.

in main In the function, we declared a Person variable of type person, then use sizeof operator to get the person size of the structure.

Finally, we use printf function prints the size of the structure, and the output is as follows:

结构体 Person 大小为: 28 字节

Note, the size of a structure may be affected by the compiler's optimizations and alignment rules; the compiler may insert extra padding bytes into the structure to align its member variables for more efficient memory access. Therefore, the actual size of a structure may be greater than the sum of the sizes of its member variables. If you need to know exactly the memory layout and alignment of the structure, you can use the offsetof macro and the attribute((packed)) attribute and others to further control and query the size and alignment of the structure.