0% found this document useful (0 votes)
13 views19 pages

Pointer in C

Uploaded by

asmodeushaha5
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)
13 views19 pages

Pointer in C

Uploaded by

asmodeushaha5
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/ 19

How Pointers Works

x
int x=10; 10

int *y =&x; y(address of x)


1000
printf(“%d”, x);
2000
printf(“%u”, &x);
printf(“%u”, y);
printf(“%d”, *y);
printf(“%d”, &y);
Pointer

Pointer is a variable that stores the address of another


variable. The pointer variable might be belonging to any
of the data type such as int, float, char, double, short etc.

Pointer syntax :
data_type *var_name;

Example :
int *p; char*p;
Initialization of Pointer variable
 Pointer initialization is the process of assigning address of a variable to
pointer variable.

 Pointer variable contains address of variable of same data type. In c


address operator & is used to determined the address of a variable. The &
returns the address of the variable.
Example

int a =10;
If a pointer of a
int *ptr; // pointer declaration different type is used,
int ptr=&a; // pointer initialization the program may
misinterpret the data
Or at that address, which
will cause errors.
int*ptr=&a; // initialization and declaration together
Rules of Pointer operations

 A pointer variable can be assigned the address of another variable.

int a = 10;
int *p = &a; // p is assigned the address of a printf("Value of
a: %d\n", *p); // Output: 10

A pointer variable can be assigned the value of another pointer variable.

int a = 20;
int *p1 = &a; // p1 points to a
int *p2 = p1; // p2 now points to the same address as p1
printf("Value of a through p2: %d\n", *p2); // Output: 20
 A pointer variable can e initialized with NULL or zero value.

int *p = NULL; // p is initialized to NULL


if (p == NULL)
{
printf("Pointer is NULL\n"); // Output: Pointer is NULL }

 A pointer variable can be pre-fixed or post-fixed with


increment and decrement operators.
int arr[] = {1, 2, 3};

int *p = arr; // p points to the first element of arr

printf("%d ", *p); // Output: 1

p++; // Move to the next element

printf("%d ", *p); // Output: 2

p--; // Move back to the previous element

printf("%d\n", *p); // Output: 1


 An integer value may be added or subtracted from a
pointer variable

int arr[] = {10, 20, 30, 40};

int *p = arr; // p points to the first element of arr

p += 2; // Move forward by two elements

printf("%d\n", *p); // Output: 30 (now points to arr[2])

p -= 1; // Move back by one element

printf("%d\n", *p); // Output: 20 (now points to arr[1])


Array of Pointers
An array of pointers in C is an array where each element
is a pointer. This can be useful for managing multiple
strings or arrays, as each pointer in the array can point to
different data or elements.

type *array_name[size];
Example
#include <stdio.h>
int main()
{
int a = 10, b = 20, c = 30;
int *arr[3]; // Array of 3 pointers to int

// Assigning addresses of variables to each element of the


pointer array
arr[0] = &a;
arr[1] = &b;
arr[2] = &c;

// Accessing the values through the pointer array


for (int i = 0; i < 3; i++)
{
printf("Value of arr[%d]: %d\n", i, *arr[i]);
}
return 0; }
Pointer As function Arguments
When we pass address to a function, the parameters receiving the address
should be pointers. The process of calling a function using pointers to pass the
address is known as call by reference.

#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int x = 5;
int y = 10;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}
POINTER AND STRUCTURE

 Address of Pointer variable can be obtained using ‘&’ operator.

 Address of each structure can be assigned to the Pointer variable.

 Pointer variable which stores the address of Structure must be declared as


Pointer to structure

Pointer to Structure Syntax


struct student_database;
{
char name [10];
int roll ;
int maks;
} stud1;

struct sudent_database *ptr;


ptr=&stud1;
Example – Pointer in structure

#include <stdio.h>
int main()

{
struct my_structure
{
char name[20];
int number;
int rank;
};
struct my_structure variable = {"Raji", 35,1};
struct my_structure *ptr;
ptr = &variable;
printf("NAME:%s\n", ptr->name);
printf("NAME:%d\n", ptr->number);
printf("NAME:%d\n", ptr->rank);
return 0;}
Self referential structure

Self referential structure means when a structure is


introducing another of the same type.

struct node i *p
Address of node
{ inside p

int i;
struct node *p;

}:
Linked list

A linked list is a linear data structure where elements,


called nodes, are linked together in a sequence. Each
node contains two main parts:

Data: The actual value or data the node holds.

Pointer (or link): A pointer or reference to the next


node in the sequence.
Types of Linked Lists:

1. Singly Linked List - In a singly linked list, each node


contains data and a single pointer to the next node in the
sequence.

2. Doubly Linked List - In a doubly linked list, each node has


two pointers: one pointing to the next node and another
pointing to the previous node.

3. Circular Linked List - In a circular linked list, the last node’s


pointer points back to the head node, creating a circular
structure.
Use of pointers in self referential structures

1. Linked Lists
Each node in a linked list contains a pointer to the next node, allowing nodes to be
linked together in a sequence.
Use of pointers in self referential structures

2. Doubly Linked Lists


In doubly linked lists, each node has pointers to both the previous and the next
node, facilitating bidirectional traversal.
Post increment and pre increment pointers - Example

#include <stdio.h> #include <stdio.h>


int main() int main()
{ { int arr[] = {10, 20, 30};
int arr[] = {10, 20, 30}; int *p = arr;
int *p = arr; printf("%d\n", ++*p); // Increments the
printf("%d\n", *p++); // Prints the value at value at `p` (10 becomes 11), then prints 11.
`p`, which is 10, then increments `p`.
printf("%d\n", *p); // Prints the
printf("%d\n", *p); // Now `p` points to incremented value again, which is 11.
the next element, so this prints 20.
return 0; }
return 0; }

You might also like