README
M-16-POINTERS-IN-C
Pointer variables
- we n we declare a variable it takes 4 byte in the memory in the stack and or the variable a address is given by the compiler by checking where is a space available in the memory.
#include <stdio.h>
int main()
{
int x=10;
printf("Value of x: %d\n", x);
printf("Address of x: %p\n",&x);
return 0;
}
Value of x: 10
Address of x: 000000000061FE4C
-
for showing address we have to use
&and%p. compiler defines the address. -
pointer variables are those who can store the address of the variable.
-
data_type* pointer_name
#include <stdio.h>
int main()
{
int x=10;
printf("Value of x: %d\n", x);
printf("Address of x: %p\n",&x);
int* ptr;
ptr = &x;
printf("Value of ptr: %p\n", ptr);
printf("Address of ptr: %p\n",&ptr);
return 0;
}
- the pointer variable we have declared also have address
Value of x: 10
Address of x: 000000000061FE4C
Value of ptr: 000000000061FE4C
Address of ptr: 000000000061FE40
-
pointer variable takes 8 byte storage.
-
we can manipulate the address of the pointer . this is called
deference -
we can see the value stored in the address
printf("Value at the address stored in ptr: %d\n", *ptr);
- we can change the value stored in the variable using the pointer as well.
#include <stdio.h>
int main()
{
int x=10;
printf("Value of x: %d\n", x);
printf("Address of x: %p\n",&x);
int* ptr; // declaring a pointer to an integer
ptr = &x; // storing the address of the variable x in the pointer ptr
printf("Value of ptr: %p\n", ptr);
printf("Address of ptr: %p\n",&ptr);
// value at the address stored in ptr
printf("Value at the address stored in ptr: %d\n", *ptr);
// changing the value stored in the address stored in ptr
*ptr = 20;
printf("Value of x after modification: %d\n", x);
return 0;
}
-
the thing is we are going from address to value and changing the value. this is called
dereferencing. when we are accessingaddressfrom value this is calledreference. -
pass by value -
when we are passing the value of x its creating another variable inside the function. the function is just taking the value not the variable thats why main function x value stays as it is and function value changes as we want. variable name is same but we can see the address is different. this is called pass by value
#include <stdio.h>
void fun(int x)
{
x= 20;
printf("Value of x in fun: %d\n", x);
printf("Address of x in fun: %p\n", &x);
}
int main()
{
int x= 10;
fun(x);
printf("Value of x in main: %d\n", x);
printf("Address of x in main: %p\n", &x);
return 0;
}
Value of x in fun: 20
Address of x in fun: 000000000061FE20
Value of x in main: 10
Address of x in main: 000000000061FE4C
- if we want to something like we have declared x variable in main and passing the value to the function and want to make something like if the value gets changed in the function it will be changed in the main function as well . in this case we have to use
pass by reference. we pass the address for this not just the value of the variable .
#include <stdio.h>
void fun(int* x)
{
*x = 20;
printf("Value of x in fun: %d\n", *x);
printf("Address of x in fun: %p\n", x);
}
int main()
{
int x = 10;
fun(&x);
printf("Value of x in main: %d\n", x);
printf("Address of x in main: %p\n", &x);
return 0;
}
Value of x in fun: 20
Address of x in fun: 000000000061FE4C
Value of x in main: 20
Address of x in main: 000000000061FE4C
- scanf working in the same way thats why we are sending reference so that changes are linked
#include <stdio.h>
void scanf(int* x)
{
*x = 10;
printf("Enter a value for x: ");
printf("Value of x: %d\n", *x);
}
int main()
{
int x= 20;
scanf(&x);
printf("Value of x: %d\n", x);
return 0;
}
pointer in array


#include <stdio.h>
int main()
{
int a[5] = {10, 20, 30, 40, 50};
printf("Address of a[0]: %p\n", &a[0]);
printf("Address of a[1]: %p\n", &a[1]);
printf("Address of a[2]: %p\n", &a[2]);
printf("Address of a[3]: %p\n", &a[3]);
printf("Address of a[4]: %p\n", &a[4]);
return 0;
}
- declaring a array name itself is a pointer and it stores the
0thindex address.
#include <stdio.h>
int main()
{
int a[5] = {10, 20, 30, 40, 50};
printf("Address of a[0]: %p\n", &a[0]);
printf("Name of the array a: %p\n", a);
return 0;
}
Address of a[0]: 000000000061FE30
Name of the array a: 000000000061FE30

-
both are same so we can say array name stores the 0th index and array name itself is a pointer
-
we can dereference as well
#include <stdio.h>
int main()
{
int a[5] = {10, 20, 30, 40, 50};
printf("Address of a[0]: %p\n", &a[0]);
printf("Name of the array a: %p\n", a);
// we can dereference as well
printf("Value at the address stored in a: %d\n", *a);
*a= 100; // changing the value at the address stored in a
printf("Value at the address stored in a after modification: %d\n", *a);
return 0;
}
Address of a[0]: 000000000061FE30
Name of the array a: 000000000061FE30
Value at the address stored in a: 10
Value at the address stored in a after modification: 100
- we can proceeded further by increasing index
#include <stdio.h>
int main()
{
int a[5] = {10, 20, 30, 40, 50};
printf("Address of a[0]: %p\n", &a[0]);
printf("Name of the array a: %p\n", a);
// we can dereference as well
printf("Value at the address stored in a: %d\n", *a);
*a= 100; // changing the value at the address stored in a
printf("Value at the address stored in a after modification: %d\n", *a);
*(a+2)= 200; // changing the value at the address stored in a+2
// a+2 gives the address of a[2], so we are changing the value at a[2]
printf("Value at the address stored in a+2 after modification: %d\n", *(a+2));
for(int i=0; i<5; i++)
{
printf("%d ", a[i]);
}
return 0;
}
Address of a[0]: 000000000061FE30
Name of the array a: 000000000061FE30
Value at the address stored in a: 10
Value at the address stored in a after modification: 100
Value at the address stored in a+2 after modification: 40
100 20 200 40 50
- function with array
#include <stdio.h>
void fun(int a[]){
a[1]=200;
}
int main()
{
int a[5] = {10, 20, 30, 40, 50};
fun(a);
printf("%d", a[1]);
return 0;
}
- even not linking with address the array index value changed because
array and string are by default pass by referenceother variable are pass by value. this is done to save memory . and the link is done because array name itself stores the address of the first index.
#include <stdio.h>
void fun(int a[], int n)
{
a[1] = 200;
// printing the modified array
for (int i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
}
int main()
{
int n;
scanf("%d", &n);
int a[n];
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
// old array print
for (int i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
printf("\n");
fun(a, n);
return 0;
}
- if we try to return an array from the function we will just get the first value because the function gets popped from the stack. normal array is not returnable from the function. we need dynamic array.

- passing string in function
#include <stdio.h>
void fun(char s[])
{
printf("String in fun: %s\n", s);
printf("Address of s in fun: %p\n", s);
}
int main()
{
char s[10];
scanf("%s", &s);
// even not giving & it will work as well because it is dealt with address
// scanf("%s", s);
printf("You entered: %s\n", s);
fun(s);
return 0;
}