Basics of Character 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 characters ?
How to use single pointer with heap ?
Basics of Single pointers
Step 1 : Define a variable and a pointer
char x = 65;
char *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)
{
char x = 65;
char *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
char x = 65;
char *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)
{
char x = 65;
char *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
char arr[10] = "Laptop";
char *ptr;
Step 2 : Let Single pointer to point to array
ptr = arr;
OR
ptr = &arr[0];
Step 3 : Access individual characters of array using
ptr[ ]
notation
ptr[2] = 'y';
printf("ptr[2] = %c\n", ptr[2] );
Note that, this is same as
*( ptr + 2 ) = 'y';
printf(" *( ptr + 2 ) = %c\n", *( ptr + 2 ) );
Note that, this is same as
arr[2] = 'y';
printf("arr[2] = %c\n", arr[2] );
Step 4 : Access full string and print using
ptr
notation
printf("ptr = %s\n", ptr);
Note that, this is same as
printf("arr = %s\n", arr);
Step 5 : Access full string and modify using
ptr
notation
strcpy(ptr, "Hi");
Note that, this is same as
strcpy(arr, "Hi");
See full program below
#include <stdio.h>
#include <string.h>
int main(void)
{
char arr[10] = "Laptop";
char *ptr;
ptr = arr;
// Change individual character using ptr[] notation
ptr[2] = 'y';
// Print individual character using ptr[] notation
printf("ptr[2] = %c\n", ptr[2] );
// Print full string using ptr notation
printf("ptr = %s\n", ptr);
// Change full string using ptr notation
strcpy(ptr, "Hi");
// Print full string using ptr notation
printf("ptr = %s\n", ptr);
return 0;
}
Output is as below
ptr[2] = y
ptr = Laytop
ptr = Hi
Step 1 : Define a pointer
ptr
char *ptr;
Step 2 : Allocate memory in heap and let
ptr
point to it
ptr = malloc(10 * sizeof(char));
This allocates 10 Bytes of memory in heap
Step 3 : memset and clear the memory before use
memset(ptr, 0, 10 * sizeof(char) );
This is needed because, memory returned by malloc may have garbage contents !
Step 4 : Access individual characters of heap using
ptr[ ]
notation
ptr[2] = 'y';
printf("ptr[2] = %c\n", ptr[2] );
Note that, this is same as
*( ptr + 2 ) = 'y';
printf(" *( ptr + 2 ) = %c\n", *( ptr + 2 ) );
Step 5 : Access full string and print using
ptr
notation
printf("ptr = %s\n", ptr);
Step 6 : Access full string and modify using
ptr
notation
strcpy(ptr, "Hi");
Step 7 : Free memory after use
free(ptr);
See full program below
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *ptr;
ptr = malloc(10 * sizeof(char));
memset(ptr, 0, 10 * sizeof(char) );
// Change individual character using ptr[] notation
ptr[0] = 'l';
ptr[1] = 'a';
ptr[2] = 'p';
ptr[3] = '\0';
// Print individual character using ptr[] notation
printf("ptr[2] = %c\n", ptr[2] );
// Print full string using ptr notation
printf("ptr = %s\n", ptr);
// Change full string using ptr notation
strcpy(ptr, "Hi");
// Print full string using ptr notation
printf("ptr = %s\n", ptr);
free(ptr);
return 0;
}
Output is as below
ptr[2] = p
ptr = lap
ptr = Hi
Step 1 : Define a pointer
char *ptr;
Step 2 : Let
ptr
point to a string
ptr = "Laptop";
Step 3 : Access individual characters and print
for (int i = 0; i < strlen(ptr); i++)
{
printf("%c", ptr[i]);
}
Step 4 : Access full string and print
printf("ptr = %s\n", ptr);
See full program below
#include <stdio.h>
#include <string.h>
int main(void)
{
char *ptr;
ptr = "Laptop";
// Access individual characters and print
for (int i = 0; i < strlen(ptr); i++)
{
printf("%c", ptr[i]);
}
printf("\n");
// Access full string using ptr and print
printf("ptr = %s\n", ptr);
return 0;
}
Output is as below
Laptop
ptr = Laptop
In this case content of string “Laptop” can not be changed
For example,
ptr[2] = 'b';
is INVALID !
Step 1 : Define an array and a pointer
char arr[10] = "Laptop";
char *ptr;
Step 2 : Let Single pointer to point to array
ptr = arr + 2;
OR
ptr = &arr[0] + 2;
With this ptr
is pointing at Byte 3 (with index 2)
Step 3 : Access Bytes in forward direction
Observe that, ptr
can access in total of 5 Bytes 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]
Step 4 : Access Bytes in backward direction
Observe that, ptr
can access in total of 2 Bytes 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)
{
char arr[10] = "Laptop";
char *ptr;
ptr = arr + 2;
printf("------ Printing characters using ptr[] notation ------\n");
printf("ptr[-2] = %c\n", ptr[-2]);
printf("ptr[-1] = %c\n", ptr[-1]);
printf("ptr[0] = %c\n", ptr[0]);
printf("ptr[1] = %c\n", ptr[1]);
printf("ptr[2] = %c\n", ptr[2]);
printf("ptr[3] = %c\n", ptr[3]);
printf("ptr[4] = %c\n", ptr[4]);
printf("------ Printing characters using *ptr notation ------\n");
printf("*(ptr - 2) = %c\n", *(ptr - 2) );
printf("*(ptr - 1) = %c\n", *(ptr - 1) );
printf("*ptr = %c\n", *ptr );
printf("*(ptr + 1) = %c\n", *(ptr + 1) );
printf("*(ptr + 2) = %c\n", *(ptr + 2) );
printf("*(ptr + 3) = %c\n", *(ptr + 3) );
printf("*(ptr + 4) = %c\n", *(ptr + 4) );
return 0;
}
Output is as below
------ Printing characters using ptr[] notation ------
ptr[-2] = L
ptr[-1] = a
ptr[0] = p
ptr[1] = t
ptr[2] = o
ptr[3] = p
ptr[4] =
------ Printing characters using *ptr notation ------
*(ptr - 2) = L
*(ptr - 1) = a
*ptr = p
*(ptr + 1) = t
*(ptr + 2) = o
*(ptr + 3) = p
*(ptr + 4) =
Relative memory access is applicable if pointer is pointing to heap, strings as well !
Rules of Single Pointer Arithmetic |
|
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Step 1 : Define an array
char arr[10] = "Laptop";
Step 2 : Let
ptr
point to arrayarr
char *ptr;
ptr = arr;
Step 3 : Increment
ptr
twice
ptr++;
ptr++;
OR
ptr += 2;
ptr
is now pointing to Byte 2
See full program below
#include <stdio.h>
int main(void)
{
char arr[10] = "Laptop";
char *ptr;
ptr = arr;
ptr++;
ptr++;
// ptr is now at Byte 2
printf("ptr = %s\n", ptr);
return 0;
}
Output is as below
ptr = ptop
From current location of ptr
, %s iterates through all characters till it finds NULL
Step 1 : Define an array
char arr[10] = "Laptop";
Step 2 : Let
ptr
point to arrayarr
char *ptr;
ptr = arr + strlen(arr) - 1;
Step 3 : Decrement
ptr
twice
ptr--;
ptr--;
OR
ptr -= 2;
ptr
is now pointing to Byte 3
See full program below
#include <stdio.h>
int main(void)
{
char arr[10] = "Laptop";
char *ptr;
ptr = arr + strlen(arr) - 1;
ptr--;
ptr--;
// ptr is now at Byte 3
printf("ptr = %s\n", ptr);
return 0;
}
Output is as below
ptr = top
From current location of ptr
, %s iterates through all characters till it finds NULL
Step 1 : Define an array
char arr[10] = "Laptop";
Step 2 : Let
ptr
point to arrayarr
char *ptr;
ptr = arr;
Step 3 : Increment
ptr
twice
ptr = ptr / 2;
This is INVALID
See full program below
#include <stdio.h>
int main(void)
{
char arr[10] = "Laptop";
char *ptr;
ptr = arr;
ptr = ptr / 2;
return 0;
}
Output is as below
p14_char_sp.c: In function main:
p14_char_sp.c:11:12: error: invalid operands to binary / (have char * and int)
11 | ptr = ptr / 2;
|
Note the compilation error !
Step 1 : Define an array
char arr[10] = "Laptop";
Step 2 : Let
ptr
point to arrayarr
char *ptr;
ptr = arr;
Step 3 : Increment
ptr
twice
ptr = ptr * 2;
This is INVALID
See full program below
#include <stdio.h>
int main(void)
{
char arr[10] = "Laptop";
char *ptr;
ptr = arr;
ptr = ptr * 2;
return 0;
}
Output is as below
p14_char_sp.c: In function main:
p14_char_sp.c:11:12: error: invalid operands to binary * (have char * and int)
11 | ptr = ptr * 2;
|
Note the compilation error !
Current Module
Previous Module
Next Module
Other Modules