CFP Unit5 2024-25
CFP Unit5 2024-25
Unit-5
Structure and Functions
Prepared by,
Prof. N. D. Kapale
Topics
•Structure : Definition, Declaration of Structure, Initialization,
Declaration of Structure Variables and Accessing Members.
• Title
• Author
• Subject
• Book ID
Defining a Structure
•The ,struct keyword is used to define the structure. Let's see
the syntax to define the structure in C.
struct structure_name
{
data_type
member1;
data_type
member2;
data_type
memeberN;
};
Keywords in C
•A keyword is a reserved word. You cannot use it as a variable
name, constant name, etc. There are only 32 reserved words
(keywords) in the C language.
•A list of 32 keywords in the c language is given below:
•Let's see the example to define a structure for an entity
employee in c.
struct employee
{ int id;
char name[20];
float salary;
};
Declaring structure variable
•We can declare a variable for the structure so that we can access
the member of the structure easily. There are two ways to declare
structure variable:
struct Student
{
char name[25];
int age;
char branch[10];
//F for female and M for male
char gender;
};
struct Student
{
char name[25];
int age;
char branch[10];
//F for female and M for male
char gender;
}s1, s2; //declaring variables of struct Student
•Which approach is good
•If number of variables are not fixed, use the 1st approach. It
provides you the flexibility to declare the structure variable many
times.
•If no. of variables are fixed, use 2nd approach. It saves your code
to declare a variable in main() function.
•How to initialize structure members?
struct Point
{
int x = 0; // COMPIL ERRO cannot initialize
here memb
here ER R:
ers
int y = 0; // COMPIL ERRO cannot initialize
memb
ers ER R:
•The reason for above error is simple, when a
datatype is declared, no memory is allocated for it.
Memory is allocated only when variables are
created.
Accessing members of the structure
•There are two ways to access structure members:
#include<stdio.h>
void print(int a)
{
printf("From print function ...\n");
printf("Address of a = %p\n",&a);
}
int main()
{
int a = 10;
printf("From Main Function ...\n");
printf("Address of a = %p\n",&a);
print(a);
return 0;
}
Call by reference in C
• In call by reference, the address of the variable is passed into the
function call as the actual parameter.
• The value of the actual parameters can be modified by changing the
formal parameters since the address of the actual parameters is passed.
• In call by reference, the memory allocation is similar for both formal
parameters and actual parameters. All the operations in the function
are performed on the value stored at the address of the actual
parameters, and the modified value gets stored at the same address
• In call by reference, we pass the address of a variable.
• So, if we modify the value, it will affect the original value of a variable.
• Example of Call by Reference
#include<stdio.h>
void print(int *a)
{
printf("From print function ...\n");
printf("Address of a = %p\n",a);
}
int main()
{
int a = 10;
printf("From Main Function ...\n");
printf("Address of a = %p\n",&a);
print(&a);
return 0;
}