0% found this document useful (0 votes)
38 views67 pages

Understanding Pointers in C Programming

The document provides an overview of pointers in programming, detailing their types, such as void pointers and null pointers, as well as their uses in memory management and data structures. It explains pointer arithmetic, how to declare and initialize pointers, and demonstrates their application through code examples. Additionally, it covers the relationship between pointers and arrays, including passing and returning arrays as pointers.

Uploaded by

gigachadmaleme
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)
38 views67 pages

Understanding Pointers in C Programming

The document provides an overview of pointers in programming, detailing their types, such as void pointers and null pointers, as well as their uses in memory management and data structures. It explains pointer arithmetic, how to declare and initialize pointers, and demonstrates their application through code examples. Additionally, it covers the relationship between pointers and arrays, including passing and returning arrays as pointers.

Uploaded by

gigachadmaleme
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

Module 3

SUDHA C,SCOPE,VIT-CHENNAI
& *
(address operator) (indirection
operator/dereference/
value at address operator)

POINTERS
VOID POINTER
NULL POINTER
POINTER ARITHMETIC

SUDHA C,SCOPE,VIT-CHENNAI
POINTERS
• It is a variable that points to the address of another variable

• This variable can be of type int, char, array, function, or any other pointer.

• Pointers are memory addresses(location of data)

• The value stored at that location can be accessed by dereferencing pointer/


indirection operator (*)

• The size of the pointer depends on the architecture.

• 32-bit architecture the size of a pointer is 4 byte.


• 64-bit architecture the size of a pointer is 8 byte.
Java and
Benefits: python do not
[Link] easily access the memory location of any variable directly. support
[Link] facilitates the concept of dynamic memory allocation, making it a
SUDHA C,SCOPE,VIT-CHENNAI pointers
much faster
WHY POINTERS
• Return multiple values from function(Pass by reference).If we pass the pointer to
the function, there will not be any data duplication. So, our program will run
much faster.

• Using pointers, we can dynamically allocate the memory. In runtime, we can


create, resize, deallocate the memory based on our requirement.

• Pointers are used to build complicated data structures like a linked list, graph, tree,
etc
• Access a memory location
 Reading temperature information from the temperature sensor. This
temperature sensor chip will have its own device memory where it will store the
current temperature [Link] can read the temperature value from the sensor
device memory using pointers. Basically, we will assign device memory address
to a pointer variable. using the pointer variable, we can directly read and write
data to the device memory.
SUDHA C,SCOPE,VIT-CHENNAI
POINTER- Illustrations

SUDHA C,SCOPE,VIT-CHENNAI
POINTER- DECLARATION
SYNTAX

EXAMPLE

Assigning address of a variable to pointer variable Integer pointer can store only integer value

SYNTAX pointer variable =& variable;

EXAMPLE p =& a;

• Pointers are not allowed to store memory addresses directly


• Instead ,it can store only address of a variable or NULL value

int *ptr=1000; //illegal SUDHA C,SCOPE,VIT-CHENNAI


POINTERS& ADDRESS

SUDHA C,SCOPE,VIT-CHENNAI
POINTER-EXAMPLE/ILLUSTRATION

#include<stdio.h>
int main()
{
int x=10,y=30;
int *ptr;
ptr=&y;
printf(“%d %d”,x,*ptr);
return 0;
}
SUDHA C,SCOPE,VIT-CHENNAI
POINTERS- SAMPLE CODE
%u prints address in decimal form
#include<stdio.h> %pprints address in hexadecimal form
int main()
{
int *p,a=10;
p=&a;
printf("Address of a:%p",&a);
printf("\nValue of a:%d",a);
printf("\nValue of p:%p",p);
printf("\nValue defeerenced by p:%d",*p);
return 0;
}

SUDHA C,SCOPE,VIT-CHENNAI
CHAIN OF POINTERS/DOUBLE POINTERS

SUDHA C,SCOPE,VIT-CHENNAI
CALL BY REFERENCE
If a function is to be made to return more than one value at a time, then return these values
indirectly by using a call by reference.

