unit 4
unit 4
The Dynamic memory allocation enables the C programmers to allocate memory at runtime.
The different functions that we used to allocate memory dynamically at run time are −
Using dynamic memory allocation functions, we are trying to reduce the wastage of memory.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *dynamicArray;
int size;
scanf("%d", &size);
if (dynamicArray == NULL) {
scanf("%d", &dynamicArray[i]);
free(dynamicArray);
return 0;
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.
Syntax:
struct name_1
{
member1;
member2;
.
.
membern;
struct name_2
{
member_1;
member_2;
.
.
member_n;
}, var1
} var2;
program-
#include <stdio.h>
#include <string.h>
struct Organisation
{
char organisation_name[20];
char org_number[20];
struct Employee
{
int employee_id;
char name[20];
int salary;
} emp;
};
int main()
{
struct Organisation org;
org.emp.employee_id = 101;
strcpy(org.emp.name, "Robert");
org.emp.salary = 400000;
strcpy(org.organisation_name,
"GeeksforGeeks");
strcpy(org.org_number, "GFG123768");
Output:
4)Define pointer and explain how the pointer variable is declared and initialized in C.
A pointer is defined as a derived data type that can store the address of other C variables or a
memory location. We can access and manipulate the data stored in that memory location using
pointers.
Syntax of C Pointers
The syntax of pointers is similar to the variable declaration in C, but we use the ( * )
dereferencing operator in the pointer declaration.
datatype * ptr;
where
1. Pointer Declaration
2. Pointer Initialization
3. Pointer Dereferencing
1. Pointer Declaration
In pointer declaration, we only declare the pointer but do not initialize it. To declare a
pointer, we use the
int *ptr;
2. Pointer Initialization
Pointer initialization is the process where we assign some initial value to the pointer variable.
We generally use the ( & ) addressof operator to get the memory address of a variable and
then store it in the pointer variable.
Example
program-
void geeks()
{
int var = 10;
// Driver program
int main()
{
geeks();
return 0;
}
Syntax:
int main()
{
// declaring some temp variables
int var1 = 10;
int var2 = 20;
int var3 = 30;
return 0;
}
struct tagname{
datatype member1;
datatype member2;
datatype member n;
};
#include<stdio.h>
struct student
char name[20];
int id;
float marks;
};
void main()
int dummy;
scanf("%c",&dummy);
scanf("%s %d %f",s2.name,&s2.id,&s2.marks);
scanf("%c",&dummy);
scanf("%s %d %f",s3.name,&s3.id,&s3.marks);
scanf("%c",&dummy);
printf("%s %d %f\n",s1.name,s1.id,s1.marks);
printf("%s %d %f\n",s2.name,s2.id,s2.marks);
printf("%s %d %f\n",s3.name,s3.id,s3.marks);
Output
union student
{
int id;
char name[20];
};
#include <stdio.h>
int main() {
// Array of pointers to strings (city names)
const char *cities[] = {
"New York",
"London",
"Tokyo",
"Paris",
"Sydney"
};
return 0;
}
Output –
List of Cities:
1. New York
2. London
3. Tokyo
4. Paris
5. Sydney
#include <stdio.h>
#include <string.h>
int main() {
// Declare a structure variable
struct Person person1;
return 0;
}
Output-
Person Information:
Name: John Doe
Age: 25
Height: 5.90 feet
Pass by Value: In this method, a copy of the entire structure is passed to the function.
c
• #include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point myPoint = {5, 10};
displayPoint(myPoint);
return 0;
}
The downside of passing by value is that it involves copying the entire structure, which can
be inefficient for large structures.
• Pass by Address (Using Pointers): In this method, a pointer to the structure is passed to
the function. This allows the function to directly modify the original structure.
c
• #include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point myPoint = {5, 10};
displayPoint(&myPoint);
return 0;
}
Passing by address is more efficient than passing by value for large structures, and it allows
the function to modify the original structure.
• Pass by Reference (Using Pointers): Although C does not have true pass by reference,
you can simulate it by passing a pointer to the structure. This way, modifications made inside
the function affect the original structure.
c
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point myPoint = {5, 10};
displayPoint(&myPoint);
return 0;
}
11)Write a C program to calculate the student total marks, subject’s total marks and total marks
using array of structures.
#include <stdio.h>
struct Subject {
char name[50];
int marks;
};
struct Student {
char name[50];
};
int main() {
printf("Name: ");
scanf("%s", students[i].name);
scanf("%d", &students[i].subjects[j].marks);
students[i].totalMarks = 0;
students[i].totalMarks += students[i].subjects[j].marks;
subjectTotal[j] += students[i].subjects[j].marks;
overallTotal += students[i].totalMarks;
return 0;
12) Write a C program to pass a student structure to function and display structure members in the
function.
#include <stdio.h>
struct Student {
char name[50];
int rollNumber;
float marks;
};
// Function to display student information
printf("Student Information:\n");
int main() {
displayStudent(myStudent);
return 0;
Output-
Student Information:
Marks: 85.50