Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Stucture

Rating
-
Sold
-
Pages
39
Uploaded on
14-02-2024
Written in
2023/2024

Stucture programming

Institution
Course

Content preview

V UNIT

Initializing Structure, Declaring structure variable – structure using typedef- Accessing members – Nested
structure- Accessing elements in a structured array – Array of structure – Accessing elements in structure
array- Passing Array of structure to function – Array of pointers to structures, Bit manipulation to structure
and pointer to structure- Union Basic and declaration – Accessing union members- Pointers to Union-
Dynamic memory allocation, malloc, realloc, free – Allocation Dynamic array- Multidimensional array
using dynamic memory allocation-file:opening, defining, closing, File modes, File types – writing
contents into a file- Reading file contents – Appending an existing file- File permissions and rights,
changing permissions and rights




STRUCTURES IN C

Arrays allow to define type of variables that can hold several data items of the same kind.
Similarly structure is another user defined data type available in C that allows to combine
data items of different kinds.
Structures are used to represent a record. Suppose you want to keep track of your books in a
library. You might want to track the following attributes about each book −

 Title
 Author
 Subject
 Book ID

What is a structure?
A structure is a user defined data type in C/C++. A structure creates a data type that can be
used to group items of possibly different types into a single type.

,Defining a Structure

To define a structure, you must use the struct statement. The struct statement defines a new
data type, with more than one member. The format of the struct statement is as follows −
struct[structure tag]{

member definition;
member definition;
...
member definition;
}[one or more structure variables];


The structure tag is optional and each member definition is a normal variable definition, such
as int i; or float f; or any other valid variable definition. At the end of the structure's definition,
before the final semicolon, you can specify one or more structure variables but it is optional.
Here is the way you would declare the Book structure −
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} book;

Here is an example:
struct Person
{
char name[50];
int citNo;
float salary;
};

,@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

STRUCTURE USING TYPEDEF

 Typedef is a keyword that is used to give a new symbolic name for the existing name
in a C program. This is same like defining alias for the commands.
 Consider the below structure.

struct student
{
int mark [2];
char name [10];
float average;
}

Variable for the above structure can be declared in two ways.

1st way :

struct student record; /* for normal variable */
struct student *record; /* for pointer variable */

2nd way :Typedef struct student status;

 When we use “typedef” keyword before struct<tag_name> like above, after that we can
simply use type definition “status” in the C program to declare structure variable.
 Now, structure variable declaration will be, “status record”.
 This is equal to “struct student record”. Type definition for “struct student” is status. i.e.
status = “struct student”

AN ALTERNATIVE WAY FOR STRUCTURE DECLARATION USING TYPEDEF IN
C:

typedef struct student
{
int mark [2];
char name [10];
float average;
} status;

To declare structure variable, we can use the below statements.

status record1; /* record 1 is structure variable */
status record2; /* record 2 is structure variable */

EXAMPLE PROGRAM FOR C TYPEDEF
// Structure using typedef:

, #include <stdio.h>
#include <string.h>

typedef struct student
{
int id;
char name[20];
float percentage;
} status;

int main()
{
status record;
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
return 0;
}

OUTPUT:
id is: 1
Name is: Raju
Percentage is: 86.500000


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

ACCESSING STRUCTURE MEMBERS

To access any member of a structure, we use the member access operator (.). The member
access operator is coded as a period between the structure variable name and the structure
member that we wish to access. You would use the keyword struct to define variables of
structure type. The following example shows how to use a structure in a program −
#include<stdio.h>
#include<string.h>

struct Books{
char title[50];
char author[50];
char subject[100];
int book_id;
};

int main(){

Written for

Course

Document information

Uploaded on
February 14, 2024
Number of pages
39
Written in
2023/2024
Type
Class notes
Professor(s)
Nirmala
Contains
All classes

Subjects

$8.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
nathiya

Get to know the seller

Seller avatar
nathiya Bharath university
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions