Basics of Integer Single Pointers
In this section, you are going to learn
How to use single pointer with a variable ?
How to use single pointer with an array of integers ?
How to use single pointer with heap ?
Basics of Single pointers
Step 1 : Define a variable and a pointer
int x = 65;
int *ptr;
Step 2 : Pointer
ptr
points to variablex
ptr = &x;
Step 3 : Use
*ptr
to write tox
*ptr = 100;
Step 4 : Use
*ptr
to read fromx
printf("x = %d, *ptr = %d\n", x, *ptr);
See full program below
#include <stdio.h>
int main(void)
{
int x = 65;
int *ptr;
ptr = &x;
printf("x = %d, *ptr = %d\n", x, *ptr);
*ptr = 100;
printf("x = %d, *ptr = %d\n", x, *ptr);
return 0;
}
Output is as below
x = 65, *ptr = 65
x = 100, *ptr = 100
Step 1 : Define a variable and a pointer
int x = 65;
int *ptr;
Step 2 : Pointer
ptr
points to variablex
ptr = &x;
Step 3 : Use
ptr[0]
to write tox
ptr[0] = 100;
Step 4 : Use
ptr[0]
to read fromx
printf("x = %d, ptr[0] = %d\n", x, ptr[0] );
See full program below
#include <stdio.h>
int main(void)
{
int x = 65;
int *ptr;
ptr = &x;
printf("x = %d, ptr[0] = %d\n", x, ptr[0] );
ptr[0] = 100;
printf("x = %d, ptr[0] = %d\n", x, ptr[0] );
return 0;
}
Output is as below
x = 65, ptr[0] = 65
x = 100, ptr[0] = 100
Step 1 : Define an array and a pointer
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *ptr;
Step 2 : Let Single pointer to point to array
ptr = arr;
OR
ptr = &arr[0];
Step 3 : Access individual integers of array using
ptr[ ]
notation
ptr[2] = 100;
printf("ptr[2] = %d\n", ptr[2] );
Note that, this is same as
*( ptr + 2 ) = 100;
printf(" *( ptr + 2 ) = %d\n", *( ptr + 2 ) );
Note that, this is same as
arr[2] = 100;
printf("arr[2] = %d\n", arr[2] );
Step 4 : Print full array using
ptr
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
printf("ptr[%d] = %d\n", i, ptr[i]);
}
See full program below
#include <stdio.h>
#include <string.h>
int main(void)
{
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *ptr;
ptr = arr;
// Change individual integer using ptr[] notation
ptr[2] = 100;
// Print individual integer using ptr[] notation
printf("ptr[2] = %d\n", ptr[2] );
// Print full array using "ptr"
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
printf("ptr[%d] = %d\n", i, ptr[i]);
}
return 0;
}
Output is as below
ptr[2] = 100
ptr[0] = 1
ptr[1] = 2
ptr[2] = 100
ptr[3] = 4
ptr[4] = 5
ptr[5] = 6
ptr[6] = 7
ptr[7] = 8
ptr[8] = 9
ptr[9] = 10
Step 1 : Define a pointer
ptr
int *ptr;
Step 2 : Allocate memory in heap and let
ptr
point to it
ptr = malloc(10 * sizeof(int));
This allocates 40 Bytes of memory in heap
Step 3 : memset and clear the memory before use
memset(ptr, 0, 10 * sizeof(int) );
This is needed because, memory returned by malloc may have garbage contents !
Step 4 : Access individual integers of heap using
ptr[ ]
notation
ptr[2] = 100;
printf("ptr[2] = %d\n", ptr[2] );
Note that, this is same as
*( ptr + 2 ) = 100;
printf(" *( ptr + 2 ) = %d\n", *( ptr + 2 ) );
Step 5 : Free memory after use
free(ptr);
See full program below
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
int *ptr;
ptr = malloc(10 * sizeof(int));
memset(ptr, 0, 10 * sizeof(int) );
// Change individual integer using ptr[] notation
ptr[0] = 0;
ptr[1] = 1;
ptr[2] = 2;
ptr[3] = 3;
ptr[4] = 4;
ptr[5] = 5;
ptr[6] = 6;
ptr[7] = 7;
ptr[8] = 8;
ptr[9] = 9;
ptr[2] = 200; // or *( ptr + 2 ) = 200;
ptr[3] = 100; // or *( ptr + 3 ) = 300;
// Print individual integer using ptr[] notation
printf("ptr[2] = %d\n", ptr[2] );
// Print full array
for (int i = 0; i < 10 ; i++)
{
printf("ptr[%d] = %d\n", i, ptr[i]);
}
free(ptr);
return 0;
}
Output is as below
ptr[2] = 200
ptr[0] = 0
ptr[1] = 1
ptr[2] = 200
ptr[3] = 100
ptr[4] = 4
ptr[5] = 5
ptr[6] = 6
ptr[7] = 7
ptr[8] = 8
ptr[9] = 9
Step 1 : Define an array and a pointer
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *ptr;
Step 2 : Let Single pointer to point to array
ptr = arr + 2;
OR
ptr = &arr[0] + 2;
With this ptr
is pointing at index 2
Step 3 : Access Bytes in forward direction
Observe that, ptr
can access in total of 8 indexes in forward direction with respect to it’s current location
ptr[0] is equal to arr[2]
ptr[1] is equal to arr[3]
ptr[2] is equal to arr[4]
ptr[3] is equal to arr[5]
ptr[4] is equal to arr[6]
ptr[5] is equal to arr[7]
ptr[6] is equal to arr[8]
ptr[7] is equal to arr[9]
Step 4 : Access Bytes in backward direction
Observe that, ptr
can access in total of 2 indexes in backward direction with respect to it’s current location
ptr[-1] is equal to arr[1]
ptr[-2] is equal to arr[0]
See full program below
#include <stdio.h>
int main(void)
{
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *ptr;
ptr = arr + 2;
printf("------ Printing integers using ptr[] notation ------\n");
printf("ptr[-2] = %d\n", ptr[-2]);
printf("ptr[-1] = %d\n", ptr[-1]);
printf("ptr[0] = %d\n", ptr[0]);
printf("ptr[1] = %d\n", ptr[1]);
printf("ptr[2] = %d\n", ptr[2]);
printf("ptr[3] = %d\n", ptr[3]);
printf("ptr[4] = %d\n", ptr[4]);
printf("------ Printing integers using *ptr notation ------\n");
printf("*(ptr - 2) = %d\n", *(ptr - 2) );
printf("*(ptr - 1) = %d\n", *(ptr - 1) );
printf("*ptr = %d\n", *ptr );
printf("*(ptr + 1) = %d\n", *(ptr + 1) );
printf("*(ptr + 2) = %d\n", *(ptr + 2) );
printf("*(ptr + 3) = %d\n", *(ptr + 3) );
printf("*(ptr + 4) = %d\n", *(ptr + 4) );
return 0;
}
Output is as below
------ Printing integers using ptr[] notation ------
ptr[-2] = 1
ptr[-1] = 2
ptr[0] = 3
ptr[1] = 4
ptr[2] = 5
ptr[3] = 6
ptr[4] = 7
------ Printing integers using *ptr notation ------
*(ptr - 2) = 1
*(ptr - 1) = 2
*ptr = 3
*(ptr + 1) = 4
*(ptr + 2) = 5
*(ptr + 3) = 6
*(ptr + 4) = 7
Relative memory access is applicable if pointer is pointing to heap, strings as well !
Rules of Single Pointer Arithmetic |
|
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Step 1 : Define an array
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Step 2 : Let
ptr
point to arrayarr
int *ptr;
ptr = arr;
Step 3 : Increment
ptr
twice
ptr++;
ptr++;
OR
ptr += 2;
ptr
is now pointing to Index 2
See full program below
#include <stdio.h>
int main(void)
{
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *ptr;
ptr = arr;
ptr++;
ptr++;
// ptr is now at Index 2
printf("ptr[0] = %d\n", ptr[0]);
return 0;
}
Output is as below
ptr[0] = 3
Step 1 : Define an array
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Step 2 : Let
ptr
point to arrayarr
int *ptr;
ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1;
Step 3 : Decrement
ptr
twice
ptr--;
ptr--;
OR
ptr -= 2;
ptr
is now pointing to Index 7
See full program below
#include <stdio.h>
int main(void)
{
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *ptr;
ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1;
ptr--;
ptr--;
// ptr is now at Index 7
printf("ptr[0] = %d\n", ptr[0]);
return 0;
}
Output is as below
ptr[0] = 8
Step 1 : Define an array
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Step 2 : Let
ptr
point to arrayarr
int *ptr;
ptr = arr;
Step 3 : Increment
ptr
twice
ptr = ptr / 2;
This is INVALID
See full program below
#include <stdio.h>
int main(void)
{
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *ptr;
ptr = arr;
ptr = ptr / 2;
return 0;
}
Output is as below
p9_int_sp.c: In function "main":
p9_int_sp.c:11:19: error: invalid operands to binary / (have "int *" and "int")
11 | ptr = ptr / 2;
Note the compilation error !
Step 1 : Define an array
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Step 2 : Let
ptr
point to arrayarr
int *ptr;
ptr = arr;
Step 3 : Increment
ptr
twice
ptr = ptr * 2;
This is INVALID
See full program below
#include <stdio.h>
int main(void)
{
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *ptr;
ptr = arr;
ptr = ptr * 2;
return 0;
}
Output is as below
p10_int_sp.c: In function "main":
p10_int_sp.c:11:19: error: invalid operands to binary * (have "int *" and "int")
11 | ptr = ptr * 2;
Note the compilation error !
Current Module
Previous Module
Next Module
Other Modules