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

Pointer

Here are the programs to print elements of array and characters in string along with their memory addresses using pointers: Program 01: #include <stdio.h> int main() { int arr[] = {10, 20, 30, 40}; int *ptr = arr; printf("Elements of array: \n"); for(int i=0; i<4; i++) { printf("Element %d: %d Address: %p\n", i, *ptr, ptr); ptr++; } return 0; } Program 02: #include <stdio.h> int main() { char str[] = "

Uploaded by

Yash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Pointer

Here are the programs to print elements of array and characters in string along with their memory addresses using pointers: Program 01: #include <stdio.h> int main() { int arr[] = {10, 20, 30, 40}; int *ptr = arr; printf("Elements of array: \n"); for(int i=0; i<4; i++) { printf("Element %d: %d Address: %p\n", i, *ptr, ptr); ptr++; } return 0; } Program 02: #include <stdio.h> int main() { char str[] = "

Uploaded by

Yash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

What are Pointers ?

The pointer is a variable which stores the address of another


variable.A pointer is a variable whose value is the address of another
variable, i.e., direct address of the memory location. Like any variable or
constant, you must declare a pointer before using it to store any variable
address
Advantages of using a pointer: -

i) Pointers allows us to access any memory location in the computer's memory.

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

iii)Pointer reduces the code and improves the performance,it is used to retrieving

strings, trees, etc. and used with arrays, structures, and functions.

iv)In c language, we can dynamically allocate memory where the pointer is used.
What is a pointer?
i) The pointer is a variable which stores the address of another variable.
ii) This pointer variable can be of type int, char, array, function, or any other pointer.
iii)The size of the pointer depends on the architecture.
Integer pointer: -
in 32-bit architecture the size of an integer pointer is 2 bytes. in 64-bit

architecture the size of an integer pointer is 4 bytes

Character pointer: -

in 32-bit and 64-bit architecture the size of a character pointer is 1 byte. etc.

Float pointer: -

in 32-bit architecture the size of a float pointer is 4 bytes. in 64-bit

architecture the size of a float pointer is 8 bytes.


Pointer Declaration Syntax: -

Data_type * pointer variable;

The pointer in c language can be declared using * (asterisk symbol). It is also known as

indirection pointer

Eg-

int *p;

float* p;

char * ch;
POINTER DECLARATION STYLES

int* p ; // * style 1*//


int *p ; //* style 2*//
int * p ; //*style 3*//

However ,the style 2 is more popular.

2/9/2023
POINTER INITIALIZATION

As ordinary variables are Initialized with in Declaration part of the program ,


The Pointer variables can initialized by Accessing address of the variable that are
Used in the program .

Syntax :
Data_type *ptr = expression

Where:
data_type = Any Basic Data type
*ptr = pointer variable
expression = another variable or may be constant

2/9/2023
Initialization of pointer variable

int x;
int *p; //Declaration of pointer variable
p=&x; //Initialization of pointer variable
Example: -

int number=50; //number is a normal variable.


int *p=&number; //p is a pointer variable.
* before p indicates that p is pointer variable.
P stores the address of another variable i.e variable number;
// Program to Illustrate the Concept of Pointers

#include <stdio.h>
int main() /* output
{
Address of a: 65524
int a = 10;
int *p; Value of a: 10
p = &a; */

printf("\n\nAddress of a: %x", p);


printf("\n\nValue of a: %d", *p);
return 0;
}
Operation on Pointer
Incrementing a Pointer (++)
Decrementing a Pointer(--)

2/9/2023
Operation on Pointer

• A pointer in c is an address, which is a numeric value.


• Therefore, you can perform arithmetic operations on a pointer
• Most used pointer operation are: ++, --,
• ++/-- will increment/decrement the pointer by the total number of
bytes required to store the data pointed by pointer.
e.g
1) Pointer pointing to int value is incremented by 2 bytes
2) Pointer pointing to char value is incremented by 1 bytes
Increment Operation on Pointer
Print the address of every element in array

#include<stdio.h>
/* Output
int main() Address of 0th element is 65516
{ Address of 1th element is 65518
int a[5]={10,20,30,40,50}; Address of 2th element is 65520
int *p,i; Address of 3th element is 65522
Address of 4th element is 65524
p=&a[0];
*/
for(i=0;i<5;i++)
{
printf("Address of %dth element is %x\n",i,p);
p++;
}
return 0;
}
Decrement Operation on Pointer

Consider following
int x[5]={1,2,3,4,5};
int *p;
x[0] x[1] x[2] x[3] x[4]
1 2 3 4 5
1000 1002 1004 1006 1008

p = &x[4]=1008;
P--=&x[3]=1006;
P--=&x[2]=1004 ……
Print the address of every element in array

#include<stdio.h>
int main() /* output:---
{ Address of 4th element is 65526
int a[5]={10,20,30,40,50}; Address of 3th element is 65524
Address of 2th element is 65522
int *p,i; Address of 1th element is 65520
p=&a[5]; Address of 0th element is 65518
for(i=4;i>=0;i--) */
{
printf("Address of %dth element is %x\n",i,p);
p--;
}
return 0;
}
Passing array to function using pointer

Methods of parameter passing


• Call by value
• Call by reference
Call by value
The call by value method of passing arguments to a
function copies the actual value of an argument into
the formal parameter of the function.
In this case, changes made to the parameter inside the
function have no effect on the argument.
This method is default parameter passing method

2/9/2023
1.Call by Value

#include<stdio.h>
void change(int num) {
printf("Before adding value inside function num=%d \n",num);
num=num+100;
printf("After adding value inside function num=%d \n", num);
}
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(x);//passing value in function
printf("After function call x=%d \n", x);
return 0;
}
Call by Reference

• The call by reference method of passing arguments to a


function copies the address of an actual parameter into
the formal parameter.
• Inside the function, the address is used to access the
actual argument used in the call. It means the changes
made to the parameter affect the passed argument.
• To pass a value by reference, argument pointers are
passed to the functions just like any other value.

2/9/2023
1.Call by Reference

#include<stdio.h>
void change(int *num)
{
printf("Before adding value inside function num=%d \n",*num);
(*num) += 100;
printf("After adding value inside function num=%d \n", *num);
}
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(&x);//passing reference in function
printf("After function call x=%d \n", x);
return 0;
}
Addition exmaple
Swap values using Pointer
Program 01

Write a program to print the elements of a 1D integer array along with memory address of each element using pointer.

(use increment operator on pointer to print the address)


Program 02- Write a program to print characters in string along with memory addresses of each character.

You might also like