Pointer to a Single Dimension Array of Characters

In this section, you are going to learn

type ( *ptr ) [ size_of_array ];

An example is as below

char a[10] = "Laptop";

char (*ptr)[10];

ptr = &a;

Pointer to an Array when incremented, increments by size of the array to which it is pointing to !

Pointer to an Array when decremented, decrements by size of the array to which it is pointing to !

Let us see how it is done. See below

  • Step 1 : Define a single dimension array

char a[10] = "Laptop";
  • Step 2 : Define a pointer to an array

char (*ptr)[10];
  • Step 3 : Let pointer to an array, point to single dimension array

ptr = &a;
  • Step 4 : Check pointer arithmetic

printf("Before Increment : ptr = %lx\n", (unsigned long int) ptr);

ptr++;

printf("After Increment : ptr = %lx\n", (unsigned long int) ptr);
  • See full program below

#include <stdio.h>

int main(void)
{
        char a[10] = "Laptop";

        char (*ptr)[10];

        ptr = &a;

        printf("Before Increment : ptr = %lx\n", (unsigned long int) ptr);

        ptr++;

        printf("After Increment : ptr = %lx\n", (unsigned long int) ptr);

        return 0;
}
  • Output is as below

Before Increment : ptr = 7ffde08b01ee
After Increment  : ptr = 7ffde08b01f8

Observe that difference is 10 !

When Pointer to an Array is passed as argument, Prototype of function should match the type properly !

Let us see how it is done. See below

  • Step 1 : Define a single dimension array

char a[7] = "Laptop";
  • Step 2 : Define a pointer to an array

char (*ptr)[7];
  • Step 3 : Let pointer to an array, point to single dimension array

ptr = &a;
  • Step 4 : Pass ptr as argument to function fun

fun(ptr);
  • Step 5 : Define function fun

void fun( char (*p)[] )
{
}
  • See full program below

#include <stdio.h>

void fun( char (*p)[] )
{
}

int main(void)
{
        char a[7] = "Laptop";

        char (*ptr)[7];

        ptr = &a;

        fun(ptr);

        return 0;
}

When Pointer to an Array is passed as argument by call by reference, Prototype of function should match the type properly !

Let us see how it is done. See below

  • Step 1 : Define a Double dimension array

char a[3][10] = { "Laptop", "Keyboard", "Mouse" };
  • Step 2 : Define a pointer to an array

char (*ptr)[10];
  • Step 3 : Let pointer to an array, point to single dimension array

ptr = &a[0];
  • Step 4 : Pass ptr as argument to function fun. Call by reference

fun(&ptr);
  • Step 5 : Define function fun

void fun(char (**p)[10])
{
        for (int i = 0; i < 3; i++) {
                printf("%s\n", (*p)[i]);
        }
}

This is easy to understand ! Let us derive the rules

  • Rule 1 : Base rule in main

ptr = &a[0];
  • Rule 2 : Base rule in fun

p = &ptr
  • Rule 2 : Move & from RHS to LHS. This becomes * on LHS

*p = ptr
  • Rule 3 : Replace ptr referring to Rule 1

*p = &a[0]
  • Rule 4 : Move & from RHS to LHS. This becomes * on LHS

**p = a[0]
  • Rule 5 : * and [ ] can be used interchangeably

(*p)[0] = a[0]
  • Rule 6 : Extending Rule 5

(*p)[i] = a[i]
  • See full program below

#include <stdio.h>

void fun(char (**p)[10])
{
        for (int i = 0; i < 3; i++) {
                printf("%s\n", (*p)[i]);
        }
}

int main(void)
{
        char a[3][10] = { "Laptop", "Keyboard", "Mouse" };

        char (*ptr)[10];

        ptr = &a[0];

        fun(&ptr);

        return 0;
}
  • Output is as below

Laptop
Keyboard
Mouse

We can use pointer to an array directly inorder to print the contents of array to which it is pointing to !

