0% found this document useful (0 votes)
6 views29 pages

SEM1-BPL - Lesson10-Pointers

Pointers are variables that store the memory address of another variable, allowing indirect access to its value. The C programming language supports pointer arithmetic, comparisons, and dynamic memory allocation using functions like malloc(), calloc(), realloc(), and free(). Pointers can also be used with functions, enabling features like call by reference and function pointers.

Uploaded by

tuan anh nguyen
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)
6 views29 pages

SEM1-BPL - Lesson10-Pointers

Pointers are variables that store the memory address of another variable, allowing indirect access to its value. The C programming language supports pointer arithmetic, comparisons, and dynamic memory allocation using functions like malloc(), calloc(), realloc(), and free(). Pointers can also be used with functions, enabling features like call by reference and function pointers.

Uploaded by

tuan anh nguyen
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/ 29

BASIC PROGRAMMING LANGUAGE

LESSON 9

Pointers
CONTENT
1. What are Pointers?
2. Pointer Variables and Operators
3. Pointer Arithmetic
4. Pointers and Single Dimensional Arrays
5. Pointer and Multidimensional Arrays
6. Dynamic Memory Allocation
7. Pointers and Functions
8. Summary

Basic Programming Language 2


Address in C
• If you have a variable var in your program, &var will give you its address in
the memory.
• We have used address numerous times while using the scanf() function.
scanf("%d", &var);

Basic Programming Language 3


Variable Address Example

Basic Programming Language 4


What are Pointers?
• A pointer is a variable, which contains the address of a memory location of
another variable.
• If one variable contains the address of another variable, the first variable is
said to point to the second variable.
• A pointer provides an indirect method of accessing the value of a data item.
• Pointers can point to variables of other fundamental data types like int,
char, or double or data aggregates like arrays or structures.

Basic Programming Language 5


Pointer Variables
• A pointer declaration consists of a base type and a variable name preceded
by symbol (*).
• General declaration syntax is :
type *name;

• For example:
int *ptr;

Basic Programming Language 6


Pointer Operators
• There are 2 special operators which are used with pointers & and *.
• Operator & is a unary operator and it returns the memory address of the
operand.
ptr = &var;

• Operator * is the complement of &, returns the value contained in the


memory location pointed to by the pointer variable’s value.
value = *ptr;

Basic Programming Language 7


Pointer Example

Basic Programming Language 8


Pointer Arithmetic
• Addition and subtraction are the only operations that can be performed on
pointers.
int var, *ptr;
ptr = &var;
var = 500;
ptr++;
• Let us assume that var is stored at the address 1000.
• Then ptr has the value 1000 stored in it. Since integers are 2 bytes long,
after the expression “ptr++;” ptr will have the value as 1004 and not 1001.

Basic Programming Language 9


Pointer Arithmetic
• Each time a pointer is incremented, it points to the memory location of the
next element of its base type.
• Each time it is decremented it points to the location of the previous element.
• All other pointers will increase or decrease depending on the length of the
data type they are pointing to.

Basic Programming Language 10


Pointer Arithmetic

Pointer Arithmetic Description

++ptr_var or ptr_var++ Points to next integer after var

—ptr_var or ptr_var— Points to integer previous to var

ptr_var +1 Points to the ith integer after var

ptr_var -1 Points to the ith integer before var

++*ptr_var or (*ptr_var)++ Will increment var by 1

*ptr_var++ Will fetch the value of the next integer after var

Basic Programming Language 11


Pointer Comparisons
• Two pointers can be compared in a relational expression provided both the
pointers are pointing to variables of the same type.
• Pointers of the same type (after pointer conversions) can be compared for
equality. Two pointers of the same type compare equal if and only if they are
both null, both point to the same function, or both represent the same
address.
• Consider that ptr_a and ptr_b are 2 pointer variables, which point to data
elements a and b. In this case the following comparisons are possible:

Basic Programming Language 12


