C Functions

C structures, c structures (structs).

Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure.

Unlike an array , a structure can contain many different data types (int, float, char, etc.).

Create a Structure

You can create a structure by using the struct keyword and declare each of its members inside curly braces:

To access the structure, you must create a variable of it.

Use the struct keyword inside the main() method, followed by the name of the structure and then the name of the structure variable:

Create a struct variable with the name "s1":

Access Structure Members

To access members of a structure, use the dot syntax ( . ):

Now you can easily create multiple structure variables with different values, using just one structure:

What About Strings in Structures?

Remember that strings in C are actually an array of characters, and unfortunately, you can't assign a value to an array like this:

An error will occur:

However, there is a solution for this! You can use the strcpy() function and assign the value to s1.myString , like this:

Simpler Syntax

You can also assign values to members of a structure variable at declaration time, in a single line.

Just insert the values in a comma-separated list inside curly braces {} . Note that you don't have to use the strcpy() function for string values with this technique:

Note: The order of the inserted values must match the order of the variable types declared in the structure (13 for int, 'B' for char, etc).

Copy Structures

You can also assign one structure to another.

In the following example, the values of s1 are copied to s2:

Modify Values

If you want to change/modify a value, you can use the dot syntax ( . ).

And to modify a string value, the strcpy() function is useful again:

Modifying values are especially useful when you copy structure values:

Ok, so, how are structures useful?

Imagine you have to write a program to store different information about Cars, such as brand, model, and year. What's great about structures is that you can create a single "Car template" and use it for every cars you make. See below for a real life example.

Real-Life Example

Use a structure to store different information about Cars:

C Exercises

Test yourself with exercises.

Fill in the missing part to create a Car structure:

Start the Exercise

Get Certified

COLOR PICKER

colorpicker

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

[email protected]

Top Tutorials

Top references, top examples, get certified.

Structured data types in C - Struct and Typedef Explained with Examples

During your programming experience you may feel the need to define your own type of data. In C this is done using two keywords: struct and typedef . Structures and unions will give you the chance to store non-homogenous data types into a single collection.

Declaring a new data type

After this little code student will be a new reserved keyword and you will be able to create variables of type student . Please mind that this new kind of variable is going to be structured which means that defines a physically grouped list of variables to be placed under one name in a block of memory.

New data type usage

Let’s now create a new student variable and initialize its attributes:

As you can see in this example you are required to assign a value to all variables contained in your new data type. To access a structure variable you can use the point like in stu.name . There is also a shorter way to assign values to a structure:

Or if you prefer to set it’s values following a different order:

Unions vs Structures

Unions are declared in the same was as structs, but are different because only one item within the union can be used at any time.

You should use union in such case where only one condition will be applied and only one variable will be used. Please do not forget that we can use our brand new data type too:

A few more tricks

  • When you create a pointer to a structure using the & operator you can use the special -> infix operator to deference it. This is very used for example when working with linked lists in C
  • The new defined type can be used just as other basic types for almost everything. Try for example to create an array of type student and see how it works.
  • Structs can be copied or assigned but you can not compare them!

More Information:

  • The C Beginner's Handbook: Learn C Programming Language basics in just a few hours
  • Data Types in C - Integer, Floating Point, and Void Explained
  • malloc in C: Dynamic Memory Allocation in C Explained

If this article was helpful, share it .

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

Next: Unions , Previous: Overlaying Structures , Up: Structures   [ Contents ][ Index ]

15.13 Structure Assignment

Assignment operating on a structure type copies the structure. The left and right operands must have the same type. Here is an example:

Notionally, assignment on a structure type works by copying each of the fields. Thus, if any of the fields has the const qualifier, that structure type does not allow assignment:

See Assignment Expressions .

When a structure type has a field which is an array, as here,

structure assigment such as r1 = r2 copies array fields’ contents just as it copies all the other fields.

This is the only way in C that you can operate on the whole contents of a array with one operation: when the array is contained in a struct . You can’t copy the contents of the data field as an array, because

would convert the array objects (as always) to pointers to the zeroth elements of the arrays (of type struct record * ), and the assignment would be invalid because the left operand is not an lvalue.

Intro to C for CS31 Students

Part 2: structs & pointers.

  • Pointers and Functions C style "pass by referece"
  • Dynamic Memory Allocation (malloc and free)
  • Pointers to Structs

Defining a struct type

Declaring variables of struct types, accessing field values, passing structs to functions, rules for using pointer variables.

  • Next, initialize the pointer variable (make it point to something). Pointer variables store addresses . Initialize a pointer to the address of a storage location of the type to which it points. One way to do this is to use the ampersand operator on regular variable to get its address value: int x; char ch; ptr = &x; // ptr get the address of x, pointer "points to" x ------------ ------ ptr | addr of x|--------->| ?? | x ------------ ------ cptr = &ch; // ptr get the address of ch, pointer "points to" ch cptr = &x; // ERROR! cptr can hold a char address only (it's NOT a pointer to an int) All pointer variable can be set to a special value NULL . NULL is not a valid address but it is useful for testing a pointer variable to see if it points to a valid memory address before we access what it points to: ptr = NULL; ------ ------ cptr = NULL; ptr | NULL |-----| cptr | NULL |----| ------ ------
  • Use *var_name to dereference the pointer to access the value in the location that it points to. Some examples: int *ptr1, *ptr2, x, y; x = 8; ptr1 = NULL; ptr2 = &x; ------------ ------ ptr2 | addr of x|--------->| 8 | x ------------ ------ *ptr2 = 10; // dereference ptr2: "what ptr2 points to gets 10" ------------ ----- ptr2 | addr of x|--------->| 10 | x ------------ ----- y = *ptr2 + 3; // dereference ptr2: "y gets what ptr2 points to plus 3" ----- ----- ptr1 = ptr2; // ptr1 gets address value stored in ptr2 ------------ ----- ptr2 | addr of x |--------->| 10 | x ------------ ----- /\ ------------ | ptr1 | addr of x |-------------- ------------ // TODO: finish tracing through this code and show what is printed *ptr1 = 100; ptr1 = &y; *ptr1 = 80; printf("x = %d y = %d\n", x, y); printf("x = %d y = %d\n", *ptr2, *ptr1);
  • and what does this mean with respect to the value of each argument after the call?
  • Implement a program with a swap function that swaps the values stored in its two arguments. Make some calls to it in main to test it out.

