Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Accessing Structure Members in C, Summaries of C programming

Array elements are accessed using the Subscript variable, Similarly Structure members are ... Nested Structures are allowed in C Programming Language.

Typology: Summaries

2021/2022

Uploaded on 09/27/2022

johnatan
johnatan 🇺🇸

4

(29)

280 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Accessing Structure Members in C:
1. Array elements are accessed using the Subscript variable, Similarly Structure members are
accessed using dot [.] operator.
2. (.) is called as “Structure member Operator”.
3. Use this Operator in between “Structure name” & “member name”
Example :
#include<stdio.h>
#include<conio.h>
struct Vehicle
{
int wheels;
char vname[20];
char color[10];
} v1 = {4,"Maruti 800","White"};
void main ()
{
printf ("Vehicle No of Wheels : %d",v1.wheels);
printf("Vehicle Name : %s",v1.vname);
printf("Vehicle Color : %s",v1.color);
getch();
}
Structure within Structure: Nested Structure
Structure written inside another structure is called as nesting of two structures.
Nested Structures are allowed in C Programming Language.
We can write one Structure inside another structure as member of another structure.
I- Way of declaration of nested structure:
struct date
{
int date;
int month;
int year;
};
pf3
pf4
pf5

Partial preview of the text

Download Accessing Structure Members in C and more Summaries C programming in PDF only on Docsity!

Accessing Structure Members in C:

  1. Array elements are accessed using the Subscript variable, Similarly Structure members are accessed using dot [.] operator.
  2. (.) is called as “Structure member Operator”.
  3. Use this Operator in between “Structure name” & “member name”

Example : #include<stdio.h> #include<conio.h>

struct Vehicle { int wheels; char vname[20]; char color[10]; } v1 = {4,"Maruti 800","White"};

void main () { printf ("Vehicle No of Wheels : %d",v1.wheels); printf("Vehicle Name : %s",v1.vname); printf("Vehicle Color : %s",v1.color); getch(); }

Structure within Structure: Nested Structure

 Structure written inside another structure is called as nesting of two structures.

 Nested Structures are allowed in C Programming Language.

 We can write one Structure inside another structure as member of another structure.

I- Way of declaration of nested structure:

struct date { int date; int month; int year; };

struct Employee { char ename[20]; int ssn; float salary; struct date doj; } emp1;

Way of Accessing Elements of Nested Structure :

  1. Structure members are accessed using dot operator.
  2. date ‘structure is nested within Employee Structure.
  3. Members of the ‘ date ‘ can be accessed using ’employee’
  4. emp1 & doj are two structure names (Variables)

Explanation of Nested Structure :

Accessing Month Field: emp1.doj.month

Accessing day Field : emp1.doj.day

Accessing year Field: emp1.doj.year

II- Way of declaration of embedded structures

struct Employee { char ename[20]; int ssn; float salary; struct date { int date; int month; int year; }doj; }emp1;

Accessing Nested Members :

Accessing Month Field : emp1.doj.month

Accessing day Field : emp1.doj.day

Accessing year Field : emp1.doj.year

t1 = {"India",11,"Dhoni"} , *sptr = &t1;

int main() {

printf("\nTeam : %s",(sptr).name); printf("\nMemebers : %d",sptr->members); printf("\nCaptain : %s",(sptr).captain);

return 0; }

Passing Structure to Function in C Programming

  1. Structure can be passed to function as a Parameter.
  2. function can also Structure as return type.
  3. Structure can be passed as follow

Example:

#include<stdio.h> #include<conio.h> //------------------------------------- struct Example { int num1; int num2; }s[3]; //------------------------------------- void accept( struct Example *sptr) { printf("\nEnter num1 : "); scanf("%d",&sptr->num1); printf("\nEnter num2 : "); scanf("%d",&sptr->num2); } //------------------------------------- void print( struct Example *sptr) { printf("\nNum1 : %d",sptr->num1); printf("\nNum2 : %d",sptr->num2); } //------------------------------------- void main() { int i;

clrscr(); for (i=0;i<3;i++) accept(&s[i]);

for (i=0;i<3;i++) print(&s[i]);

getch(); }

Accessing Element in Structure Array

  1. Array of Structure can be accessed using dot [.] operator.
  2. Here Records of 3 Employee are Stored.
  3. ‘for loop’ is used to Enter the Record of first Employee.
  4. Similarly ‘for Loop’ is used to Display Record.

Example:

#include<stdio.h> #include<conio.h>

struct Employee { int ssn; char ename[20]; char dept[20]; }emp[3];

//--------------------------------------------- void main() { int i,sum;

//Enter the Employee Details for (i=0;i<3;i++) { printf("nEnter the Employee Details : "); scanf("%d %s %s",&emp[i].ssn,emp[i].ename,emp[i].dept); } //Print Employee Details for (i=0;i<3;i++) { printf("nEmployee SSN : %d",emp[i].ssn);