Pointer Comparisons
ptr_a < ptr_b Returns true provided a is stored before b
ptr_a > ptr_b Returns true provided a is stored after b
Returns true provided a is stored before b or ptr_a and ptr_b point to the
ptr_a <= ptr_b
same location
Returns true provided a is stored after b or ptr_a and ptr_b point to the
ptr_a >= ptr_b
same location.
Returns true provided both pointers ptr_a and ptr_b points to the same
ptr_a = ptr_b
data element.
Returns true provided both pointers ptr_a and ptr_b point to different
ptr_a != ptr_b
data elements but of the same type.
ptr_a = NULL Returns true if ptr_ais assigned NULL value (zero)

Basic Programming Language 13


NULL Pointers
• It is always a good practice to assign a NULL value to a pointer variable in
case you do not have an exact address to be assigned. This is done at the
time of variable declaration.
• A pointer that is assigned NULL is called a null pointer.
• The NULL pointer is a constant with a value of zero defined in several
standard libraries.
• For example:
int *ptr = NULL;

Basic Programming Language 14


Pointer & Single Dimensional Arrays
• In most contexts, array names decay to pointers. In simple words, array
names are converted to pointers.
• That's the reason why you can use pointers to access elements of arrays.
However, you should remember that pointers and arrays are not the same.
• The address of an array element can be expressed in two ways :
• By writing the actual array element preceded by the ampersand sign (&)
• By writing an expression in which the subscript is added to the array name

Basic Programming Language 15


Pointer & Single Dimensional Arrays

Basic Programming Language 16


Pointer & Multi Dimensional Arrays
• A two-dimensional array can be defined as a pointer to a group of
contiguous one-dimensional arrays.
• A two-dimensional array declaration can be written as:
data_type (*ptr_var) [expr 2];

• instead of
data_type ptr_var [expr1][expr 2];

Basic Programming Language 17


Dynamic Memory Allocation
• As you know, an array is a collection of a fixed number of values. Once the
size of an array is declared, you cannot change it.
• Sometimes the size of the array you declared may be insufficient. To solve
this issue, you can allocate memory manually during run-time. This is
known as dynamic memory allocation in C programming.
• The C programming language provides several functions for memory
allocation and management.

Basic Programming Language 18


Dynamic Memory Allocation
• These functions can be found in the <stdlib.h> header file:
• void *malloc(int num);
This function allocates an array of num bytes and leave them initialized
• void *calloc(int num, int size);
This function allocates an array of num elements each of which size in bytes will
be size
• void *realloc(void *address, int newsize);
This function re-allocates memory extending it upto newsize
• void free(void *address);
This function releases a block of memory block specified by address

Basic Programming Language 19


malloc() Function Demo

Basic Programming Language 20


calloc() Function Demo

Basic Programming Language 21


realloc() Function Demo

Basic Programming Language 22


Pointers and Functions
• In C programming, it is also possible to pass addresses as arguments to
functions.
• Call by Value
• Call by Reference

• Function Pointers

Basic Programming Language 23


Call By Value

Basic Programming Language 24


Call By Reference

Basic Programming Language 25


Function Pointers
• Function Pointers point to code like normal pointers. In Functions
Pointers, function’s name can be used to get function’s address.
• A function can also be passed as an arguments and can be returned from a
function.
• By using function pointers, a function can be sent as a parameter to another
function.
• This feature enables the C program to load function dynamically at runtime.
• Syntax:
function_return_type(*Pointer_name)(function argument list)

Basic Programming Language 26


Function Pointers Example

Basic Programming Language 27


Summary
• A pointer is a variable, which contains the address of a memory location of
another variable.
• If one variable contains the address of another variable, the first variable is
said to point to the second variable.
• The C programming language provides several functions for memory
allocation and management.
• To allocate memory dynamically, library functions are malloc(),
calloc(), realloc() and free() are used. These functions are
defined in the <stdlib.h> header file.
• Basic Programming Language 28
29
29

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy