Thursday, December 2, 2021

Struct in C part-2

In this article we will discuss only Part-2 of structure concepts as mentioned below

Part 2:
  1. An array of structures
    1. Pass to function by Value
    2. Pass to function by Reference
    3. Return from function 
  2. Why use an array of structures?
  3. struct member is pointer
  4. Pointer structure
  5. Pointer to structure array
  6. 2-D structure (Array of struct has array member or struct array declared as array)
====================================================================================================

An array of structures

Array of structure means creation of multiple structure variables with different entities.
For example, we create student structure with struct members like name, roll number,  address etc.
if we need to store same information for 20 student then we create array of struct and store the information.

struct student S1, S2,S3; means  creation of 3 struct variable of student struct.
struct student S[3]; means instead of create separate struct variable, we created array of struct.

Method-1: define array struct and Initialize each members for each Index.

You look into the above example we done the following activities.
  1. Created struct "book" with following members bookname, bookauthor, pages. price
  2. In the main() function created array of struct "book b1[3]". array size is 3
  3. Initialized struct member of array index like b1[0].bookname, b1[1].bookname and b1[2].bookname.
  4. access the array struct by using for loop.
Method-2: define array struct and Initialize it (definition and Initialization)


Difference between first method and this method is we declared and initialized at the same time.

Method-3: define array struct and ask user to enter the details.


Difference between first method and this method is we are asking user to enter the details.
Note: while reading the string we are not considered space between the strings. we will discuss same in later articles.

struct array is pass to function by Value

This method same as pass struct to function by value but major difference is using "for loop" to pass each individual structure to the calling function.


You look into the program
  1. display() function arguments are struct pointer and integer for Index.
  2. display() is called from main() function and passing address of struct with Indexes (0,1,2)
  3. printing the received struct member values in display() function called from main() function

struct array is pass to function by Reference

For this method we should understand the concepts of array and pointer relationship.

declare an array : int x[4];  this holds the value of 4 Integers and we can see "Contiguous memory for 4 integers"
Note that, the address of "&x[0]" and "x" is the same. It's because the variable name "x" points to the first element of the array.
&x[i] is equivalent to x+i and x[i] is equivalent to *(x+i).

There are three ways of returning an array to a function:
  • Using dynamically allocated array
  • Using static array
  • Using structure

Please Refer below two programs for better understanding.
  • In first program,  function prototype is void display(book);  ==>this gives error when pass the array of struct (Refer: Figure-1)
  • In second program,  function prototype is void display(book*); ==> Pointer holds the base address of an array hence we can retrieve struct members.(Refer: Figure-2)
Figure-1: Below program


Figure-2: Below program

Below program, To find the employee and his salary below the market standard. display the same.



struct array is Return from function


You look into the above program, this program has concepts of return array from function, passing argument to holds the return value and receive the return value by pointer.

Method-1: Initialize the local struct variable  and update to the function argument struct variable "return_value" and finally return the "array of struct".
  • Emp *Rec_SW_Div; Created for holds the function Market_correction() return value.
  • Emp return_value[EMP_SIZE]; when the function returned , local stack frame is deleting hence pass the argument to hold the current value of struct.
  • function return type is "struct pointer" because return value is array type hence it holds the base address of  "Array of struct".
Method-2: use static variable to hold current struct member values. 
static scope is through out the file hence if program come out of function static still holds the values because its stack frame not yet deleted.



struct member is pointer and pointer structure

Below example program clarifies many of our doubts regarding pointer concepts.

we defined struct with two pointer struct members like *ptrobj1, *ptrobj2.
we declared struct variable "M" in main() like this main_struct M={20, &a, b};
Keep observe the declaration first pointer referred with address and second pointer is with value.
when try to dereference the struct members from struct var "M"
  • M.ptrobj1 same as &a. why because we are passing address of  variable "a".
  • *(M.ptrobj1) same as *(&a)   Note: *(&a) same as a because we are dereference the value of "address a"
  • M.ptrobj2 is value of variable "b". we are passing variable b so it holds only value of "b". don't confuse here M.ptrobj2 not holding any reference.
  • *(M.ptrobj2) does not give any value because struct member does not create any stack frame.
Address of var a is 457104488
Value of var a is 10
Address of var b is 457104492
Value of var b is 20
Normal struct member value is =20
Pointer struct member1 address is =457104488
Pointer struct member1 value is =10
Pointer struct member2 address is =20



Pointer structure and struct member is pointer is different. we are not using arrow operator (->) for access the member of "struct member is pointer". shown in above program.

Below program explain, when to use arrow operator.

  • struct "main_struct" contains the pointer struct member "ptr_struct"
  • "ptr_struct" is type "substruct_L1" has one normal member "str_member" and one struct member "member_L1" and type is "substruct_L2"
  • struct "substruct_L2" has one struct member type "int"
  • In the main() function  declared struct variables like  "L2", ptrobj1, M
  • In the struct variable "M", address of  "ptrobj1" is passed. 
  • struct variable "ptrobj1"  has value and struct L2 value.
see the declaration of struct variable in main() function.
  • main_struct M={20, &ptrobj1}; Normal struct var declaration but struct member is pointer.
  • main_struct *ptr_M =&M; this is pointer structure because pointer struct var "ptr_M " created and assigned with  address of "M"



I clarified when to use arrow operator and dot operator for access the struct members.

Pointer to structure array

In the above examples we accessed struct members by using for loop.
In this program we will access struct member by pointer variable.

Below figure shows that pointing to struct member when pointer increment done.
declaration of array of struct :  Emp SW_Div[10];
declaration of pointer to the array of struct:  Emp *ptr_struct=SW_Div;


Example program as follows:


2-D structure : Array of struct has array member 

In the below program, "struct_main" is an array of struct again  struct member declared as an array of size 3 "int stmem[3];" hence it is called 2-D struct.





2-D structure : struct array declared as array
In the below program, "Emp" is an array of struct again  structure variable declared as array of size 4.  "Emp SW_Div[4]" =>Internally it look like this SW_Div[2][4].



========================================================================================================

Topics like struct contains function pointer, struct memory allocation and structure padding will discuss in the next article
Link : 

========================================================================================================

Thanks for visiting the Post..!!

========================================================================================================

No comments:

Post a Comment