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

CP_Module-4b_ppt-2023-24

This document provides a comprehensive overview of pointers in C, covering their fundamentals, declaration, initialization, dereferencing, and operations such as pointer arithmetic. It also discusses the advantages of pointers, their relationship with arrays and functions, and the concept of dynamic memory allocation, detailing functions like malloc(), calloc(), realloc(), and free(). Overall, it serves as a guide for understanding and effectively using pointers in C programming.

Uploaded by

niharpadave2812
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)
2 views

CP_Module-4b_ppt-2023-24

This document provides a comprehensive overview of pointers in C, covering their fundamentals, declaration, initialization, dereferencing, and operations such as pointer arithmetic. It also discusses the advantages of pointers, their relationship with arrays and functions, and the concept of dynamic memory allocation, detailing functions like malloc(), calloc(), realloc(), and free(). Overall, it serves as a guide for understanding and effectively using pointers in C programming.

Uploaded by

niharpadave2812
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/ 25

Module 4.

Pointers
● Fundamentals of pointers
● Declaration, initialization and dereferencing of pointers
● Operations on Pointers
● Concept of dynamic memory allocation
What is Pointer?
- 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. As the pointers store the memory addresses,
their size is independent of the type of data they are pointing to. This size of pointers
only depends on the system architecture.

Syntax
- The syntax of pointers is similar to the variable declaration in C, but we use the ( * )
dereferencing operator in the pointer declaration.
datatype * ptr;
- where
● ptr is the name of the pointer.
● datatype is the type of data it is pointing to.
- The above syntax is used to define a pointer to a variable. We can also define
pointers to functions, structures, etc.
How to use pointers?
The use of pointers can be divided into three steps:

1. Pointer Declaration
2. Pointer Initialization
3. Dereferencing
How to use pointers?
1. 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.
Example:
int *ptr;
- The pointer declared here will point to some random memory address as it is
not initialized. Such pointers are called wild pointers.
How to use pointers?
2. Pointer Initialization

- Pointer initialization is the process where we assign some initial value to the pointer
variable. We generally use the ( & ) addressof operator to get the memory
address of a variable and then store it in the pointer variable.
- Example int var = 10;
int * ptr;

ptr = &var;
- We can also declare and initialize the pointer in a single step. This method is called
pointer definition as the pointer is declared and initialized at the same time.
- Example
int *ptr = &var;
Note: It is recommended that the pointers should always be initialized to some
value before starting using it. Otherwise, it may lead to number of errors.
How to use pointers?
3. Dereferencing

- 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.
Advantage of pointer

1)pointer reduces the code and improves the performance, it is used to retrieving
strings, trees, etc. and used with arrays, structures, and functions.

2) We can return multiple values from a function using the pointer.

3) It makes you able to access any memory location in the computer's memory.
Example on Pointer

Output
#include <stdio.h>

void geeks()

int var = 10;

// declare pointer variable

int* ptr;

// note that data type of ptr and var must be same

ptr = &var;

// assign the address of a variable to a pointer

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

printf("Value at var = %d \n", var);

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

// Driver program

int main() {

geeks();

return 0;
Operations on pointers
1) Pointer assignment:

- We can use a pointer variable on right hand side of assignment statement as we


assign its value to another variable.
- If p1 and p2 are pointer variable then we may write.

p1=p2;

- Here both p1 and p2 will point to same variable.


Operations on pointers

2) Pointer Arithmetic
- There are only two operations that you may use on pointers: addition and subtraction.
Suppose p1 is an integer pointer with the current value 6000.

The expression, p1=p1+10;

- Makes p1 point to the tenth element of p1’s type beyond the one it currently points to. If
integer is 2 byte long, the news value of p1 will be 6020 and not 6010.

The expression, p1 ++;

- Will increment p1. The new value of p1 will be 6002 and not 6001.

The expression, p1 - - ;

- Will decrement p1. The new value of p1 will be 5998.