malloc and free

Pointers, the heap, and functions, linked lists in c.

  • How to Initialize a Struct in C

Use Designated Initializers to Initialize a Struct in C

Use compound literals to initialize a struct in c, use explicit assignments to initialize a struct in c, use constructors (in c99 and later) to initialize a struct in c.

How to Initialize a Struct in C

Structs in C provide a powerful mechanism for grouping variables of diverse data types under a unified name. Properly initializing a struct is crucial for ensuring the correct functioning of your programs.

In this article, we’ll explore various methods to initialize a struct in C, each offering its advantages.

In C programming, structs serve as a pivotal tool for grouping variables of diverse data types under a unified name. When it comes to initializing a struct, one particularly versatile technique is the use of designated initializers.

This method allows for explicit specification of which members of the struct to initialize, offering clarity and flexibility in code organization.

The syntax for designated initializers involves specifying the member name followed by an equal sign and the value to assign. The general form looks like this:

This syntax allows initializing specific members of the struct while leaving others uninitialized, fostering flexibility in struct initialization.

Let’s dive into an example featuring a Person struct, representing an individual’s first name, last name, age, and a boolean indicating their vitality. Designated initializers will be employed to create an instance of this struct.

In this code snippet, we include necessary header files ( stdbool.h , stdio.h , stdlib.h , string.h ) and define a Person struct using typedef . The struct encapsulates members for the first name ( firstname ), last name ( lastname ), age, and a boolean indicating whether the person is alive ( alive ).

In the main function, we create an instance of the Person struct named me . Using designated initializers, we initialize each member of the struct with specific values.

The first name is set to John , the last name to McCarthy , the age to 24 , and the alive status to true .

The subsequent printf statement displays the information stored in the me struct. It prints the first name, last name, and age using the %s and %d format specifiers, respectively.

The last part of the output depends on the boolean value of the alive member. If alive is true , Yes is printed; otherwise, No is printed.

Finally, the program exits with EXIT_SUCCESS , indicating successful execution.

Another versatile technique to initialize a struct is using compound literals. This method allows for the creation of temporary instances of a struct with specific initial values.

The syntax for compound literals involves enclosing a list of values within parentheses and casting them to the desired struct type. The general form looks like this:

This syntax allows for the creation of an unnamed temporary instance of the struct with the specified initial values.

Let’s delve into a practical example using the Person struct:

In this example, we have a Person struct representing an individual’s first name, last name, age, and alive status. The main function initializes an instance of this struct named me using a compound literal.

The compound literal (Person){.firstname = "John\0", .lastname = "McCarthy\0", .age = 24, .alive = 1} creates a temporary instance of the Person struct with each member explicitly initialized.

This method combines clarity and conciseness, eliminating the need for a separate declaration and assignment step.

The subsequent printf statement displays the information stored in the me struct, presenting the first name, last name, age, and alive status. The output, in this case, would be:

Using compound literals in this manner enhances the readability of the code, making it clear and expressive. This approach is particularly advantageous when immediate struct initialization is preferred, providing a streamlined and elegant solution for struct initialization in C.

When initializing struct members in C, an alternative method involves declaring a variable and assigning each member with its corresponding value individually. This approach is characterized by explicit assignments, making it clear and straightforward.

It’s worth noting that when dealing with char arrays, assigning them directly with strings isn’t allowed in C. Instead, explicit copying using functions like memcpy or memmove is necessary (refer to the manual for more details).

Additionally, care should be taken to ensure that the length of the array is not less than the string being stored.

Here’s an illustrative example using a Person struct:

As we can see in this example, each member of the me2 instance is assigned a value individually. Here, the use of memcpy ensures the proper copying of string values into char arrays, respecting the necessary length constraints.

The subsequent printf statement remains unchanged, displaying the information stored in the me2 struct. The output, as before, will be:

While this approach is valid, it often involves more lines of code and can be less concise compared to using compound literals, as demonstrated in the previous section.

Compound literals provide a more streamlined and expressive way to initialize structs in C, especially when dealing with immediate initialization or temporary instances.

The concept of constructors, while not native, like in some other languages, can be emulated to achieve struct initialization.

C99 and later versions allow for a more expressive and organized approach by enabling the use of designated initializers within functions. This technique can be likened to a constructor, allowing for structured and customizable initialization of structs.

The syntax for creating a constructor-like function involves defining a function that returns an instance of the struct with designated initializers.

Here’s an example using a Person struct:

In this example, we define a Person struct with members for the first name, last name, age, and alive status. Here, the createPerson function serves as a constructor-like function, initializing and configuring a Person struct with specific values.

The function uses strncpy to copy the first and last names into the struct’s firstname and lastname members, respectively. It also ensures null-termination of the strings to prevent potential buffer overflows.

The age and alive members are then set according to the provided arguments.

In the main function, the createPerson function is used to initialize a Person struct named me . The subsequent printf statement displays the information stored in the me struct, showcasing the first name, last name, age, and alive status.

This constructor-like approach offers a structured and modular way to initialize structs in C, providing a clear and organized solution for customizable struct initialization.

Initializing structs in C involves a range of techniques, each catering to different preferences and scenarios.

Designated initializers offer clarity and flexibility, whereas compound literals provide immediate initialization. Explicit assignments ensure straightforward initialization and constructors (in C99 and later) allow for a structured approach.

Choose the method that aligns with your coding style and the specific requirements of your program. Whether you prioritize clarity, conciseness, or modularity, understanding these initialization techniques will empower you to write more efficient and maintainable C code.

Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

Related Article - C Struct

  • Bit Field in C
  • Difference Between Struct and Typedef Struct in C
  • How to Use struct Alignment and Padding in C
  • How to Initialize Array of Structs in C
  • How to Return a Struct From Function in C
  • How to Allocate Struct Memory With malloc in C

C Programming Tutorial

  • Pointer to a Structure in C

Last updated on July 27, 2020

We have already learned that a pointer is a variable which points to the address of another variable of any data type like int , char , float etc. Similarly, we can have a pointer to structures, where a pointer variable can point to the address of a structure variable. Here is how we can declare a pointer to a structure variable.

This declares a pointer ptr_dog that can store the address of the variable of type struct dog . We can now assign the address of variable spike to ptr_dog using & operator.

Now ptr_dog points to the structure variable spike .

Accessing members using Pointer #

There are two ways of accessing members of structure using pointer:

  • Using indirection ( * ) operator and dot ( . ) operator.
  • Using arrow ( -> ) operator or membership operator.

Let's start with the first one.

Using Indirection (*) Operator and Dot (.) Operator #

At this point ptr_dog points to the structure variable spike , so by dereferencing it we will get the contents of the spike . This means spike and *ptr_dog are functionally equivalent. To access a member of structure write *ptr_dog followed by a dot( . ) operator, followed by the name of the member. For example:

(*ptr_dog).name - refers to the name of dog (*ptr_dog).breed - refers to the breed of dog

Parentheses around *ptr_dog are necessary because the precedence of dot( . ) operator is greater than that of indirection ( * ) operator.

Using arrow operator (->) #

The above method of accessing members of the structure using pointers is slightly confusing and less readable, that's why C provides another way to access members using the arrow ( -> ) operator. To access members using arrow ( -> ) operator write pointer variable followed by -> operator, followed by name of the member.

Here we don't need parentheses, asterisk ( * ) and dot ( . ) operator. This method is much more readable and intuitive.

We can also modify the value of members using pointer notation.

Here we know that the name of the array ( ptr_dog->name ) is a constant pointer and points to the 0th element of the array. So we can't assign a new string to it using assignment operator ( = ), that's why strcpy() function is used.

In the above expression precedence of arrow operator ( -> ) is greater than that of prefix decrement operator ( -- ), so first -> operator is applied in the expression then its value is decremented by 1.

The following program demonstrates how we can use a pointer to structure.

Expected Output:

How it works:

In lines 3-9, we have declared a structure of type dog which has four members namely name , breed , age and color .

In line 13, a variable called my_dog of type struct dog is declared and initialized.

In line 14, a pointer variable ptr_dog of type struct dog is declared.

In line 15, the address of my_dog is assigned to ptr_dog using & operator.

In lines 17-20, the printf() statements prints the details of the dog.

In line 23, a new name is assigned to ptr_dog using the strcpy() function, because we can't assign a string value directly to ptr_dog->name using assignment operator.

In line 26, the value of ptr_dog->age is incremented by 1 using postfix increment operator. Recall that postfix ++ operator and -> have the same precedence and associates from left to right. But since postfix ++ is used in the expression first the value of ptr_dog->age is used in the expression then it's value is incremented by 1 .

Load Comments

  • Intro to C Programming
  • Installing Code Blocks
  • Creating and Running The First C Program
  • Basic Elements of a C Program
  • Keywords and Identifiers
  • Data Types in C
  • Constants in C
  • Variables in C
  • Input and Output in C
  • Formatted Input and Output in C
  • Arithmetic Operators in C
  • Operator Precedence and Associativity in C
  • Assignment Operator in C
  • Increment and Decrement Operators in C
  • Relational Operators in C
  • Logical Operators in C
  • Conditional Operator, Comma operator and sizeof() operator in C
  • Implicit Type Conversion in C
  • Explicit Type Conversion in C
  • if-else statements in C
  • The while loop in C
  • The do while loop in C
  • The for loop in C
  • The Infinite Loop in C
  • The break and continue statement in C
  • The Switch statement in C
  • Function basics in C
  • The return statement in C
  • Actual and Formal arguments in C
  • Local, Global and Static variables in C
  • Recursive Function in C
  • One dimensional Array in C
  • One Dimensional Array and Function in C
  • Two Dimensional Array in C
  • Pointer Basics in C
  • Pointer Arithmetic in C
  • Pointers and 1-D arrays
  • Pointers and 2-D arrays
  • Call by Value and Call by Reference in C
  • Returning more than one value from function in C
  • Returning a Pointer from a Function in C
  • Passing 1-D Array to a Function in C
  • Passing 2-D Array to a Function in C
  • Array of Pointers in C
  • Void Pointers in C
  • The malloc() Function in C
  • The calloc() Function in C
  • The realloc() Function in C
  • String Basics in C
  • The strlen() Function in C
  • The strcmp() Function in C
  • The strcpy() Function in C
  • The strcat() Function in C
  • Character Array and Character Pointer in C
  • Array of Strings in C
  • Array of Pointers to Strings in C
  • The sprintf() Function in C
  • The sscanf() Function in C
  • Structure Basics in C
  • Array of Structures in C
  • Array as Member of Structure in C
  • Nested Structures in C
  • Pointers as Structure Member in C
  • Structures and Functions in C
  • Union Basics in C
  • typedef statement in C
  • Basics of File Handling in C
  • fputc() Function in C
  • fgetc() Function in C
  • fputs() Function in C
  • fgets() Function in C
  • fprintf() Function in C
  • fscanf() Function in C
  • fwrite() Function in C
  • fread() Function in C

Recent Posts

  • Machine Learning Experts You Should Be Following Online
  • 4 Ways to Prepare for the AP Computer Science A Exam
  • Finance Assignment Online Help for the Busy and Tired Students: Get Help from Experts
  • Top 9 Machine Learning Algorithms for Data Scientists
  • Data Science Learning Path or Steps to become a data scientist Final
  • Enable Edit Button in Shutter In Linux Mint 19 and Ubuntu 18.04
  • Python 3 time module
  • Pygments Tutorial
  • How to use Virtualenv?
  • Installing MySQL (Windows, Linux and Mac)
  • What is if __name__ == '__main__' in Python ?
  • Installing GoAccess (A Real-time web log analyzer)
  • Installing Isso

Learn C practically and Get Certified .

Popular Tutorials

Popular examples, reference materials, learn c interactively, c introduction.

  • Keywords & Identifier
  • Variables & Constants
  • C Data Types
  • C Input/Output
  • C Operators
  • C Introduction Examples

C Flow Control

  • C if...else
  • C while Loop
  • C break and continue
  • C switch...case
  • C Programming goto
  • Control Flow Examples

C Functions

  • C Programming Functions
  • C User-defined Functions
  • C Function Types
  • C Recursion
  • C Storage Class
  • C Function Examples
  • C Programming Arrays
  • C Multi-dimensional Arrays
  • C Arrays & Function
  • C Programming Pointers
  • C Pointers & Arrays
  • C Pointers And Functions
  • C Memory Allocation
  • Array & Pointer Examples