SUDHA C,SCOPE,VIT-CHENNAI
SUDHA C,SCOPE,VIT-CHENNAI
SUDHA C,SCOPE,VIT-CHENNAI
SUDHA C,SCOPE,VIT-CHENNAI
SUDHA C,SCOPE,VIT-CHENNAI
SUDHA C,SCOPE,VIT-CHENNAI
VOID POINTERS (Generic Pointers)
• Void pointer can point to a variable of any data type (an integer value or a float to a string of characters)
• The type casting or assignment must be used to turn the void pointer to a pointer of a concrete data type to
which we can refer.

#include <stdio.h>
int main() void pointer cannot be dereferenced
{ printf(“%d”,*vp); //invalid
int a=5,
double b=3.1415;
void *vp;
vp=&a;
printf(“\n a= %d”, *((int *)vp));
vp=&b; Advantage :
printf(“\n a= %f”, *((double *)vp)); The malloc() and calloc() function return the void
pointer, so these functions can be used to allocate the
return 0; memory of any data type.
}
Output:
a= 5 SUDHA C,SCOPE,VIT-CHENNAI
VOID POINTERS (Generic Pointers)
Why we use void pointers?
We use void pointers because of its reusability.
Void pointers can store the object of any type, and we can retrieve the object of any type by using the indirection operator
with proper typecasting.

SUDHA C,SCOPE,VIT-CHENNAI
NULL POINTERS
• A null pointer is a special pointer that points nowhere.

• To get a null pointer ,use the predefined constant NULL, which is defined by several standard
header files, including <stdio.h>, <stdlib.h>, and <string.h>.

• To initialize a pointer to a null pointer


#include <stdio.h>
int *ip = NULL;

• To test it for a null pointer before inspecting the value pointed to


if(ip != NULL)
printf(“%d\n”, *ip);

• It is also possible to refer to the null pointer using a constant 0, and to set null pointers by
simply saying
int *ip = 0;

SUDHA C,SCOPE,VIT-CHENNAI
NULL POINTERS-Example

#include <stdio.h>
int main()
{ Output:
The value of p is 0
int *p;
p = NULL;
printf(“\n The value of p is %u”, p);
return 0;
}

SUDHA C,SCOPE,VIT-CHENNAI
POINTER ARITHMETIC

SUDHA C,SCOPE,VIT-CHENNAI
POINTER ARITHMETIC
Pointers with the assignment operators can be used if the following conditions are met.

Left hand operator Right hand operator


Pointer Null pointer constant int *p=NULL(or 0);
Pointer to an object of Pointer to void
incompatible type
Pointers(compatible type) Pointers(compatible type)

int i=5;
int *ip;
void *vp;
ip = &i;
vp = ip;
printf(“\n *vp= %d”,*((int *)vp));
ip = vp;
printf(“\n *ip= %d”,*ip);
SUDHA C,SCOPE,VIT-CHENNAI
POINTER ARITHMETIC
• Adding or subtracting a pointer and an integer

int main()
{
int a=6,*p,*q;
p=&a;
q=p+1;
printf("%d\n",p);
printf("%d\n",q);
printf("%d\n",q-p);return 0;
}
SUDHA C,SCOPE,VIT-CHENNAI
POINTER ARITHMETIC
• Prefix or postfix increment and decrement operators can be applied
on a pointer variable

• A pointer variable can be compared with another pointer variable of


the same type using relational operators

SUDHA C,SCOPE,VIT-CHENNAI
POINTER ARITHMETIC
The following arithmetic operations will not work on pointers
• Addition of two pointers
• Multiplying a pointer with a number
• Dividing a pointer with a number

SUDHA C,SCOPE,VIT-CHENNAI
POINTER ARITHMETIC
Rule to increment the pointer

SUDHA C,SCOPE,VIT-CHENNAI
#include <stdio.h> #include <stdio.h>
int main() { int main() {
int a = 10, b = 20, c = 30; int arr[3] = {10, 20, 30}; // Normal array
int *pArr[3]; // Pointer array for (int i = 0; i < 3; i++) {
printf("Value at arr[%d]: %d\n", i, arr[i]);
pArr[0] = &a;
}
pArr[1] = &b; return 0;
pArr[2] = &c; }

