0% found this document useful (0 votes)
2 views10 pages

DS Unit1 Part2

The document explains the relationship between arrays and pointers in C, detailing how arrays are collections of items stored in contiguous memory locations and how they can be accessed using pointers. It covers the syntax for declaring arrays, accessing elements through pointers, and introduces the concept of arrays of pointers and structures. Additionally, it discusses self-referential structures and their applications in creating complex data structures like linked lists and trees.

Uploaded by

cotoham700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

DS Unit1 Part2

The document explains the relationship between arrays and pointers in C, detailing how arrays are collections of items stored in contiguous memory locations and how they can be accessed using pointers. It covers the syntax for declaring arrays, accessing elements through pointers, and introduces the concept of arrays of pointers and structures. Additionally, it discusses self-referential structures and their applications in creating complex data structures like linked lists and trees.

Uploaded by

cotoham700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Relationship between Arrays and Pointers

An Array is a collection of items of same datatypes stored in contiguous (next in sequence)


memory locations. We can say that an array is a kind of variable that can hold more than one
value at the same time.
Generally, a simple variable can hold a single value at a time but the array can hold many. It
depends upon the size of the array.
In order to hold many values, an array is allotted with many blocks having index numbers to
access them. These index numbers always start with 0 and end with (the size of the array-1).
Syntax:
Data_type Name_of_array[ size_of_array ];
Example,
int a[10];
Here, int is datatype and x is a variable of an array type with size 10.
When an array x of size 10 is created, the compiler will allocate 10 consecutive units of memory
blocks to the array x.

• The base address of an array is the address of its first element.


• a is a constant pointer pointing to the first element a[0].
• a is a constant pointer i.e., a is a pointer in itself. This means that the variable_name we
give to an array is a pointer that is pointing to the first element a[0] of the array.
Example code,

#include <stdio.h>
int main() {
int a[4];
int *b = a;
int i;

for(i = 0; i < 4; ++i) {


printf("&a[%d] = %d\n",i, &a[i]);
}
printf("Address of array a: %d", a);
return 0;
}

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;

printf("The elements of x are\n");


for(int i = 0; i < 4; ++i) {
printf("x[%d] is %d\n", i, *(y+i));
}

return 0;
}

Output:

The elements of x are


x[0] is 12
x[1] is 32
x[2] is 43
x[3] is 22

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:

// C program to demonstrate the use of array of pointers


#include <stdio.h>
int main()
{
// declaring some temp variables
int var1 = 10;
int var2 = 20;
int var3 = 30;

// array of pointers to integers


int* arr[3] = { &var1, &var2, &var3 };

// traversing using loop


for (int i = 0; i < 3; i++) {
printf("Value %d is at Address: %d\n", *arr[i], arr[i]);
}

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.

Array of Pointers to Character


One of the main applications of the array of pointers is to store multiple strings as an array of
pointers to characters. Here, each pointer in the array is a character pointer that points to the
first character of the string.
Syntax:
char *array_name [array_size];
After that, we can assign a string of any length to these pointers.
Example code
#include <stdio.h>

int main()
{

char* arr[3] = { "Sentry", "Thor", "Spiderman" };

for (int i = 0; i < 3; i++) {


printf("%s\n", arr[i]);
}
printf("The addresses of these strings are\n");
for (int i = 0; i < 3; i++) {
printf("%s\n", &arr[i]);
}
return 0;
}

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;

The variables for structure can also be declared as:


Syntax: struct structure_name variable_name1, variable_name2,..
Example: struct chocolate eclairs;

Accessing structure members


We can access structure members with a variable by using the dot(.) operator.
Syntax:
variable_name.member1;
variable_name.member2;
Example:
eclairs.price;
eclairs.sales;
Pointers to Structure
A structure pointer is defined as the pointer which points to the address of the memory block
that stores a structure known as the structure pointer.
Complex data structures like Linked lists, trees, graphs, etc. are created with the help of
structure pointers.
The structure pointer tells the address of a structure in memory by pointing the variable to
the structure variable.

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);

printf("\nStudent details are: \n");


printf("Roll No: %d\n", ptr->roll_no);
printf("Name: %s\n", ptr->name);
printf("Branch: %s\n", ptr->branch);
printf("Batch: %d\n", 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.

Types of Self Referential Structures


1. Self Referential Structure with Single link
2. Self Referential Structure with multiple links

Self referential structure with single link


Self Referential Structure with single link has only one member/pointer in the structure that is
pointing to same structure type.
The following example will show us how to connect the objects of a self-referential structure
with the single link and access the corresponding data members.
Example code:
#include <stdio.h>

struct node {
int data;
struct node* link;
};

int main()
{
struct node n1, n2;
n1.data = 10;
n2.data = 20;

// Linking the nodes n1 and n2


n1.link = &n2;
n2.link = NULL;

printf("%d", n1.data);
printf("\n%d", n2.data);
return 0;
}

Output:
10
20

Self referential structure with multiple link


Self referential structure with multiple lnks can have more than one pointers that point to
different nodes of same structure type.
Many data structures can be easily constructed using these structures, as they can easily
connect to more than one node at a time.
Example code:
#include <stdio.h>
struct node {
int data;
struct node* prev_link;
struct node* next_link;
};

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

You might also like