C Programming Strings

  • C Programming String
  • C String Functions
  • C String Examples

Structure And Union

  • C Structure
  • C Struct & Pointers
  • C Struct & Function
  • C struct Examples

C Programming Files

  • C Files Input/Output
  • C Files Examples

Additional Topics

  • C Enumeration
  • C Preprocessors
  • C Standard Library
  • C Programming Examples
  • Store Data in Structures Dynamically

C Structure and Function

C Dynamic Memory Allocation

C Struct Examples

  • Store Information of a Student Using Structure

C structs and Pointers

Before you learn about how pointers can be used with structs, be sure to check these tutorials:

  • C Pointers to struct

Here's how you can create pointers to structs.

Here, ptr is a pointer to struct .

Example: Access members using Pointer

To access members of a structure using pointers, we use the -> operator.

In this example, the address of person1 is stored in the personPtr pointer using personPtr = &person1; .

Now, you can access the members of person1 using the personPtr pointer.

By the way,

  • personPtr->age is equivalent to (*personPtr).age
  • personPtr->weight is equivalent to (*personPtr).weight
  • Dynamic memory allocation of structs

Before you proceed this section, we recommend you to check C dynamic memory allocation .

Sometimes, the number of struct variables you declared may be insufficient. You may need to allocate memory during run-time. Here's how you can achieve this in C programming.

Example: Dynamic memory allocation of structs

When you run the program, the output will be:

In the above example, n number of struct variables are created where n is entered by the user.

To allocate the memory for n number of struct person , we used,

Then, we used the ptr pointer to access elements of person .

Table of Contents

  • Example: Access struct members using pointers

Sorry about that.

Related Tutorials

Search anything:

Structure (struct) in C [Complete Guide]

Software engineering c programming c++.

Internship at OpenGenus

Get this book -> Problems on Array: For Interviews and Competitive Programming

A structure is defined as a collection of same/different data types. All data items thus grouped logically related and can be accessed using variables.

Table of contents:

Basics of Structure in C

  • Structure Declaration 2.1. Tagged Structure 2.2. Structure Variables 2.3. Type Defined Structures

Structure Initialization in C

Accessing structures in c, array of structure in c, nested structures in c.

Let us get started with Structure (struct) in C.

" Struct " keyword is used to identify the structure.

Memory Allocation

12-1

Always, contiguous(adjacent) memory locations are used to store structure members in memory.Consider above example to understand how memory is allocated for structures.

There are 5 members declared for structure in above program. In 32 bit compiler, 4 bytes of memory is occupied by int datatype. 1 byte each of memory is occupied by char datatype.

Memory is reserved only if the above definition is associated with variables.That is once the structure are defined,they have to be declared .Then only 56 bytes of memory space is reserved.

Structure Declaration

Structure can be declare using three different ways:

1: Tagged Structure 2: Structure Variables 3: Type Defined Structures

1: Tagged Structure

The structure name with tag name is called tagged structure.The tag name is the name of the structure.

  • Here studentDetails is the tag name.
  • To allocate the memory for the structure, we have to declare the variable as shown below

struct studentDetails Jack,Jonas;

Once the structure variables are declared, the compiler allocates memory for the structure, So 32 bytes are reserved for the variable Jonas and another 32 bytes are reserved for the variable Jack.The size of the memory allocated is the sum of size of individiual members

2: Structure Variables

It is possible to declare variables of a structure, either along with structure definition or after the structure is defined.

Structure variable declaration is similar to the declaration of any normal variable of any other data type.

6

Observe that 40 bytes of memory is allocated for the variable S1 and another 22 bytes of memory is allocated for the variable S2.

Note: We avoid this way of defining and declaring the structure variablesm because of the following reasons:

  • Without a tag,it is not possible to declare variables in later stages inside the functions.
  • It is not possible to use them as parameter in the function,because all the parameter have to be declared.
  • We can define them only in the beginning of the program.In such situations,they are treated as global variables.In the structured programming it is better to avoid the usage of global variables.

3: Typed-Defined Structure

The Structure definition associated with keyword typedef is called type-defined structure.This is the most powerful way of defining the structure

It can be initialized in various ways

Method 1 : Specify the initializers within the braces and seperated by commas when the variables are declared as shown below:

Method 2 : Specify the initializers within the braces and seperated by commas when the variables are declared as shown below:

We can access structure in two ways:

  • By . (memeber or dot operator)
  • By -> ( structure pointer operator)

The array of structures in C are used to store information about various member of different data types.It is also known as collection of structure.

Nested structure means structure within structure. As we have declared members inside the structure in the same we can declare declare structure.

With this article at OpenGenus, you must have a complete idea of structure (Struct) in C / C++ Programming Language.

OpenGenus IQ: Computing Expertise & Legacy icon

  • Overview of C
  • Features of C
  • Install C Compiler/IDE
  • My First C program
  • Compile and Run C program
  • Understand Compilation Process
  • C Syntax Rules
  • Keywords and Identifier
  • Understanding Datatypes
  • Using Datatypes (Examples)
  • What are Variables?
  • What are Literals?
  • Constant value Variables - const
  • C Input / Output
  • Operators in C Language
  • Decision Making
  • Switch Statement
  • String and Character array
  • Storage classes

C Functions

  • Introduction to Functions
  • Types of Functions and Recursion
  • Types of Function calls
  • Passing Array to function

C Structures

  • All about Structures
  • Pointers concept
  • Declaring and initializing pointer
  • Pointer to Pointer
  • Pointer to Array
  • Pointer to Structure
  • Pointer Arithmetic
  • Pointer with Functions

C File/Error

  • File Input / Output
  • Error Handling
  • Dynamic memory allocation
  • Command line argument
  • 100+ C Programs with explanation and output

Structure is a user-defined datatype in C language which allows us to combine data of different types together. Structure helps to construct a complex data type which is more meaningful. It is somewhat similar to an Array , but an array holds data of similar type only. But structure on the other hand, can store data of any type, which is practical more useful.

For example: If I have to write a program to store Student information, which will have Student's name, age, branch, permanent address, father's name etc, which included string values, integer values etc, how can I use arrays for this problem, I will require something which can hold data of different types together.

