0% found this document useful (0 votes)
7 views33 pages

Pointer

Uploaded by

MU Asad
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)
7 views33 pages

Pointer

Uploaded by

MU Asad
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/ 33

Programming Language

Dr. Rahul Singh (AI Cluster)


Pointer
• A pointer is a variable that stores the memory address of another variable, functions,
or even other pointers.

• The size of the pointer depends on the architecture. However, in 32-bit architecture the
size of a pointer is 4 byte (Depend on compiler).

• Define a pointer which stores the address of an integer


int n = 10;

int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of type
integer.

Dr. Rahul Singh (AI Cluster)


Declaring a pointer
• The pointer in c language can be declared using * (asterisk symbol). It is also known as
indirection pointer used to dereference a pointer.
int *a; //pointer to int
char *c; //pointer to char

• Address in C
#include <stdio.h>
int main()
{
int var = 5;
printf("var: %d\n", var);
// Notice the use of & before var
printf("address of var: %p", &var);
return 0;
}
Dr. Rahul Singh (AI Cluster)
Pointer Syntax
• int* p; // we have declared a pointer p of int type.

• Declare pointers in these ways

int *p1;

int * p2;

• another example of declaring pointers

int* p1, p2; // we have declared a pointer p1 and a normal variable p2

Dr. Rahul Singh (AI Cluster)


Assigning addresses to Pointers
int* pc, c;
c = 5;

pc = &c;

//5 is assigned to the c variable. And, the address of c is assigned to the pc pointer.

int* pc, c;

c = 5;

pc = &c;

printf("%d", *pc);

// Output: 5
Dr. Rahul Singh (AI Cluster)
Assigning addresses to Pointers
int* pc, c;
c = 5;

pc = &c;

c = 1;

printf("%d", c);

printf("%d", *pc);

// Ouptut: 1

// Ouptut: 1
Dr. Rahul Singh (AI Cluster)
Assigning addresses to Pointers
int* pc, c, d;
c = 5;

d = -15;

pc = &c; printf("%d", *pc);

pc = &d; printf("%d", *pc);

// Output: 5

// Ouptut: -15
Dr. Rahul Singh (AI Cluster)
#include <stdio.h>
Pointer example
int main()
{

int* pc, c;

c = 22;

printf("Address of c: %p\n", &c);

printf("Value of c: %d\n\n", c); // 22

pc = &c;

printf("Address of pointer pc: %p\n", pc);

printf("Content of pointer pc: %d\n\n", *pc); // 22

c = 11;

printf("Address of pointer pc: %p\n", pc);

printf("Content of pointer pc: %d\n\n", *pc); // 11

*pc = 2;

printf("Address of c: %p\n", &c);

printf("Value of c: %d\n\n", c); // 2

return 0;
Dr. Rahul Singh (AI Cluster)
}
Pointer example
int a = 10;

int *b;

Printf(“%d”, a)

Printf(“%d”, b)

Printf(“%d”, *b)

(*(&a)) (*(*(&b)))

& --à reference operator

* à de-reference operator

int **c

C = &b

**C
Dr. Rahul Singh (AI Cluster)
Pointer to array
int a[5];
int *p=&a; // Variable p of type pointer is pointing to the address of an integer array a.

a[5] = {10,20,30,40,50}

• a+3 à means after a 3 int variable.

• *(a+3) à *(a+4(3)) à *(1000 + 12) à *(1012) = 40.

• *(a+0) = 10 = a[0]

• For integer a[i] = * (a + i ) = *( a + 2 i) // i = Size of integer.

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

Dr. Rahul Singh (AI Cluster)


