KEMBAR78
Unit 3dsa Pointer | PDF | Pointer (Computer Programming) | Integer (Computer Science)
0% found this document useful (0 votes)
3 views13 pages

Unit 3dsa Pointer

Uploaded by

khadsey44
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)
3 views13 pages

Unit 3dsa Pointer

Uploaded by

khadsey44
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/ 13

Unit III: Linked List

Pointer: Basic concepts, Pointer declaration and initialization, Dynamic Memory Allocation
(malloc, calloc, realloc, free), comparison of sequential and linked organization,
Linked List: Singly, Doubly, and Circular Linked List; Stack and Queue implementation using
Linked list, Representation and manipulation of polynomial using linked list
Pointer
• A pointer is a variable that stores the memory address of another
variable. Instead of holding a direct value, it holds the address where
the value is stored in memory. It is the backbone of low-level memory
manipulation in C. Accessing the pointer directly will just give us the
address that is stored in the pointer. For example,
#include <stdio.h>

int main() {

// Normal Variable
int var = 10;

// Pointer Variable ptr that


// stores address of var
int* ptr = &var;

// Directly accessing ptr will


// give us an address
printf("%d", ptr);
Output
return 0; 0x7fffa0757dd4
}
• Declare a Pointer
A pointer is declared by specifying its name and type, just like simple
variable declaration but with an asterisk (*) symbol added before the
pointer's name.
data_type* name
Here, data_type defines the type of data that the pointer is pointing to.
An integer type pointer can only point to an integer. Similarly, a pointer
of float type can point to a floating-point data, and so on.
int *ptr;
In the above statement, pointer ptr can store the address of an integer.
It is pronounced as pointer to integer.
• Initialize the Pointer
Pointer initialization means assigning some address to the pointer
variable. In C, the (&) address of operator is used to get the memory
address of any variable. This memory address is then stored in a
pointer variable.
int var = 10; // Initializing ptr
int *ptr = &var;
In the above statement, pointer ptr store the address of
variable var which was determined using address-of operator (&).
Dereference a Pointer
We have to first dereference the pointer to access the value present at the memory
address. This is done with the help of dereferencing operator(*) (same operator used
in declaration).
#include <stdio.h>

int main() {
int var = 10;

// Store address of var variable


int* ptr = &var;

// Dereferencing ptr to access the value


printf("%d", *ptr);

return 0;
Output
}
10
Size of Pointers
The size of a pointer in C depends on the architecture (bit system) of
the machine, not the data type it points to.
• On a 32-bit system, all pointers typically occupy 4 bytes.
• On a 64-bit system, all pointers typically occupy 8 bytes.
C Pointer Arithmetic
• The pointer arithmetic refers to the arithmetic operations that can be
performed on a pointer. It is slightly different from the ones that we
generally use for mathematical calculations as only a limited set of
operations can be performed on pointers.
These operations include:
• Increment/Decrement
• Addition/Subtraction of Integer
• Subtracting Two Pointers of Same Type
• Comparing/Assigning Two Pointers of Same Type
• Comparing/Assigning with NULL
Pointer increment/decrement
ptr++; // Move to arr[2]
#include <stdio.h>
printf("After another increment: %d\n", *ptr); // 30

int main() { ptr--; // Move back to arr[1]


printf("After decrement: %d\n", *ptr); // 20
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr; // points to arr[0] return 0;
}

printf("Initial value: %d\n", *ptr); // 10

ptr++; // Move to arr[1]


printf("After increment: %d\n", *ptr); // 20
Pointer Addition
#include <stdio.h>

int main() {
int arr[] = {10, 20, 30, 40, 50};
int *p = arr;

printf("Value at p : %d\n", *p); // 10


printf("Value at p + 2 : %d\n", *(p + 2)); // 30
return 0;
}
Pointer substraction
// strlen: returns the length of the string s • The function strlen returns the length of a null-
int strlen(char *s) { terminated string, i.e., it counts how many
char *p = s; characters are present before the null character \0.
while (*p != '\0') { 1. A pointer p is initialized to point to the same
p++; location as s (the start of the string).
} 2. The while loop continues moving the pointer
return p - s; p forward one character at a time until it
reaches the null character \0, which marks the
}
end of the string.
3. This returns the difference between the two
pointers:p now points to the end of the string
(at the null character),s points to the
beginning.p - s gives the number of characters
between them, which is the length of the
string.
char str[] = "Hello"; 'H' 'e' 'l' 'l' 'o' '\0‘
int len = strlen(str); // len = 5 Pointer moves 5 times → returns 5
Pointer to Function
• A function pointer is a type of pointer that stores the address of a function,
allowing functions to be passed as arguments and invoked dynamically. It is useful
in techniques such as callback functions, event-driven programs.
#include <stdio.h>

int add(int a, int b) {


return a + b;
}
Output
int main() { 15

// Declare a function pointer that matches


// the signature of add() fuction
int (*fptr)(int, int);

// Assign address of add()


fptr = &add;

// Call the function via ptr


printf("%d", fptr(10, 5));

return 0;

You might also like