Day-3 - 4 - C Programming Foundations For DSA
Day-3 - 4 - C Programming Foundations For DSA
DAY - 3
Structures
struct Person
{
char name[50];
int age;
float salary;
};
Syntax:
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
};
Example:
//Another example of Structure
struct card {
int no;
float amount;
};
int main(){
1
Notes Compiled by: Dr. Anand Motwani, Faculty, SCSE, VIT Bhopal University
In this case, you have defined a structure named card with two members: an integer no and a
floating-point amount. You then create an instance of this structure named c1 and assign values
to its members. Finally, you print the values of these members using printf.
Pointers
Pointer is a special type of variable used to store the memory location address of a
variable.
In c++ programming language, we use the reference operator "&" to access the
address of the variable.
For example, to access the address of a variable marks, we use &marks.
In the c++ programming language, we can create pointer variables of any datatype.
Every pointer stores the address of the variable with the same datatype only.
Syntax: data_type *pointerName;
In C++, void pointer is a pointer variable that is independent of data type. A void
pointer is used to store the address of a variable of any datatype. We use the keyword
"void" to create void pointer.
void *anyPtr;
2
Notes Compiled by: Dr. Anand Motwani, Faculty, SCSE, VIT Bhopal University
Pointer to Pointer (Double Pointer) Concept:
int **d_ptr = &ptr; //declared double pointer and assigned address of ptr.
printf("%d", **d_ptr);
}
// *ptr value at the address
// &num address of operator
The code demonstrates the use of pointers in C, including declaring, assigning, and using
single and double pointers. Here's a step-by-step explanation of the code:
1. int num = 10;: declare an integer variable num and initialize it with the value 10.
2. int *ptr;: declare a pointer to an integer named ptr.
3. ptr = #: assign the address of the integer variable num to the pointer ptr.
Now, ptr points to the memory location where num is stored.
4. printf("%p \n", ptr);: use %p format specifier to print the address stored in
ptr.
5. printf("%d \n", *ptr);: use *ptr to dereference the pointer and access the
value stored at the memory location pointed to by ptr. In this case, it's the value 10,
which you print using %d.
6. int **d_ptr = &ptr;: declare a double pointer to an integer named d_ptr and
assign the address of the pointer ptr to it. So, d_ptr points to the memory location
where ptr is stored.
7. printf("%d", **d_ptr);: use **d_ptr to first dereference d_ptr to get the
address stored in ptr, and then dereference ptr to access the value stored at that
address. Again, it's the value 10.
8. Address vary on different systems.
3
Notes Compiled by: Dr. Anand Motwani, Faculty, SCSE, VIT Bhopal University
Output:
0x7ffdb34d0a7c
10
10
4
Notes Compiled by: Dr. Anand Motwani, Faculty, SCSE, VIT Bhopal University
DAY - 4
Pointer to Array
A pointer to an array in C is a pointer that points to the first element of an array. It can be
used to access the elements of the array and perform various operations on them. Here's an
example of using a pointer to an array:
// Pointer to Array
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr; // Pointer to the first element of the array
In this example:
5
Notes Compiled by: Dr. Anand Motwani, Faculty, SCSE, VIT Bhopal University
2. A pointer ptr is declared and initialized with the address of the first
element of the array arr. This effectively makes ptr a pointer to the
array.
3. The program then uses the pointer ptr to access and print the elements of
the array using pointer arithmetic. It also demonstrates how you can
modify the array elements using the pointer.
4. After modifying the elements using the pointer, it displays the modified
array.
When you run this program, it will print the original array elements, double each
element using the pointer, and then display the modified array.
Pointer to Structure
6
Notes Compiled by: Dr. Anand Motwani, Faculty, SCSE, VIT Bhopal University
#include <stdio.h>
#include <string.h>
// Define a structure
struct Student {
char name[50];
int age;
float grade;
};
int main() {
// Declare a structure variable
struct Student student1;
7
Notes Compiled by: Dr. Anand Motwani, Faculty, SCSE, VIT Bhopal University
Memory Management
malloc and calloc are two functions in C that are used for dynamic memory
allocation. They are both important in data structure implementations, but they
serve slightly different purposes.
Example:
int *arr = (int *)malloc(5 * sizeof(int));
Example:
int *arr = (int *)malloc(5 * sizeof(int));
8
Notes Compiled by: Dr. Anand Motwani, Faculty, SCSE, VIT Bhopal University
Creating Node in Linked List
Steps:
1. Create Structure: Node
struct Node{
int data;
struct Node *next;
};
9
Notes Compiled by: Dr. Anand Motwani, Faculty, SCSE, VIT Bhopal University
5. Connect Nodes (as per the figure of linked list above)
//Connect nodes
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = NULL;
head = n1; //assign n1 to head.
//printf("%d\n", n1->data);
10
Notes Compiled by: Dr. Anand Motwani, Faculty, SCSE, VIT Bhopal University
USE of malloc() and free()
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, *ptr, sum = 0;
return 0;
}
11
Notes Compiled by: Dr. Anand Motwani, Faculty, SCSE, VIT Bhopal University