for (int i = 0; i < 3; i++) {


printf("Value at pArr[%d]: %d\n", i, *pArr[i]);
}
return 0;
}
SUDHA C,SCOPE,VIT-CHENNAI
#include <stdio.h>

int main() {

// A variable
int var = 10;

// Pointer to int
int *ptr1 = &var;

// Pointer to pointer (double pointer)


int **ptr2 = &ptr1;
Output:
printf("var: %d\n", var); var: 10
printf("*ptr1: %d\n", *ptr1); *ptr1: 10
**ptr2: 10
printf("**ptr2: %d", **ptr2);

return 0; SUDHA C,SCOPE,VIT-CHENNAI


// C NULL pointer demonstration
#include <stdio.h>

int main()
{
// declaring null pointer
int* ptr = NULL;

// derefencing only if the pointer have any value


if (ptr == NULL) {
printf("Pointer does not point to anything");
}
else {
printf("Value pointed by pointer: %d", *ptr);
}
Pointer does not point to
return 0; anything
}

SUDHA C,SCOPE,VIT-CHENNAI
POINTERS & ARRAYS
CHARACTER POINTERS
ARRAY OF POINTERS
POINTER TO AN ARRAY

SUDHA C,SCOPE,VIT-CHENNAI
POINTERS & ARRAYS

• Array notation is a form of pointer notation.


• The name of an array is the beginning address of the array( called the base address of
the array).
• The base address of an array is the address of the zeroth element of the array.
• The array name is referred to as an address constant.
• Mentioning the name of the array fetches its base address.

SUDHA C,SCOPE,VIT-CHENNAI
POINTERS & ARRAYS
For any one-dimensional array a and integer i, the following relationships
are always true.
a[i] ≡ *(a+i) ≡ *(i+a) ≡ i[a].

Access value of an array

a[i]
*(a + i)
*(i + a)
i[a]

SUDHA C,SCOPE,VIT-CHENNAI
POINTERS & ARRAYS
32 bit compiler

SUDHA C,SCOPE,VIT-CHENNAI
POINTERS & ARRAYS
16 bit compiler

SUDHA C,SCOPE,VIT-CHENNAI
POINTERS & ARRAYS

#include <stdio.h>
int main()
{
int array[]={10, 20, 30, 40, 50};
printf(“%p\n%p\n%p”, array, &array[0], &array);
return 0;
}

SUDHA C,SCOPE,VIT-CHENNAI
POINTERS & ARRAYS
Traversing an array using pointers

#include<stdio.h>
int main()
{
int a[5],i;
for(i=0;i<3;i++)
scanf("%d",a+i);
for(i=0;i<3;i++)
printf("%d",*(a+i));
return 0;
}

SUDHA C,SCOPE,VIT-CHENNAI
POINTERS & 2D ARRAYS

SUDHA C,SCOPE,VIT-CHENNAI
POINTERS & 2D ARRAYS

SUDHA C,SCOPE,VIT-CHENNAI
POINTERS & 2D ARRAYS
#include<stdio.h>
int main()
{
int arr[3][4] = {{11,22,33,44},{55,66,77,88},
{11,66,77,44}};
int i, j;
for(i = 0; i < 3; i++)
{
printf("Address of %d th array %u \n",i , *(arr + i));
for(j = 0; j < 4; j++)
{
printf("arr[%d][%d]=%d\n", i, j, *( *(arr + i) + j) );
}
printf("\n\n");
}

return 0;
}

SUDHA C,SCOPE,VIT-CHENNAI
POINTERS & 2D ARRAYS
#include <stdio.h> for(i=0;i<3;i++)
int main() { {
int array[3][4] = { {1, 2, 3, 4}, j=1;
{5, 6, 7, 8}, {9, 10, 11, 12} };
int i ,result,j; printf(" %d\n", *(array + i)+j);
for(i=0;i<3;i++) }printf("\n");
{ for(i=0;i<3;i++)
{
printf(" %d\n", array+i);
j=1;
} printf("\n"); printf(" %d\n", *(*(array + i)+j));
for(i=0;i<3;i++) }
{
printf(" %d\n", *(array + i)); return 0;
}printf("\n"); }

SUDHA C,SCOPE,VIT-CHENNAI
POINTERS & 2D ARRAYS
#include<stdio.h> printf("%d\n",a);
main()
{
printf("%d\n",a[0]+1);
int i,j; printf("%d\n",a[0][0]);
int a[3][3]={1,2,3,4,5,6,7,8,9}; printf("%d\n",a+1);
printf("%d\n",*a);
for(i=0;i<3;i++) getch();
{ return 0;
for(j=0;j<3;j++)
{
}
printf("%d\t",*(*(a+i)+j));
}
printf("\n");
}

SUDHA C,SCOPE,VIT-CHENNAI
PASSING/RETURNING 1D ARRAY AS A POINTER

Function definition Function call

void fun(int* array) fun(array);


Passing array as argument {
}

int* fun(int array[]) {


Returning array as argument … fun(array);
return array;
}

SUDHA C,SCOPE,VIT-CHENNAI
PASSING & RETURNING 1D ARRAY USING POINTERS
#include<stdio.h> int* bubble_Sort(int *a,int n)
int* bubble_Sort(int*,int); {
void main () int i, j,temp;
{ for(i = 0; i<n; i++)
int *arr ,n,i; {
scanf("%d",&n); for(j = i+1; j<n; j++)
for(i = 0; i<n; i++) {
scanf("%d",(arr+i)); if(*(a+j) < *(a+i))
arr=bubble_Sort(arr,n); {
temp = *(a+i);
printf(" Sorted List ...\n"); *(a+i) = *(a+j);
for(i = 0; i<n; i++) *(a+j) = temp;
{ } }
printf("%d\n",*(arr+i)); }
} return a;
} }
SUDHA C,SCOPE,VIT-CHENNAI
CHARACTER POINTERS

char *c=“hello”;
h e l l o \0

char *ptr=“Hello world”;

SUDHA C,SCOPE,VIT-CHENNAI
CHARACTER ARRAY VS CHARACTER POINTER

h e l l o \0 h e l l o \0

SUDHA C,SCOPE,VIT-CHENNAI
p
ARRAY OF POINTERS
POINTER TO ARRAY

SUDHA C,SCOPE,VIT-CHENNAI
ARRAY OF POINTERS

Array of pointers can be declared as

//declares an array of 10 pointers


//each pointer points to an integer variable
int *ptr[10];

SUDHA C,SCOPE,VIT-CHENNAI
ARRAY OF POINTERS-Storing address of 3 integer variables

printf(“%d”,*p[1]); Output:
SUDHA C,SCOPE,VIT-CHENNAI
20
ARRAY OF POINTERS-Storing address of 3 arrays

Output:
1 10 100

SUDHA C,SCOPE,VIT-CHENNAI
CHARACTER ARRAY OF POINTERS
char *sports[5]={“golf”,”hockey”,”football”,”cricket”,”shooting”};

SUDHA C,SCOPE,VIT-CHENNAI
CHARACTER ARRAY OF POINTERS-Sample Code

SUDHA C,SCOPE,VIT-CHENNAI
POINTER TO AN ARRAY/ARRAY POINTER
• pointer to access the components(zeroth) of the array

SUDHA C,SCOPE,VIT-CHENNAI
POINTER TO AN ARRAY

Declare a pointer that can point to whole array rather than just a
single component of the array
Syntax:
data type (*var name)[size of array];
Declaration of the pointer to an array:

// pointer to an array of five numbers

int (*ptr)[5] = NULL;

SUDHA C,SCOPE,VIT-CHENNAI
POINTER TO AN ARRAY

SUDHA C,SCOPE,VIT-CHENNAI
POINTERS TO AN ARRAY-Sample Code
points to whole array Points to single component of the array

int main()
{
// Pointer to an array of five numbers
int (*a)[5];
int b[5] = { 1, 2, 3, 4, 5 };
int i = 0;
// Points to the whole array b
a = &b;
for (i = 0; i < 5; i++)
printf(“%d\t”, *(*a + i));
return 0;
} SUDHA C,SCOPE,VIT-CHENNAI
ARRAY OF POINTER vs POINTER TO AN ARRAY

SUDHA C,SCOPE,VIT-CHENNAI
PASSING ARRAY TO A FUNCTION

SUDHA C,SCOPE,VIT-CHENNAI
Pointers and Array
#include <stdio.h>
int main(){
int arr[5] = {1, 2, 3, 4, 5};
int *b = arr;
printf("Address of a[0]: %d value at a[0] :
%d\n",b, *b); address of a[0]: 6422016 value at a[0] : 1
b++; address of a[1]: 6422020 value at a[1] : 2
printf("Address of a[1]: %d value at a[1] : address of a[2]: 6422024 value at a[2] : 3
%d\n", b, *b); address of a[3]: 6422028 value at a[3] : 4
b++; address of a[4]: 6422032 value at a[4] : 5
printf("Address of a[2]: %d value at a[2] :
%d\n", b, *b);
b++;
printf("Address of a[3]: %d value at a[3] :
%d\n", b, *b);
b++;
printf("Address of a[4]: %d value at a[4] :
%d\n", b, *b); return 0; }
SUDHA C,SCOPE,VIT-CHENNAI
Pointers and Array
#include <stdio.h>
int main(){
int arr[5] = {1, 2, 3, 4,
5};
int *b = arr; a[0] = 1
int i; a[1] = 2
for(i = 0; i <= 4; i++) a[2] = 3
{ a[3] = 4
printf("a[%d] = %d\n",i, a[4] = 5
*(b+i));
}
return 0;
} SUDHA C,SCOPE,VIT-CHENNAI
POINTERS &FUNCTIONS

SUDHA C,SCOPE,VIT-CHENNAI
ADDRESS OF FUNCTION

#include<stdio.h>

void sample()
{
printf("sample code\n");
}

int main()
{
printf("Address of function = %p\
n",sample);
return 0;
}

SUDHA C,SCOPE,VIT-CHENNAI
FUNCTION POINTER
• Pointers can also be used to create references to functions.
• In other words, a function pointer is a variable that contains the
address of a function
• Memory stores the function code
• The address or start of a function is referred to as a "function pointer"

return_type (*variable)(argumentType_1,
argumentType_2,....)
function_return_type(*Pointer_name)(function argument list)

void (*ptr)() = &hello;


void hello(){
"(*ptr)();" printf("Hello World"); }
SUDHA C,SCOPE,VIT-CHENNAI
Sample code
#include <stdio.h>
// Defining a function
void hello() { printf("Hello World"); }
Output:
// The main code Hello World
int main() {
// Declaring a function pointer
void (*ptr)() = &hello;
// Calling function using the // function pointer
(*ptr)();
return 0; void (*ptr)() = hello;
} SUDHA C,SCOPE,VIT-CHENNAI
Function Pointer with Arguments

SUDHA C,SCOPE,VIT-CHENNAI
Sample Code
#include <stdio.h>
int addition (int a, int b)
{ return a + b; } Output:
Addition of x: 10 and y: 20 = 30
int main(){
int (*ptr)(int, int) = addition;
int x = 10, y = 20;
int z = (*ptr)(x, y);
printf("Addition of x: %d and y: %d = %d", x, y, z);
return 0; }
SUDHA C,SCOPE,VIT-CHENNAI
Pointer to Function with Pointer Arguments

Values of x: 10 and y: 20 before swap


Values of x: 20 and y: 10 after swap

SUDHA C,SCOPE,VIT-CHENNAI
Array of Function Pointers
int main(){
float (*ptr[])(int, int) =
#include <stdio.h> {add, subtract, multiply,
float add(int a, int b) divide};
{ return a + b; } int a = 15, b = 10;
float subtract(int a, // 1 for addition, 2 for
int b){ return a - b; } subtraction
float multiply(int a, // 3 for multiplication, 4
int b){ return a * b; } for division
int op = 3;
float divide(int a, int
b){ return a / b; } if (op > 5)
return 0;
printf("Result: %.2f",
(*ptr[op-1])(a, b)); return
0; }
SUDHA C,SCOPE,VIT-CHENNAI

You might also like