In structure, data is stored in form of records .

Defining a structure

struct keyword is used to define a structure. struct defines a new data type which is a collection of primary and derived data types .

As you can see in the syntax above, we start with the struct keyword, then it's optional to provide your structure a name, we suggest you to give it a name, then inside the curly braces, we have to mention all the member variables, which are nothing but normal C language variables of different types like int , float , array etc.

After the closing curly brace, we can specify one or more structure variables, again this is optional.

Note: The closing curly brace in the structure type declaration must be followed by a semicolon( ; ).

Example of Structure

Here struct Student declares a structure to hold the details of a student which consists of 4 data fields, namely name , age , branch and gender . These fields are called structure elements or members .

Each member can have different datatype, like in this case, name is an array of char type and age is of int type etc. Student is the name of the structure and is called as the structure tag .

Declaring Structure Variables

It is possible to declare variables of a structure , either along with structure definition or after the structure is defined. Structure variable declaration is similar to the declaration of any normal variable of any other datatype. Structure variables can be declared in following two ways:

1) Declaring Structure variables separately

2) declaring structure variables with structure definition.

Here S1 and S2 are variables of structure Student . However this approach is not much recommended.

Accessing Structure Members

Structure members can be accessed and assigned values in a number of ways. Structure members have no meaning individually without the structure. In order to assign a value to any structure member, the member name must be linked with the structure variable using a dot . operator also called period or member access operator.

For example:

Name of Student 1: Viraaj Age of Student 1: 18

We can also use scanf() to give values to structure members through terminal.

Structure Initialization

Like a variable of any other datatype, structure variable can also be initialized at compile time.

Array of Structure

We can also declare an array of structure variables. in which each element of the array will represent a structure variable. Example : struct employee emp[5];

The below program defines an array emp of size 5. Each element of the array emp is of type Employee .

Nested Structures

Nesting of structures, is also permitted in C language. Nested structures means, that one structure has another stucture as member variable .

Structure as Function Arguments

We can pass a structure as a function argument just like we pass any other variable or an array as a function argument.

  • ← Prev
  • Next →

A man is valued by his works, not his words!

Structure assignment and its pitfall in c language.

Jan 28 th , 2013 9:47 pm

There is a structure type defined as below:

If we want to assign map_t type variable struct2 to sturct1 , we usually have below 3 ways:

Consider above ways, most of programmer won’t use way #1, since it’s so stupid ways compare to other twos, only if we are defining an structure assignment function. So, what’s the difference between way #2 and way #3? And what’s the pitfall of the structure assignment once there is array or pointer member existed? Coming sections maybe helpful for your understanding.

The difference between ‘=’ straight assignment and memcpy

The struct1=struct2; notation is not only more concise , but also shorter and leaves more optimization opportunities to the compiler . The semantic meaning of = is an assignment, while memcpy just copies memory. That’s a huge difference in readability as well, although memcpy does the same in this case.

Copying by straight assignment is probably best, since it’s shorter, easier to read, and has a higher level of abstraction. Instead of saying (to the human reader of the code) “copy these bits from here to there”, and requiring the reader to think about the size argument to the copy, you’re just doing a straight assignment (“copy this value from here to here”). There can be no hesitation about whether or not the size is correct.

Consider that, above source code also has pitfall about the pointer alias, it will lead dangling pointer problem ( It will be introduced below section ). If we use straight structure assignment ‘=’ in C++, we can consider to overload the operator= function , that can dissolve the problem, and the structure assignment usage does not need to do any changes, but structure memcpy does not have such opportunity.

The pitfall of structure assignment:

Beware though, that copying structs that contain pointers to heap-allocated memory can be a bit dangerous, since by doing so you’re aliasing the pointer, and typically making it ambiguous who owns the pointer after the copying operation.

If the structures are of compatible types, yes, you can, with something like:

} The only thing you need to be aware of is that this is a shallow copy. In other words, if you have a char * pointing to a specific string, both structures will point to the same string.

And changing the contents of one of those string fields (the data that the char points to, not the char itself) will change the other as well. For these situations a “deep copy” is really the only choice, and that needs to go in a function. If you want a easy copy without having to manually do each field but with the added bonus of non-shallow string copies, use strdup:

This will copy the entire contents of the structure, then deep-copy the string, effectively giving a separate string to each structure. And, if your C implementation doesn’t have a strdup (it’s not part of the ISO standard), you have to allocate new memory for dest_struct pointer member, and copy the data to memory address.

Example of trap:

Below diagram illustrates above source memory layout, if there is a pointer field member, either the straight assignment or memcpy , that will be alias of pointer to point same address. For example, b.alias and c.alias both points to address of a.alias . Once one of them free the pointed address, it will cause another pointer as dangling pointer. It’s dangerous!!

  • Recommend use straight assignment ‘=’ instead of memcpy.
  • If structure has pointer or array member, please consider the pointer alias problem, it will lead dangling pointer once incorrect use. Better way is implement structure assignment function in C, and overload the operator= function in C++.
  • stackoverflow.com: structure assignment or memcpy
  • stackoverflow.com: assign one struct to another in C
  • bytes.com: structures assignment
  • wikipedia: struct in C programming language
  • 90% Refund @Courses
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Preprocessors
  • C File Handling
  • C Cheatsheet
  • C Interview Questions

Related Articles

  • Solve Coding Problems
  • Anonymous Union and Structure in C
  • How to Pass or Return a Structure To or From a Function in C?
  • How to avoid Structure Padding in C?
  • Flexible Array Members in a structure in C
  • C Program to Add N Distances Given in inch-feet System using Structures
  • Difference between Structure and Array in C
  • C Structures
  • Difference Between Structure and Union in C
  • Local Labels in C
  • Error Handling in C
  • C | Structure & Union | Question 10
  • lvalue and rvalue in C language
  • Alternative of Malloc in C
  • Top 50 C Coding Interview Questions and Answers (2024)
  • Implicit Type Conversion in C with Examples
  • Security issues in C language
  • pthread_self() in C with Example
  • Dynamically Growing Array in C
  • How To Run CUDA C/C++ on Jupyter notebook in Google Colaboratory

Nested Structure in C with Examples

Pre-requisite: Structures in C

A nested structure in C is a structure within structure. One structure can be declared inside another structure in the same way structure members are declared inside a structure.