Let us see how it is done. See below

  • Step 1 : Define a single dimension array

char a[7] = "Laptop";
  • Step 2 : Define a pointer to an array

char (*ptr)[7];
  • Step 3 : Let pointer to an array, point to single dimension array

ptr = &a;

We can derive other rules from this base rule !

  • Rule 1 : Base rule

ptr = &a;
  • Rule 2 : Move & from RHS to LHS. This becomes * on LHS

*ptr = a
  • Rule 3 : a is also equals to &a[0]

*ptr = &a[0]
  • Rule 4 : *ptr is equal to ptr[0]

ptr[0] = &a[0]
  • Rule 5 : Move & from RHS to LHS. This becomes * on LHS

(*ptr)[0] = a[0]
  • Rule 6 : (*ptr)[0] is equal to ptr[0][0]

ptr[0][0] = a[0]
  • Rule 7 : Extending Rule 5

(*ptr)[i] = a[i]
  • Step 4 : Access individual characters using (*ptr)[i]

for (int i = 0; i < 7; i++) {
        printf("%c", (*ptr)[i]);
}
  • Step 5 : Access full string using *ptr to print using %s

printf("str = %s\n", *ptr);
  • See full program below

#include <stdio.h>

int main(void)
{
        char a[7] = "Laptop";

        char (*ptr)[7];

        ptr = &a;

        // Print individual characters
        for (int i = 0; i < 7; i++) {
                printf("%c", (*ptr)[i]);
        }

        printf("\n");

        // Print string at once
        printf("str = %s\n", *ptr);

        return 0;
}
  • Output is as below

Laptop
str = Laptop

We can use pointer to an array directly inorder to print the contents of array to which it is pointing to !

Let us see how it is done. See below

  • Step 1 : Define a single dimension array

char a[7] = "Laptop";
  • Step 2 : Pass address of array a as an argument to function fun

fun(&a, sizeof(a));
  • Step 3 : Define function fun

void fun(char (*p)[], int size)
{
}

Note the function definition !

  • Since first argument is passed as address of array a, prototype must have pointer to an array in first parameter

  • Step 4 : Let us write the code to access individual characters of single dimension array inside function fun

void fun(char (*p)[7], int size)
{
        for (int i = 0; i < size; i++) {
                printf("%c\n", (*p)[i]);
        }

        for (int i = 0; i < size; i++) {
                printf("%c\n", p[0][i]);
        }
}

This is easy to understand ! Let us derive the rules

  • Rule 1 : Base rule

p = &a

Where,

  • LHS p is a variable on stack of function fun fun

  • RHS &a is actual argument passed to function fun

  • Rule 2 : Move & from RHS to LHS. This becomes * on LHS

*p = a
  • Rule 3 : a is also equals to &a[0]

*p = &a[0]
  • Rule 4 : Move & from RHS to LHS. This becomes * on LHS

**p = a[0]
  • Rule 5 : * and [ ] can be used interchangeably

(*p)[0] = a[0]
  • Rule 6 : * and [ ] can be used interchangeably

p[0][0] = a[0]
  • Rule 7 : Extending Rule 5

(*p)[i] = a[i]
  • Rule 8 : Extending Rule 5

p[0][i] = a[i]
  • Step 5 : Let us write the code to access full string at once inside function fun

void fun(char (*p)[], int size)
{
        char *sp;

        sp = *p;

        printf("sp = %s\n", sp);
        printf("*p = %s\n", *p);
}

This is easy to understand ! Let us derive the rules

  • Rule 1 : Base rule

p = &a

Where,

  • LHS p is a variable on stack of function fun fun

  • RHS &a is actual argument passed to function fun

  • Rule 2 : Move & from RHS to LHS. This becomes * on LHS

*p = a
  • Rule 3 : a is also equals to &a[0]

*p = &a[0]
  • Rule 4 : From Rule 3, it is clear that *p is a pointer to an integer. Hence we can assign *p to char *sp

