0% found this document useful (0 votes)
10 views

Chapter 3 - Data Structures in c - Array

Chapter 3 discusses linear data structures in C/C++, focusing on pointers and arrays. It covers pointer declaration, initialization, dereferencing, and arithmetic, as well as array declaration, initialization, and traversal. The chapter emphasizes the relationship between arrays and pointers, including dynamic memory allocation using the malloc function.

Uploaded by

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

Chapter 3 - Data Structures in c - Array

Chapter 3 discusses linear data structures in C/C++, focusing on pointers and arrays. It covers pointer declaration, initialization, dereferencing, and arithmetic, as well as array declaration, initialization, and traversal. The chapter emphasizes the relationship between arrays and pointers, including dynamic memory allocation using the malloc function.

Uploaded by

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

CHAPTER 3: DATA STRUCTURE

IN C/C++
D ATA S T R U C T U R E a n d A L G O R I T H M S

M.E. LE THANH TUNG


3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.1 Pointer in C/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.
⚬ In C, a pointer can be used to store the memory address of other
variables, functions, or even other pointers. The use of pointers
allows low-level memory access, dynamic memory allocation, and
many other functionality.
⚬ The use of pointers in C can be divided into three steps:
⚬ Pointer Declaration
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.1 Pointer in C/C++:


⚬ Pointer Declaration:
⚬ In pointer declaration, we only declare the pointer but do not
initialize it. To declare a pointer, we use the ( * ) dereference operator
before its name.
⚬ The pointer declared here will point to some random memory
address as it is not initialized. Such pointers are called wild pointers.
⚬ Syntax of pointer declaration:
data_type* pointer_name;

• VD: int* ptr1;


3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.1 Pointer in C/C++:


⚬ Pointer Declaration:
⚬ In pointer declaration, we only declare the pointer but do not
initialize it. To declare a pointer, we use the ( * ) dereference operator
before its name.
⚬ The pointer declared here will point to some random memory
address as it is not initialized. Such pointers are called wild pointers.
⚬ Syntax of pointer declaration:
data_type* pointer_name;
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.1 Pointer in C/C++:


#include <stdio.h>
int main()
{
int* ptr1; // An integer pointer
float* ptr2; // A float pointer
void* ptr3; // A void (none-type) pointer

return 0;
}
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.1 Pointer in C/C++:


⚬ Pointer Initialization:
⚬ Pointer initialization is the process where we assign some initial
value to the pointer variable. We generally use the ( & ) address-of
operator to get the memory address of a variable and then store it
in the pointer variable.
⚬ Syntax of pointer initialization:
pointer_name = &variable_name;
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.1 Pointer in C/C++:


#include <stdio.h>
int main()
{
int a = 10; // An integer variable
float b = 10.5; // A float variable

int* ptr = NULL; // A NULL pointer


int* ptr1 = &a; // An integer pointer
float* ptr2 = &b; // A float pointer

return 0;
}
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.1 Pointer in C/C++:


3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.1 Pointer in C/C++:


⚬ Pointer Deferencing:
⚬ Dereferencing a pointer is the process of accessing the value
stored in the memory address specified in the pointer. We use the
same ( * ) dereferencing operator that we used in the pointer
declaration.
⚬ Syntax of pointer deferencing:
variable = *pointer_name;
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.1 Pointer in C/C++:


⚬ Pointer Deferencing:

int var = 10;

int* ptr; // declare pointer variable


ptr = &var;

printf("Value at ptr = %p \n", ptr);


printf("Value at var = %d \n", var);
printf("Value at *ptr = %d \n", *ptr);
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.1 Pointer in C/C++:


⚬ Pointer arithmetic:
⚬ Increment/Decrement.
int var = 10;

int* ptr;
ptr = &var;

printf("Pointer value = %d \n", ptr);

ptr++;
printf("Pointer value = %d \n", ptr);
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.1 Pointer in C/C++:


⚬ Pointer arithmetic:
⚬ Addition/Subtraction.
int var = 10;

int* ptr1, *ptr2, *ptr3;


ptr1 = &var;
ptr2 = ptr1 + 5;

printf("Pointer1 value = %d \n", ptr1);


printf("Pointer2 value = %d \n", ptr2);
printf("Pointer3 value = %d \n", ptr3);
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.2 Array in C/C++:


⚬ In C, an array is a collection of fixed value numbers of a single type.
⚬ An array is used to store a collection of data of the same type.
⚬ It can be used to store the collection of primitive data types such
as int, char, float, etc., and also derived and user-defined data types
such as pointers, structures, etc.
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.2 Array in C/C++:


⚬ Array declaration:
⚬ In C, we have to declare the array like any other variable before
using it. We can declare an array by specifying its name, the type of
its elements, and the size of its dimensions.
⚬ Syntax of array declaration:

data_type array_name [size];

■ data_type: the data type of array


■ array_name: name of the array
■ size: the number of elements in array
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.2 Array in C/C++:


⚬ Example:
// C Program
#include <stdio.h>
int main()
{
// array declaration:
int A[5]; // integer 1D-array
fl oat B[4][4]; // fl oat 2D-array

return 0;
}
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.2 Array in C/C++:


⚬ Array initialization:
⚬ Initialization in C is the process to assign some initial value to the
variable.
⚬ To assign value for array, by enclosing those initial values in braces
{}.
⚬ For example:
int A[5] = { 1,2,3,4,5};
⚬ The number of values between braces {} shall not be greater
than the number of elements in the array
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.2 Array in C/C++:


⚬ Example:
// C Program
#include <stdio.h>
int main()
{
// 1D-array initialization:
int A[8] = {1,2,3,4,5,6,7,8};
int B[] = {1,2,3,4,5,6,7,8};

// 2D-array initialization:
int X[4][4] = {1,2,3,4,1,2,3,4,
1,2,3,4,1,2,3,4};
int Y[4][4] = {{1,2,3,4},{1,2,3,4},
{1,2,3,4},{1,2,3,4}};
return 0;
}
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.2 Array in C/C++:


⚬ Array’s element:
⚬ In C, each value in an array is an element of array. It is accessed
by its index (index: represents the position in the array, which
starts from zero for the first element).
⚬ To access elements of the array, you use the array name followed
by the index in square brackets:
⚬ For example:
int A[5] = { 1,2,3,4,5};
int x = A[0];
A[1] = 10;
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.2 Array in C/C++:


⚬ Array’s element:
⚬ To access the element of multi-dimensional array, we need multi
indices. For example, to access the element of bi-dimensional array:
int A[3][2] = { 1,2,3,4,5,6};
int x = A[0][1];
A[1][1] = 10;

• Notice : [0] means the element located in first row, [1] means
the element located in second column.
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.2 Array in C/C++:


⚬ Array & pointer:
⚬ In C, the name of an array, is actually a pointer to the first element of
the array. This basically means that we can work with arrays
through pointers!
⚬ For example:
int A[5] = { 3,5,7,9,11};
int* ptr = A;
int x = *(ptr + 1);
*(ptr
• Notice : ptr is +
the2)address
= 10; of first element in array -> (ptr +1)
means the address of second element in array (or A[1]).
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.2 Array in C/C++:


⚬ Array & pointer:
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.2 Array in C/C++:


⚬ Array & pointer:
⚬ A 2-D array is an array of arrays, we can understand 2-D array as they
are made up of n 1-D arrays stored in a linear manner in the memory.
⚬ For example:
int A[3][3] = { {1,2,3},{4,5,6},{7,8,9}};
int* ptr = A;
int x = A[1][2];
int x = *(ptr + 1*3 + 2);
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.2 Array in C/C++:


⚬ Array & pointer:
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.2 Array in C/C++:


⚬ Array traversal:
⚬ Traversal is the process in which we visit every element of the
data structure. For C array traversal, we use for loops to iterate
through each element of the array:
for (int i = 0; i < N; i++)
{
array_name[i];
}
⚬ Notice : N is the number of elements in array.
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.2 Array in C/C++:


⚬ Example:
// C Program to demonstrate array traversal
#include <stdio.h>
int main()
{
// array initialization using initialier list
int A[5] = { 10, 20, 30, 40, 50 };

// array initialization using for loop


float B[5];
for (int i = 0; i < 5; i++)
{
B[i] = (float)i * 2.1;
}
return 0;
}
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.2 Array in C/C++:


⚬ Example:
// C Program to demonstrate array traversal
#include <stdio.h>
int main()
{
// array initialization using initialier list
int A[5] = { 10, 20, 30, 40, 50 };

// array copy
int B[5];
for (int i = 0; i < 5; i++)
{
B[i] = A[i];
}
return 0;
}
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.2 Array in C/C++:


⚬ Example:
// C Program to demonstrate array traversal
#include <stdio.h>
int main()
{
int A[3][3] = { 1,2,3,4,5,6,7,8,9 };
// print out the values of array
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d\t", A[i][j]);
}
printf("\n");
}
return 0;
}
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.2 Array in C/C++:


⚬ Memory allocation:
⚬ C Dynamic Memory Allocation can be defined as a procedure in
which the size of a data structure (like Array) is changed during the
runtime.
⚬ The “malloc” function in C is used to dynamically allocate a single
large block of memory with the specified size. It returns a pointer of
type void.
⚬ Syntax of malloc() in C:

ptr = (cast-type*) malloc(byte-size)


3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.2 Array in C/C++:


⚬ Memory allocation:
⚬ For Example:
int* ptr = (int*) malloc(5 * sizeof(int));
3 . 1 L I N E A R D ATA S T R U C T U R E S :

• 3.1.2 Array in C/C++:


⚬ Example:
#include <stdio.h>
int main()
{
int* A;
int n;
// Get the number of elements for the array
printf("Enter number of elements:"); scanf("%d",&n);
// Dynamically allocate memory using malloc()
A = (int*)malloc(n * sizeof(int));
// Get the elements of the array
for (int i = 0; i < n; ++i) A[i] = i + 1;
// Print the elements of the array
printf("The elements of the array are: \n" );
for (int i = 0; i < n; ++i) printf("%d\t", A[i]);
return 0;
}
DATA STRUCTURE &
ALGORITHMS
THANKS YO U

You might also like