- Each time the pointer is incremented, it points to the memory location of the next element of
its base type. Each time it is decremented, its points to the location of the previous element.
In case of character pointers it will cause increment and decrement by 1 only.
Operations on pointers

2) Pointer Arithmetic

- If p1 and p2 are pointers, then following statements

y=*p1 * *p2;

sum =sum + *p1;

*p2=*p2+10;

- Are all valid statements.


Pointers and arrays:
- There is close relationship between pointer and arrays. Any operation that can
be achieved by array subscripting can also be achieved by using pointers. It
uses less memory and program runs faster.
- The array name is a pointer to the first element of the array.
For example,
char c [MAX];
- The array name c is a pointer to first element c [0].
- If cptr is a character pointer,
char *cptr;
Then
cptr=c;
cptr=&c [0].
- Are equivalent. Here cptr is set to the address of the first element of the c.
Array subscripting can also be defined in terms of pointer arithmetic.
Pointers and arrays:
Example:
Write a program using
pointers to display the
contents of array in reverse
order.

Output
Pointers and Functions:

- Pointers can be used in functions declaration. When pointer variable is used as


formal argument, then calling a function is referred as call by reference/address.
- In call by reference, when a function is called by a program, the address of actual
arguments are copied on to the formal arguments.
- That is, formal and actual arguments are referencing to same memory location.
Therefore change in value of formal argument affects the value of actual argument.
- The call by reference is used when function produces more than one value and
provides these values to the caller.
Pointers and Functions:

Example:
Program to swap two numbers using call by
address (reference)

Output
Dynamic memory allocation in C

As it can be seen that the length (size) of the array above made is 9. But what if there is a
requirement to change this length (size). For Example,

- If there is a situation where only 5 elements are needed to be entered in this array. In this
case, the remaining 4 indices are just wasting memory in this array. So there is a
requirement to lessen the length (size) of the array from 9 to 5.
- Take another situation. In this, there is an array of 9 elements with all 9 indices filled. But
there is a need to enter 3 more elements in this array. In this case, 3 indices more are
required. So the length (size) of the array needs to be changed from 9 to 12.
Dynamic memory allocation in C
- This procedure is referred to as Dynamic Memory Allocation in C.
- Therefore, 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.
- C provides some functions to achieve these tasks. There are 4 library functions
provided by C defined under <stdlib.h> header file to facilitate dynamic memory
allocation in C programming. They are:
1. malloc()
2. calloc()
3. free()
4. realloc()
Dynamic memory allocation in C
- Before learning above functions, let's understand the difference between static
memory allocation and dynamic memory allocation.
A quick look at the methods used for dynamic memory allocation.
Dynamic memory allocation in C
1. malloc() method
- The malloc() function allocates single block of requested memory.
- It doesn't initialize memory at execution time, so it has garbage value initially.
- It returns NULL if memory is not sufficient.

Syntax of malloc() in C

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

For Example:

ptr = (int*) malloc(100 * sizeof(int));


Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory. And, the
pointer ptr holds the address of the first byte in the allocated memory.
Dynamic memory allocation in C

2. calloc() method
- “calloc” or “contiguous allocation” method in C is used to dynamically allocate the
specified number of blocks of memory of the specified type. it is very much similar to
malloc() but has two different points and these are:

- The calloc() function allocates multiple block of requested memory.


- It initially initialize all bytes to zero.
- It returns NULL if memory is not sufficient.
- The syntax of calloc() function is given below:
ptr=(cast-type*)calloc(number, byte-size)
Dynamic memory allocation in C
3. realloc() method

- If memory is not sufficient for malloc() or calloc(), you can reallocate the memory by
realloc() function. In short, it changes the memory size.
- Let's see the syntax of realloc() function.

ptr=realloc(ptr, new-size)
Dynamic memory allocation in C
4. free() method

- The memory occupied by malloc() or calloc() functions must be released by


calling free() function. Otherwise, it will consume memory until program exit.
- Let's see the syntax of free() function.

free(ptr)

You might also like