struct name_1 {     member1;    member2;    .    .    membern;    struct name_2    {        member_1;        member_2;        .        .       member_n;    }, var1 } var2;

The member of a nested structure can be accessed using the following syntax:

Variable name of Outer_Structure.Variable name of Nested_Structure.data member to access 
  • Consider there are two structures Employee (depended structure) and another structure called Organisation(Outer structure) .
  • The structure Organisation has the data members like organisation_name,organisation_number.
  • The Employee structure is nested inside the structure Organisation and it has the data members like employee_id, name, salary.

For accessing the members of Organisation and Employee following syntax will be used:

org.emp.employee_id; org.emp.name; org.emp.salary; org.organisation_name; org.organisation_number; Here, org is the structure variable of the outer structure Organisation and emp is the structure variable of the inner structure Employee.

Different ways of nesting structure

The structure can be nested in the following different ways:

  • By separate nested structure
  • By embedded nested structure .

1. By separate nested structure: In this method, the two structures are created, but the dependent structure(Employee) should be used inside the main structure(Organisation) as a member. Below is the C program to implement the approach:

The size of structure organisation : 68 Organisation Name : GeeksforGeeks Organisation Number : GFG123768 Employee id : 101 Employee name : Robert Employee Salary : 400000

2. By Embedded nested structure: Using this method, allows to declare structure inside a structure and it requires fewer lines of code. 

Case 1: Error will occur if the structure is present but the structure variable is missing.

Error

Whenever an embedded nested structure is created, the variable declaration is compulsory at the end of the inner structure, which acts as a member of the outer structure. It is compulsory that the structure variable is created at the end of the inner structure. 

Case 2: When the structure variable of the inner structure is declared at the end of the inner structure. Below is the C program to implement this approach:

Drawback of Nested Structure

The drawback in nested structures are:

  • Independent existence not possible: It is important to note that structure Employee doesn’t exist on its own. One can’t declare structure variable of type struct Employee anywhere else in the program.
  • Cannot be used in multiple data structures: The nested structure cannot be used in multiple structures due to the limitation of declaring structure variables within the main structure. So, the most recommended way is to use a separate structure and it can be used in multiple data structures

C Nested Structure Example

Below is another example of a C nested structure.

College name : GeeksforGeeks Ranking : 7 Student id : 111 Student name : Paul Roll no : 278

Nesting of structure within itself is not allowed. 

struct student {     char name[50];     char address[100];     int roll_no;     struct student geek; // Invalid }

Passing nested structure to function

A nested structure can be passed into the function in two ways:

  • Pass the nested structure variable at once.
  • Pass the nested structure members as an argument into the function.

Let’s discuss each of these ways in detail.

1. Pass the nested structure variable at once: Just like other variables, a nested structure variable can also be passed to the function. Below is the C program to implement this concept:

Printing the Details : Organisation Name : GeeksforGeeks Organisation Number : GFG111 Employee id : 278 Employee name : Paul Employee Salary : 5000

2. Pass the nested structure members as arguments into the function: Consider the following example to pass the structure member of the employee to a function display() which is used to display the details of an employee.

Accessing Nested Structure

Nested Structure can be accessed in two ways:

  • Using Normal variable.
  • Using Pointer variable.

Let’s discuss each of these methods in detail.

1. Using Normal variable: Outer and inner structure variables are declared as normal variables and the data members of the outer structure are accessed using a single dot(.) and the data members of the inner structure are accessed using the two dots. Below is the C program to implement this concept:

College ID : 14567   College Name : GeeksforGeeks   Student ID : 12   Student Name : Kathy   Student CGPA : 7.800000 

2. Using Pointer variable: One normal variable and one pointer variable of the structure are declared to explain the difference between the two. In the case of the pointer variable, a combination of dot(.) and arrow(->) will be used to access the data members. Below is the C program to implement the above approach:

A nested structure will allow the creation of complex data types according to the requirements of the program.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now !

Please Login to comment...

author

  • C-Structure & Union
  • simmytarika5
  • surindertarika1234
  • sagar0719kumar

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

assigning value to structure in c

Create a form in Word that users can complete or print

In Word, you can create a form that others can fill out and save or print.  To do this, you will start with baseline content in a document, potentially via a form template.  Then you can add content controls for elements such as check boxes, text boxes, date pickers, and drop-down lists. Optionally, these content controls can be linked to database information.  Following are the recommended action steps in sequence.  

Show the Developer tab

In Word, be sure you have the Developer tab displayed in the ribbon.  (See how here:  Show the developer tab .)

Open a template or a blank document on which to base the form

You can start with a template or just start from scratch with a blank document.

Start with a form template

Go to File > New .

In the  Search for online templates  field, type  Forms or the kind of form you want. Then press Enter .

In the displayed results, right-click any item, then select  Create. 

Start with a blank document 

Select Blank document .

Add content to the form

Go to the  Developer  tab Controls section where you can choose controls to add to your document or form. Hover over any icon therein to see what control type it represents. The various control types are described below. You can set properties on a control once it has been inserted.

To delete a content control, right-click it, then select Remove content control  in the pop-up menu. 

Note:  You can print a form that was created via content controls. However, the boxes around the content controls will not print.

Insert a text control

The rich text content control enables users to format text (e.g., bold, italic) and type multiple paragraphs. To limit these capabilities, use the plain text content control . 

Click or tap where you want to insert the control.

Rich text control button

To learn about setting specific properties on these controls, see Set or change properties for content controls .

Insert a picture control

A picture control is most often used for templates, but you can also add a picture control to a form.

Picture control button

Insert a building block control

Use a building block control  when you want users to choose a specific block of text. These are helpful when you need to add different boilerplate text depending on the document's specific purpose. You can create rich text content controls for each version of the boilerplate text, and then use a building block control as the container for the rich text content controls.

building block gallery control

Select Developer and content controls for the building block.

Developer tab showing content controls

Insert a combo box or a drop-down list

In a combo box, users can select from a list of choices that you provide or they can type in their own information. In a drop-down list, users can only select from the list of choices.

combo box button

Select the content control, and then select Properties .

To create a list of choices, select Add under Drop-Down List Properties .

Type a choice in Display Name , such as Yes , No , or Maybe .

Repeat this step until all of the choices are in the drop-down list.

Fill in any other properties that you want.

Note:  If you select the Contents cannot be edited check box, users won’t be able to click a choice.

Insert a date picker

Click or tap where you want to insert the date picker control.

Date picker button

Insert a check box

Click or tap where you want to insert the check box control.

Check box button

Use the legacy form controls

Legacy form controls are for compatibility with older versions of Word and consist of legacy form and Active X controls.

Click or tap where you want to insert a legacy control.

Legacy control button

Select the Legacy Form control or Active X Control that you want to include.

Set or change properties for content controls

Each content control has properties that you can set or change. For example, the Date Picker control offers options for the format you want to use to display the date.

Select the content control that you want to change.

Go to Developer > Properties .

Controls Properties  button

Change the properties that you want.

Add protection to a form

If you want to limit how much others can edit or format a form, use the Restrict Editing command:

Open the form that you want to lock or protect.

Select Developer > Restrict Editing .

Restrict editing button

After selecting restrictions, select Yes, Start Enforcing Protection .

Restrict editing panel

Advanced Tip:

If you want to protect only parts of the document, separate the document into sections and only protect the sections you want.

To do this, choose Select Sections in the Restrict Editing panel. For more info on sections, see Insert a section break .

Sections selector on Resrict sections panel

If the developer tab isn't displayed in the ribbon, see Show the Developer tab .

Open a template or use a blank document

To create a form in Word that others can fill out, start with a template or document and add content controls. Content controls include things like check boxes, text boxes, and drop-down lists. If you’re familiar with databases, these content controls can even be linked to data.

Go to File > New from Template .

New from template option

In Search, type form .

Double-click the template you want to use.

Select File > Save As , and pick a location to save the form.

In Save As , type a file name and then select Save .

Start with a blank document

Go to File > New Document .

New document option

Go to File > Save As .

Go to Developer , and then choose the controls that you want to add to the document or form. To remove a content control, select the control and press Delete. You can set Options on controls once inserted. From Options, you can add entry and exit macros to run when users interact with the controls, as well as list items for combo boxes, .

Adding content controls to your form

In the document, click or tap where you want to add a content control.

On Developer , select Text Box , Check Box , or Combo Box .

Developer tab with content controls

To set specific properties for the control, select Options , and set .

Repeat steps 1 through 3 for each control that you want to add.

Set options

Options let you set common settings, as well as control specific settings. Select a control and then select Options to set up or make changes.

Set common properties.

Select Macro to Run on lets you choose a recorded or custom macro to run on Entry or Exit from the field.

Bookmark Set a unique name or bookmark for each control.

Calculate on exit This forces Word to run or refresh any calculations, such as total price when the user exits the field.

Add Help Text Give hints or instructions for each field.

OK Saves settings and exits the panel.

Cancel Forgets changes and exits the panel.

Set specific properties for a Text box

Type Select form Regular text, Number, Date, Current Date, Current Time, or Calculation.

Default text sets optional instructional text that's displayed in the text box before the user types in the field. Set Text box enabled to allow the user to enter text into the field.

Maximum length sets the length of text that a user can enter. The default is Unlimited .

Text format can set whether text automatically formats to Uppercase , Lowercase , First capital, or Title case .

Text box enabled Lets the user enter text into a field. If there is default text, user text replaces it.

Set specific properties for a Check box .

Default Value Choose between Not checked or checked as default.

Checkbox size Set a size Exactly or Auto to change size as needed.

Check box enabled Lets the user check or clear the text box.

Set specific properties for a Combo box

Drop-down item Type in strings for the list box items. Press + or Enter to add an item to the list.

Items in drop-down list Shows your current list. Select an item and use the up or down arrows to change the order, Press - to remove a selected item.

Drop-down enabled Lets the user open the combo box and make selections.

Protect the form

Go to Developer > Protect Form .

Protect form button on the Developer tab

Note:  To unprotect the form and continue editing, select Protect Form again.

Save and close the form.

Test the form (optional)

If you want, you can test the form before you distribute it.

Protect the form.

Reopen the form, fill it out as the user would, and then save a copy.

Creating fillable forms isn’t available in Word for the web.

You can create the form with the desktop version of Word with the instructions in Create a fillable form .

When you save the document and reopen it in Word for the web, you’ll see the changes you made.

Facebook

Need more help?

Want more options.

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

assigning value to structure in c

Microsoft 365 subscription benefits

assigning value to structure in c

Microsoft 365 training

assigning value to structure in c

Microsoft security

assigning value to structure in c

Accessibility center

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.

assigning value to structure in c

Ask the Microsoft Community

assigning value to structure in c

Microsoft Tech Community

assigning value to structure in c

Windows Insiders

Microsoft 365 Insiders

Was this information helpful?

Thank you for your feedback.

IMAGES

  1. Assigning Values to the Member Variables of a Structure using Structure

    assigning value to structure in c

  2. Pointer to Structure in C++

    assigning value to structure in c

  3. Variables in C

    assigning value to structure in c

  4. C# Tutorial Variables

    assigning value to structure in c

  5. Initializing Structure Variables in C Detailed Expalnation Made Easy

    assigning value to structure in c

  6. Structure in C Program with Examples

    assigning value to structure in c

VIDEO

  1. C + + sec9 ( 2D array , struct , and array of struct )

  2. Assigning Value and Printing values in Array || C Programming

  3. YOUR INFERIOR FUNCTION: vulnerable gateway to wholeness

  4. Advanced C++

  5. Data Structure & Programming In C

  6. Structure code in c++ #c++ #shorts #coding #learning #code #rectangle

COMMENTS

  1. c

    3 Answers Sorted by: 142 In C99 standard you can assign values using compound literals: Student s1; s1 = (Student) {.id = id, .name = name, .score = score}; Share Improve this answer Follow answered May 10, 2017 at 10:55 Alexander Ushakov 5,289 3 28 51

  2. C Structures (structs)

    struct myStructure { int myNum; char myLetter; }; int main () { // Create a structure variable of myStructure called s1 struct myStructure s1; // Assign values to members of s1 s1.myNum = 13; s1.myLetter = 'B'; // Print values

  3. C Structures

    We can use the struct keyword to declare the structure in C using the following syntax: Syntax struct structure_name { data_type member_name1; data_type member_name1; .... .... }; The above syntax is also called a structure template or structure prototype and no memory is allocated to the structure in the declaration. C Structure Definition