Relationship Between Arrays and Pointers
#include <stdio.h>
There is a difference of 4 bytes between two consecutive elements
int main() { of array x. It is because the size of int is 4 bytes (on our compiler).

int x[4]; Notice that, the address of &x[0] and x is the same. It's because the
variable name x points to the first element of the array.
int i;

From the above example, it is clear that &x[0] is equivalent to x. And


for(i = 0; i < 4; ++i) { x[0] is equivalent to *x.

printf("&x[%d] = %p\n", i, &x[i]); Similarly,


}
&x[1] is equivalent to x+1 and x[1] is equivalent to *(x+1).
printf("Address of array x: %p", x); &x[2] is equivalent to x+2 and x[2] is equivalent to *(x+2).
...
return 0; Basically, &x[i] is equivalent to x+i and x[i] is equivalent to *(x+i).

}
Dr. Rahul Singh (AI Cluster)
Relationship Between Arrays and Pointers
#include <stdio.h>
int main() {
int i, x[6], sum = 0;
printf("Enter 6 numbers: ");
for(i = 0; i < 6; ++i) {
scanf("%d", x+i); // Equivalent to scanf("%d", &x[i]);
sum += *(x+i); // Equivalent to sum += x[i]
}
printf("Sum = %d", sum);
return 0;
}
Dr. Rahul Singh (AI Cluster)
Relationship Between Arrays and Pointers
#include <stdio.h>
int main() {
int x[5] = {1, 2, 3, 4, 5};
int* ptr;

ptr = &x[2]; // ptr is assigned the address of the third element


printf("*ptr = %d \n", *ptr);
printf("*(ptr+1) = %d \n", *(ptr+1));
printf("*(ptr-1) = %d", *(ptr-1));
return 0;

}
// 3, 4, 2 Dr. Rahul Singh (AI Cluster)
Relationship Between Arrays and Pointers
#include <stdio.h>
int main()
{
int v[3] = { 10, 100, 200 };
int* ptr;
ptr = v;
for (int i = 0; i < 3; i++) {
printf("Value of *ptr = %d\n", *ptr);
printf("Value of ptr = %p\n\n", ptr);
ptr++;
}
return 0;
} Dr. Rahul Singh (AI Cluster)
Accessing Array Elements using Pointer Arithmetic
#include <stdio.h>
int main()
{
int arr[5] = { 1, 2, 3, 4, 5 };
int* ptr_arr = arr;
for (int i = 0; i < 5; i++) {
printf("%d ", *ptr_arr++);
}
return 0;
} Dr. Rahul Singh (AI Cluster)
Pointer Program to swap two numbers without using
#include<stdio.h> the 3rd variable.
int main(){

int a=10,b=20,*p1=&a,*p2=&b;

printf("Before swap: *p1=%d *p2=%d",*p1,*p2);

*p1=*p1+*p2;

*p2=*p1-*p2;

*p1=*p1-*p2;

printf("\nAfter swap: *p1=%d *p2=%d",*p1,*p2);

return 0;

}
Dr. Rahul Singh (AI Cluster)
#include <stdio.h>
Pass Addresses and Pointers
void swap(int *n1, int *n2); Pass Addresses to Functions
int main()

