DS Unit1 Part2
DS Unit1 Part2
#include <stdio.h>
int main() {
int a[4];
int *b = a;
int i;
Output:
&a[0] = 1450734448
&a[1] = 1450734450
&a[2] = 1450734452
&a[3] = 1450734454
Address of array a: 1450734448
In the above program, we can see that there is a difference of 2 bytes between two consecutive
elements of the array a. This is due to the fact that the size of an integer is 2 bytes and every
element of the array is taking 2 bytes of size in the memory.
• We notice that &a[0] and a return same address. It is because variable name a points
towards the first element of the array.
• Since &a[0] is equivalent to a,
&a[1] is equivalent to a+1
&a[2] is equivalent to a+2
&a[3] is equivalent to a+3
.
.
&a[n] is equivalent to a+n
When we add 1 to any pointer, it will add the scale factor based on the datatype.
Scale factor is nothing but the number of bytes each datatype takes.
Accessing Array elements using Pointers
Lets take an array x with four elements and a pointer y that is pointing towards the array x.
Example code,
#include <stdio.h>
int main() {
int x[] = {12, 32, 43, 22};
int *y = x;
return 0;
}
Output:
Since y+1 is equivalent to &x[1], to read the value which is at address y+1 we give it the
derefencing operator i.e., *(y+1)
Thus, *(y+0) is equivalent to x[0] which is 12
*(y+1) is equivalent to x[1] which is 32
*(y+2) is equivalent to x[2] which is 43
*(y+3) is equivalent to x[3] which is 22
.
*(y+i) is equivalent to x[i]
Array of Pointers
In C, a pointer array is a homogeneous collection of indexed pointer variables that are
references to a memory location. It is generally used in C Programming when we want to
point at multiple memory locations of a similar data type in our C program. We can access the
data by dereferencing the pointer pointing to it.
Syntax:
pointer_type *array_name [array_size];
Here,
• pointer_type: Type of data the pointer is pointing to.
• array_name: Name of the array of pointers.
• array_size: Size of the array of pointers.
Example code:
return 0;
}
Output:
Value 10 is at address 2013
Value 20 is at address 5634
Value 30 is at address 8328
As shown in the above example, each element of the array is a pointer pointing to an integer.
We can access the value of these integers by first selecting the array element and then
dereferencing it to get the value.
int main()
{
Output:
Sentry
Thor
Spiderman
The addresses of these strings are
2345
2351
2355
Here, the total memory used is the memory required for storing the strings and pointers
without leaving any empty space hence, saving a lot of wasted space.
Structures in C
The structure in C is a user-defined data type that can be used to group items of possibly
different types into a single type. The struct keyword is used to define the structure in the C
programming language. The items in the structure are called its member and they can be of
any valid data type.
Syntax:
struct structure_name
{
Data_type member_name1;
Data_type member_name2;
.
.
}variable1, variable2,..
The above syntax is also called a structure template or structure prototype and no memory is
allocated to the structure in the declaration.
Example:
struct chocolate
{
float price;
int sales;
} eclairs, munch;
When a structure pointer is used, we can access the members of the structure with the help
of ( -> ) arrow operator.
Example code:
#include <stdio.h>
#include <string.h>
struct Student {
int roll_no;
char name[30];
char branch[40];
int batch;
};
struct Student s, *ptr;
int main()
{
ptr = &s;
printf("Enter the Roll Number of Student\n");
scanf("%d", &ptr->roll_no);
printf("Enter Name of Student\n");
scanf("%s", &ptr->name);
printf("Enter Branch of Student\n");
scanf("%s", &ptr->branch);
printf("Enter batch of Student\n");
scanf("%d", &ptr->batch);
return 0;
}
Output:
Enter the Roll Number of Student
27
Enter Name of Student
Ritwiz
Enter Branch of Student
Computer_Science_And_Engineering
Enter batch of Student
2022
Student details are:
Roll No: 27
Name: Ritwiz
Branch: Computer_Science_And_Engineering
Batch: 2022
Self-referential Structures
A self referential structure is a structure which includes at least one member that is a pointer
which points to structure of same type.
In other words, structures pointing to the same type of structures are self-referential in
nature.
Self referential structures are very useful in creating the data structures like linked lists, trees
and graphs.
struct node {
int data;
struct node* link;
};
int main()
{
struct node n1, n2;
n1.data = 10;
n2.data = 20;
printf("%d", n1.data);
printf("\n%d", n2.data);
return 0;
}
Output:
10
20
int main()
{
struct node *head;
struct node n1, n2, n3;
n1.data = 10;
n2.data = 20;
n3.data = 30;
// Forward links
n1.next_link = &n2;
n2.next_link = &n3;
n3.next_link = NULL;
// Backward links
n1.prev_link = NULL;
n2.prev_link = &n1;
n3.prev_link = &n2;
head = &n1;
printf("The data elements are:\n");
while(head!=NULL)
{
printf("%d\t", head->data);
head = head->next;
}
return 0;
}
Output:
The data elements are:
10 20 30