  4. C struct (Structures)

    #include <stdio.h> #include <string.h> // create struct with person1 variable struct Person { char name [50]; int citNo; float salary; } person1; int main() { // assign value to name of person1 strcpy(person1.name, "George Orwell"); // assign values to other person1 variables person1.citNo = 1984; person1. salary = 2500; // print struct variabl...

  5. Structured data types in C

    As you can see in this example you are required to assign a value to all variables contained in your new data type. To access a structure variable you can use the point like in stu.name. There is also a shorter way to assign values to a structure: typedef struct{ int x; int y; }point; point image_dimension = {640,480}; Or if you prefer to set ...

  6. Structure Assignment (GNU C Language Manual)

    structure assigment such as r1 = r2 copies array fields' contents just as it copies all the other fields. This is the only way in C that you can operate on the whole contents of a array with one operation: when the array is contained in a struct. You can't copy the contents of the data field as an array, because r1.data = r2.data;

  7. CS31: Intro to C Structs and Pointers

    A struct type can be defined to store these four different types of data associated with a student. In general, there are three steps to using structured types in C programs: Define a new struct type representing the structure. Declare variables of the struct type Use DOT notation to access individual field values Defining a struct type

  8. How to Initialize a Struct in C

    Use Explicit Assignments to Initialize a Struct in C. When initializing struct members in C, an alternative method involves declaring a variable and assigning each member with its corresponding value individually. This approach is characterized by explicit assignments, making it clear and straightforward.

  9. Structure in C programming with examples

    How to assign values to structure members? There are three ways to do this. 1) Using Dot (.) operator var_name.memeber_name = value; 2) All members assigned in one statement struct struct_name var_name = {value for memeber1, value for memeber2 …so on for all the members}

  10. Pointer to a Structure in C

    Accessing members using Pointer. There are two ways of accessing members of structure using pointer: Using indirection ( *) operator and dot (.) operator. Using arrow ( ->) operator or membership operator. Let's start with the first one. Using Indirection (*) Operator and Dot (.) Operator.

  11. C structs and Pointers (With Examples)

    ptr = (struct person*) malloc(n * sizeof(struct person)); Then, we used the ptr pointer to access elements of person. In this tutorial, you'll learn to use pointers to access members of structs. You will also learn to dynamically allocate memory of struct types with the help of examples.

  12. Assign one struct to another in C

    Assign one struct to another in C Ask Question Asked 13 years, 11 months ago Modified 2 years, 10 months ago Viewed 246k times 182 Can you assign one instance of a struct to another, like so: struct Test t1; struct Test t2; t2 = t1; I have seen it work for simple structures, bu does it work for complex structures?

  13. Structure Pointer in C

    Structure Pointer in C Read Courses Practice A structure pointer is defined as the pointer which points to the address of the memory block that stores a structure known as the structure pointer. Complex data structures like Linked lists, trees, graphs, etc. are created with the help of structure pointers.

  14. Structure (struct) in C [Complete Guide]

    Structure Initialization in C. It can be initialized in various ways. Method 1: Specify the initializers within the braces and seperated by commas when the variables are declared as shown below: struct Student { char name [25]; int age; char branch [10]; //F for female and M for male char gender; }S1 = {"John",14,"CSE","M"}; Method 2: Specify ...

  15. C Language Structures

    Structure members have no meaning individually without the structure. In order to assign a value to any structure member, the member name must be linked with the structure variable using a dot . operator also called period or member access operator. For example: #include<stdio.h> #include<string.h> struct Student { char name[25]; int age; char ...

  16. Structure Assignment and Its Pitfall in C Language

    If structure has pointer or array member, please consider the pointer alias problem, it will lead dangling pointer once incorrect use. Better way is implement structure assignment function in C, and overload the operator= function in C++. Reference: stackoverflow.com: structure assignment or memcpy; stackoverflow.com: assign one struct to ...

  17. How to Assign One Structure to Another

    0:00 / 7:02 How to Assign One Structure to Another Krishna Lede 390 subscribers Subscribe 1.6K views 3 years ago #Krishnalede #Learncwithperfection In this video lesson we will see we can...

  18. Struct in C (assign value into array of struct)

    Struct in C (assign value into array of struct) Ask Question Asked 3 years ago Modified 3 years ago Viewed 1k times 2 I have this two different code writing in C. Both of them just try to assign value into structure. And i'm using cs50 library to help me to get string in simple way. For the first code, when i'm trying to execute it got an error.

  19. Nested Structure in C with Examples

    1. By separate nested structure: In this method, the two structures are created, but the dependent structure (Employee) should be used inside the main structure (Organisation) as a member. Below is the C program to implement the approach: C. #include <stdio.h>. #include <string.h>. struct Employee.

  20. How To Handle Null Values In C#

    C# data types are divided into two categories - the first is Value Type, and the other is Reference Type. If you have ever considered a value type variable, then it can never be assigned a NULL value, but the developers of C# can assign a NULL value to any Reference type variables. we will see the use of NULL values with the help of examples.

  21. Assigning values to arrays in a structure in c

    1 You can only use an initializer when you define a variable. You can't define a variable, then try to initialize it later using an initializer. You will either have to assign values to the elements of the array fields of your structure, or use an initializer at the point of definition. struct student student = { { 99, 99, 99 }, { 1, 2, 3 } };

  22. Create a form in Word that users can complete or print

    Default Value Choose between Not checked or checked as default. Checkbox size Set a size Exactly or Auto to change size as needed. Check box enabled Lets the user check or clear the text box. Set specific properties for a Combo box. Drop-down item Type in strings for the list box items.

  23. Struct array assignment in c

    2 Answers. what you are doing is defining the struct after its has been declared you can only assign to a struct after it has been declared. a struct can be declared and initialized in once sentence (i mean during declaration) as so. typedef struct _str { int arraySize; int a [10]; } str; int main () { str s [10] = { {1, {2,3,4}}, {2, {3,5,6 ...

  24. c

    The snippet below shows a struct (ckmsgq) with a function pointer as a member (func).Then there is a function (create_ckmsgq) that is assigning a value to the function pointer.However the function isn't using the usual syntax for a function pointer parameter.