sp = *p

We can now use sp, *p in printf with %s !

  • See full program below

#include <stdio.h>

void fun(char (*p)[], int size)
{
        char *sp;

        sp = *p;

        for (int i = 0; i < size; i++) {
                printf("%c\n", sp[i]);
        }

        for (int i = 0; i < size; i++) {
                printf("%c\n", (*p)[i]);
        }

        for (int i = 0; i < size; i++) {
                printf("%c\n", p[0][i]);
        }

        printf("sp = %s\n", sp);
        printf("*p = %s\n", *p);
}

int main(void)
{
        char a[7] = "Laptop";

        fun(&a, sizeof(a));

        return 0;
}
  • Step 1 : Define a double dimension array of characters

char a[3][10] = { "Laptop", "Keyboard", "Mouse" };
  • Step 2 : Define a pointer to an array of single dimension

char (*ptr)[10];
  • Step 3 : Let the pointer to point to Single dimension array inside a Double dimension array

ptr = &a[0];
  • Step 4 : Print the strings using %s : Method 1

for (int i = 0; i < 3; i++) {
        printf("%s\n", ptr[i]);
}
  • Step 5 : Print the strings using %s : Method 2

for (int i = 0; i < 3; i++) {
        printf("%s\n", (char *)ptr);
        ptr++;
}

This is easy to understand ! Let us derive the rules

  • Rule 1 : Base rule

ptr = &a[0];
  • Rule 2 : Move & from RHS to LHS. This becomes * on LHS

*ptr = a[0];
  • Rule 3 : * and [ ] can be used interchangeably

ptr[0] = a[0];
  • Rule 4 : Extending Rule 3

ptr[i] = a[i];
  • See full program below

#include <stdio.h>

int main(void)
{
        char a[3][10] = { "Laptop", "Keyboard", "Mouse" };

        char (*ptr)[10];

        ptr = &a[0];

        //Method 1 : print string
        for (int i = 0; i < 3; i++) {
                printf("%s\n", ptr[i]);
        }

        //Method 2 : print string
        for (int i = 0; i < 3; i++) {
                printf("%s\n", (char *)ptr);
                ptr++;
        }

        return 0;
}
  • Step 1 : Allocate a block of memory in Heap

char *a;

a = malloc(30 * sizeof(char));
  • Step 2 : Assign User Data in Heap memory

memset(a, 0, 30);
memcpy(a, "Laptop", 10);
memcpy(a + 10, "Keyboard", 10);
memcpy(a + 20, "Mouse", 10);
  • Step 3 : Let ptr point to block of memory in Heap

char (*ptr)[10];

ptr = (char (*)[10]) a;

Note that typecasting done !

Key is to remember that, ptr should point to block of continuous memory for it to access User data properly !

  • Step 4 : Method 1 : Access the memory block in Heap using ptr

for (int i = 0; i < 3; i++) {
        printf("%s\n", ptr[i]);
}
  • Step 5 : Method 2 : Access the memory block in Heap using ptr

for (int i = 0; i < 3; i++) {
        printf("%s\n", (char *)ptr);
        ptr++;
}

Remember after Step 5, ptr is NOT pointing to start of Heap !

  • Step 6 : Free Heap memory after use

free(a);
  • See full program below

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
        char *a;

        char (*ptr)[10];

        a = malloc(30 * sizeof(char));

        memset(a, 0, 30);
        memcpy(a, "Laptop", 10);
        memcpy(a + 10, "Keyboard", 10);
        memcpy(a + 20, "Mouse", 10);

        ptr = (char (*)[10])a;

        //Method 1 : print string
        for (int i = 0; i < 3; i++) {
                printf("%s\n", ptr[i]);
        }

        //Method 2 : print string
        for (int i = 0; i < 3; i++) {
                printf("%s\n", (char *)ptr);
                ptr++;
        }

        free(a);

        return 0;
}