{
int num1 = 5, num2 = 10;

swap( &num1, &num2);

printf("num1 = %d\n", num1);

printf("num2 = %d", num2);

return 0;

void swap(int* n1, int* n2)

{
int temp;

temp = *n1;

*n1 = *n2;

*n2 = temp;

}
Dr. Rahul Singh (AI Cluster)
Pass Addresses and Pointers
#include <stdio.h> Pass Addresses to Functions
void addOne(int* ptr) {

(*ptr)++; // adding 1 to *ptr

int main()

int* p, i = 10;

p = &i;

addOne(p);

printf("%d", *p);

return 0;

// // 11 Dr. Rahul Singh (AI Cluster)


NULL Pointer
A pointer that is not assigned any value but NULL is known as the NULL pointer. If you
don't have any address to be specified in the pointer at the time of declaration, you can
assign NULL value. It will provide a better approach.

int *p=NULL;

In the most libraries, the value of the pointer is 0 (zero).

Dr. Rahul Singh (AI Cluster)


C Program to find the size of different pointer types
#include <stdio.h> printf("Size of Integer Pointer \t:\t%d bytes\n",
struct str { sizeof(ptr_int));
};
printf("Size of Character Pointer\t:\t%d bytes\n",
void func(int a, int b){};
sizeof(ptr_char));
int main()
printf("Size of Structure Pointer\t:\t%d bytes\n",
{
sizeof(ptr_str));
int a = 10;
char c = 'G'; printf("Size of Function Pointer\t:\t%d bytes\n",

struct str x; sizeof(ptr_func));


int* ptr_int = &a; printf("Size of NULL Void Pointer\t:\t%d bytes",
char* ptr_char = &c; sizeof(ptr_vn));
struct str* ptr_str = &x; return 0;
void (*ptr_func)(int, int) = &func;
}
void* ptr_vn = NULL; Dr. Rahul Singh (AI Cluster)
C Double Pointer (Pointer to Pointer)
As we know that, a pointer is used to store the address of a variable in C. Pointer reduces the
access time of a variable. However, In C, we can also define a pointer to store the address of
another pointer. Such pointer is known as a double pointer (pointer to pointer). The first
pointer is used to store the address of a variable whereas the second pointer is used to store
the address of the first pointer. The syntax of declaring a double pointer is given below.

int **p; // pointer to a pointer which is pointing to an integer.

Dr. Rahul Singh (AI Cluster)


Example.
#include<stdio.h>
void main ()
{ int a = 10;
int *p;
int **pp;
p = &a; // pointer p is pointing to the address of a
pp = &p; // pointer pp is a double pointer pointing to the address of pointer p
printf("address of a: %x\n", p); // Address of a will be printed
printf("address of p: %x\n", pp); // Address of p will be printed
printf("value stored at p: %d\n",*p); // 10 will be printed
printf("value stored at pp: %d\n",**pp);
}
Dr. Rahul Singh (AI Cluster)
C double pointer example
Let's see an example where one pointer points to the address of another pointer.

Dr. Rahul Singh (AI Cluster)


C double pointer example
#include<stdio.h>
int main(){
int number=50;
int *p; //pointer to int
int **p2; //pointer to pointer
p=&number; //stores the address of number variable
p2=&p;
printf("Address of number variable is %x \n", &number);
printf("Address of p variable is %x \n", p);
printf("Value of *p variable is %d \n", *p);
printf("Address of p2 variable is %x \n", p2);
printf("Value of **p2 variable is %d \n", *p);
return 0;
}
Dr. Rahul Singh (AI Cluster)
Incrementing Pointer in C
If we increment a pointer by 1, the pointer will start pointing to the immediate next location.
The Rule to increment the pointer is given below:
new_address= current_address + i * size_of(data type)
int main(){
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u \n",p);
p=p+1;
printf("After increment: Address of p variable is %u \n",p); // in our case, p will get incremented by
4 bytes.
return 0;
}
Dr. Rahul Singh (AI Cluster)
Traversing an array by using pointer
#include<stdio.h>
void main ()
{
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;
int i;
printf("printing array elements...\n");
for(i = 0; i< 5; i++)
{
printf("%d ",*(p+i));
}
}
Dr. Rahul Singh (AI Cluster)
Traversing an array by using pointer
#include<stdio.h>
int main()
{
int *p;
int (*ptr)[5]; // Pointer to an array of 5 integers
int arr[5];
p = arr;
ptr = &arr; // Points to the whole array arr.
printf("p = %p, ptr = %p\n", p, ptr);
p++;
ptr++;
printf("p = %p, ptr = %p\n", p, ptr);
return 0;
}
Dr. Rahul Singh (AI Cluster)
Traversing an array by using pointer
#include<stdio.h>
int main()
{
int arr[] = { 3, 5, 6, 7, 9 };
int *p = arr;
int (*ptr)[5] = &arr;

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


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

printf("sizeof(p) = %lu, sizeof(*p) = %lu\n", sizeof(p), sizeof(*p));


printf("sizeof(ptr) = %lu, sizeof(*ptr) = %lu\n", sizeof(ptr), sizeof(*ptr));
return 0;
}
Dr. Rahul Singh (AI Cluster)
What will be the output of the following program?
void main ()
{
int a[10] = {100, 206, 300, 409, 509, 601}; //Line 1
int *p[] = {a, a+1, a+2, a+3, a+4, a+5}; //Line 2
int **pp = p; //Line 3
pp++; // Line 4
printf("%d %d %d\n", pp-p, *pp - a, **pp); // Line 5
*pp++; // Line 6
printf("%d %d %d\n", pp-p, *pp - a, **pp); // Line 7
++*pp; // Line 8
printf("%d %d %d\n", pp-p, *pp - a, **pp); // Line 9
++**pp; // Line 10
printf("%d %d %d\n",pp-p,*pp - a,**pp); // Line 11
}
Dr. Rahul Singh (AI Cluster)
Pointer to function in C
#include<stdio.h> {
int addition (); int a, b;
int main () printf("Enter two numbers?");
{ scanf("%d %d",&a,&b);
int result; return a+b;
int (*ptr)(); }
ptr = &addition;
result = (*ptr)();
printf("The sum is %d",result);
}
int addition()
Dr. Rahul Singh (AI Cluster)
Pointer to Array of functions in C
#include<stdio.h> %d",result1);
int show(); (*(*ptr+1))(result1);
int showadd(int); }
int (*arr[3])(); int show()
int (*(*ptr)[3])(); {
int a = 65;
int main () return a++;
{ }
int result1; int showadd(int b)
arr[0] = show; {
arr[1] = showadd; printf("\nAdding 90 to the value returned by
ptr = &arr; show: %d",b+90);
result1 = (**ptr)(); }
printf("printing the value returned by show :
Dr. Rahul Singh (AI Cluster)
Dr. Rahul Singh (AI Cluster)

You might also like