Pointer
Pointer
• 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).
int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of type
integer.
• 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.
int *p1;
int * p2;
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;
// Output: 5
// Ouptut: -15
Dr. Rahul Singh (AI Cluster)
#include <stdio.h>
Pointer example
int main()
{
int* pc, c;
c = 22;
pc = &c;
c = 11;
*pc = 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)))
* à 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+0) = 10 = a[0]
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;
}
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;
}
// 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;
*p1=*p1+*p2;
*p2=*p1-*p2;
*p1=*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;
return 0;
{
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) {
int main()
int* p, i = 10;
p = &i;
addOne(p);
printf("%d", *p);
return 0;
int *p=NULL;