post increment char single pointer

In this section, you are going to learn

How to iterate character strings using ptr++ ?

What is the meaning of ptr++, *ptr++, *(ptr++), *(ptr)++, (*ptr)++ ?

What is the meaning of c = ptr++, c = *ptr++, c = *(ptr++), c = *(ptr)++, c = (*ptr)++ ?

What is the difference between ptr++, *ptr++, *(ptr++), *(ptr)++, (*ptr)++ ?

What is the difference between c = ptr++, c = *ptr++, c = *(ptr++), c = *(ptr)++, c = (*ptr)++ ?

  • Step 1 : Define a Single dimension array

char a[7] = "Laptop";
  • Step 2 : Point ptr to Single dimension array

char *ptr;

ptr = a;
  • Step 3 : Increment ptr

ptr++;
  • Step 4 : Print *ptr

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

#include <stdio.h>

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

        char *ptr;

        ptr = a;

        ptr++;

        printf("*ptr = %c\n", *ptr);

        return 0;
}
  • Step 1 : Define a Single Dimension array

char a[7] = "Laptop";
  • Step 2 : Point ptr to Single dimension array

char *ptr;

ptr = a;
  • Step 3 : Print string by iterating through all characters using ptr++

for (int i = 0; i < strlen(a); i++)
{
        printf("*ptr = %c\n", *ptr);
        ptr++;
}
  • See full program below

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

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

        char *ptr;

        ptr = a;

        for (int i = 0; i < strlen(a); i++)
        {
                printf("*ptr = %c\n", *ptr);
                ptr++;
        }

        return 0;
}
  • Step 1 : Define a Single Dimension array

char a[7] = "Laptop";
  • Step 2 : Point ptr to Single dimension array

char *ptr;

ptr = a;
  • Step 3 : Print string by iterating through all characters using ptr++

while (*ptr)
{
        printf("*ptr = %c\n", *ptr);
        ptr++;
}
  • See full program below

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

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

        char *ptr;

        ptr = a;

        while (*ptr)
        {
                printf("*ptr = %c\n", *ptr);
                ptr++;
        }

        return 0;
}
  • Step 1 : Define a Single Dimension array

char a[7] = "Laptop";
  • Step 2 : Point ptr to Single dimension array

char *ptr;

ptr = a;
  • Step 3 : Print string by iterating through all characters using ptr++

while (*ptr)
{
        char c;

        c = *ptr++;

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

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

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

        char *ptr;

        ptr = a;

        while (*ptr)
        {
                char c;

                c = *ptr++;

                printf("*ptr = %c\n", c);
        }

        return 0;
}
  • Step 1 : Define a Single Dimension array

char a[7] = "Laptop";
  • Step 2 : Point ptr to Single dimension array

char *ptr;

ptr = a;
  • Step 3 : Print string by iterating through all characters using ptr++

while (*ptr)
{
        printf("*ptr = %c\n", *ptr++);
}
  • See full program below

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

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

        char *ptr;

        ptr = a;

        while (*ptr)
        {
                printf("*ptr = %c\n", *ptr++);
        }

        return 0;
}
  • Consider statement

p = ptr++;
  • There are two steps in this statement

    • Current value of ptr is assigned to p

    • ptr is incremented

  • We now derived a rule

    • First Assign, then increment

  • Step 1 : Define a single dimension array

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

char *ptr;

ptr = a;

OR

char *ptr;

ptr = &a[0];
  • Step 3 : Iterate and print the string

while (*ptr)
{
        char *p;

        p = ptr++;

        printf("*ptr = %c\n", *ptr);
        printf("*p = %c\n", *p);
}

Output is as below

*ptr = a
*p = L
*ptr = p
*p = a
*ptr = t
*p = p
*ptr = o
*p = t
*ptr = p
*p = o
*ptr =
*p = p

Can you guess what is happening ?

  • Let us Recall

p = ptr++;
  • There are two steps in this statement

    • Current value of ptr is assigned to p

    • ptr is incremented

  • We now derived a rule

    • First Assign, then increment

  • See full program below

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

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

        char *ptr;

        ptr = a;

        while (*ptr)
        {
                char *p;

                p = ptr++;
                printf("*ptr = %c\n", *ptr);
                printf("*p = %c\n", *p);
        }

        return 0;
}
  • Consider statement

c = *(ptr++);
  • There are two steps in this statement

    • Current value of *ptr is assigned to c

    • ptr is incremented

    • *ptr is NOT incremented

  • We now derived a rule

    • First Assign, then increment

  • Step 1 : Define a single dimension array

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

char *ptr;

ptr = a;

OR

char *ptr;

ptr = &a[0];
  • Step 3 : Iterate and print the string

while (*ptr)
{
        char c;

        c = *(ptr++);

        printf("*ptr = %c\n", *ptr);
        printf("c = %c\n", c);
}

Output is as below

*ptr = a
c = L
*ptr = p
c = a
*ptr = t
c = p
*ptr = o
c = t
*ptr = p
c = o
*ptr =
c = p

Can you guess what is happening ?

  • Let us Recall

c = *(ptr++);
  • There are two steps in this statement

    • Current value of *ptr is assigned to c

    • ptr is incremented

    • *ptr is NOT incremented

  • We now derived a rule

    • First Assign, then increment

  • See full program below

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

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

        char *ptr;

        ptr = a;

        while (*ptr)
        {
                char c;

                c = *(ptr++);
                printf("*ptr = %c\n", *ptr);
                printf("c = %c\n", c);
        }

        return 0;
}
  • Consider statement

c = *ptr++;
  • There are two steps in this statement

    • Current value of *ptr is assigned to c

    • ptr is incremented

    • *ptr is NOT incremented

  • We now derived a rule

    • First Assign, then increment

  • Step 1 : Define a single dimension array

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

char *ptr;

ptr = a;

OR

char *ptr;

ptr = &a[0];
  • Step 3 : Iterate and print the string

while (*ptr)
{
        char c;

        c = *ptr++;

        printf("*ptr = %c\n", *ptr);
        printf("c = %c\n", c);
}

Output is as below

*ptr = a
c = L
*ptr = p
c = a
*ptr = t
c = p
*ptr = o
c = t
*ptr = p
c = o
*ptr =
c = p

Can you guess what is happening ?

  • Let us Recall

c = *ptr++;
  • There are two steps in this statement

    • Current value of *ptr is assigned to c

    • ptr is incremented

    • *ptr is NOT incremented

  • We now derived a rule

    • First Assign, then increment

  • See full program below

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

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

        char *ptr;

        ptr = a;

        while (*ptr)
        {
                char c;

                c = *ptr++;
                printf("*ptr = %c\n", *ptr);
                printf("c = %c\n", c);
        }

        return 0;
}
  • Consider statement

c = *(ptr)++;
  • There are two steps in this statement

    • Current value of *ptr is assigned to c

    • ptr is incremented

    • *ptr is NOT incremented

  • We now derived a rule

    • First Assign, then increment

  • Step 1 : Define a single dimension array

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

char *ptr;

ptr = a;

OR

char *ptr;

ptr = &a[0];
  • Step 3 : Iterate and print the string

while (*ptr)
{
        char c;

        c = *(ptr)++;

        printf("*ptr = %c\n", *ptr);
        printf("c = %c\n", c);
}

Output is as below

*ptr = a
c = L
*ptr = p
c = a
*ptr = t
c = p
*ptr = o
c = t
*ptr = p
c = o
*ptr =
c = p

Can you guess what is happening ?

  • Let us Recall

c = *(ptr)++;
  • There are two steps in this statement

    • Current value of *ptr is assigned to c

    • ptr is incremented

    • *ptr is NOT incremented

  • We now derived a rule

    • First Assign, then increment

  • See full program below

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

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

        char *ptr;

        ptr = a;

        while (*ptr)
        {
                char c;

                c = *(ptr)++;
                printf("*ptr = %c\n", *ptr);
                printf("c = %c\n", c);
        }

        return 0;
}
  • Consider statement

c = (*ptr)++;
  • There are two steps in this statement

    • Current value of *ptr is assigned to c

    • ptr is NOT incremented

    • *ptr is incremented

  • We now derived a rule

    • First Assign, then increment

Summary of expressions

Expression

Explanation

c = ptr++

  • Assign ptr to c

  • Increment ptr

c = *(ptr++)

  • Assign *ptr to c

  • Increment ptr

  • DO NOT increment *ptr

c = *ptr++

  • Assign *ptr to c

  • Increment ptr

  • DO NOT increment *ptr

c = *(ptr)++

  • Assign *ptr to c

  • Increment ptr

  • DO NOT increment *ptr

c = (*ptr)++

  • Assign *ptr to c

  • DO NOT increment ptr

  • Increment *ptr

  • Step 1 : Define a single dimension array

char a[7] = "Laptop";
  • Step 2 : Create a structure object

struct ABC abc;
  • Step 3 : Point single character pointer to single dimension array

abc.ptr = a;
  • Step 4 : Iterate through single dimension array using pointer

while (*abc.ptr)
{
        char c;

        c = *abc.ptr++;
        printf("*ptr = %c\n", *abc.ptr);
        printf("c = %c\n", c);
}
  • See full program below

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

struct ABC
{
        char *ptr;
};

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

        struct ABC abc;

        abc.ptr = a;

        while (*abc.ptr)
        {
                char c;

                c = *abc.ptr++;
                printf("*ptr = %c\n", *abc.ptr);
                printf("c = %c\n", c);
        }

        return 0;
}
  • Step 1 : Define a single dimension array

char a[7] = "Laptop";
  • Step 2 : Allocate memory for structure pointers

struct ABC *sp;

sp = malloc(sizeof(struct ABC));
  • Step 3 : Point single character pointer to single dimension array

sp->ptr = a;
  • Step 4 : Iterate through single dimension array using pointer

while (*sp->ptr)
{
        char c;

        c = *sp->ptr++;
        printf("*ptr = %c\n", *sp->ptr);
        printf("c = %c\n", c);
}
  • Step 5 : Free memory after use

free(sp);
  • See full program below

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

struct ABC
{
        char *ptr;
};

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

        struct ABC *sp;

        sp = malloc(sizeof(struct ABC));

        sp->ptr = a;

        while (*sp->ptr)
        {
                char c;

                c = *sp->ptr++;
                printf("*ptr = %c\n", *sp->ptr);
                printf("c = %c\n", c);
        }

        free(sp);

        return 0;
}
  • Step 1 : Define a single dimension array

char a[7] = "Laptop";
  • Step 2 : Allocate memory for structure pointers

struct ABC **dp;

dp = malloc(sizeof(struct ABC *));
*dp = malloc(sizeof(struct ABC ));
  • Step 3 : Point single character pointer to single dimension array

(*dp)->ptr = a;
  • Step 4 : Iterate through single dimension array using pointer

while (*(*dp)->ptr)
{
        char c;

        c = *(*dp)->ptr++;
        printf("*ptr = %c\n", *(*dp)->ptr);
        printf("c = %c\n", c);
}
  • Step 5 : Free memory after use

free(*dp);
free(dp);
  • See full program below

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

struct ABC
{
        char *ptr;
};

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

        struct ABC **dp;

        dp = malloc(sizeof(struct ABC *));
        *dp = malloc(sizeof(struct ABC ));

        (*dp)->ptr = a;

        while (*(*dp)->ptr)
        {
                char c;

                c = *(*dp)->ptr++;
                printf("*ptr = %c\n", *(*dp)->ptr);
                printf("c = %c\n", c);
        }

        free(*dp);
        free(dp);

        return 0;
}
  • Step 1 : Define a single dimension array

char a[7] = "Laptop";
  • Step 2 : Allocate memory for structure pointers

struct ABC ***dp;

dp = malloc(sizeof(struct ABC **));
*dp = malloc(sizeof(struct ABC *));
**dp = malloc(sizeof(struct ABC ));
  • Step 3 : Point single character pointer to single dimension array

(**dp)->ptr = a;
  • Step 4 : Iterate through single dimension array using pointer

while (*(**dp)->ptr)
{
        char c;

        c = *(**dp)->ptr++;
        printf("*ptr = %c\n", *(**dp)->ptr);
        printf("c = %c\n", c);
}
  • Step 5 : Free memory after use

free(**dp);
free(*dp);
free(dp);
  • See full program below

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

struct ABC
{
        char *ptr;
};

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

        struct ABC ***dp;

        dp = malloc(sizeof(struct ABC **));
        *dp = malloc(sizeof(struct ABC *));
        **dp = malloc(sizeof(struct ABC ));

        (**dp)->ptr = a;

        while (*(**dp)->ptr)
        {
                char c;

                c = *(**dp)->ptr++;
                printf("*ptr = %c\n", *(**dp)->ptr);
                printf("c = %c\n", c);
        }

        free(**dp);
        free(*dp);
        free(dp);

        return 0;
}
  • Step 1 : Define a single dimension array

char a[7] = "Laptop";
  • Step 2 : Create a structure object

struct ABC abc;
  • Step 3 : Point single character pointer to single dimension array

abc.pqr.ptr = a;
  • Step 4 : Iterate through single dimension array using pointer

while (*abc.pqr.ptr)
{
        char c;

        c = *abc.pqr.ptr++;
        printf("*ptr = %c\n", *abc.pqr.ptr);
        printf("c = %c\n", c);
}
  • See full program below

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

struct PQR
{
        char *ptr;
};

struct ABC
{
        struct PQR pqr;
};

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

        struct ABC abc;

        abc.pqr.ptr = a;

        while (*abc.pqr.ptr)
        {
                char c;

                c = *abc.pqr.ptr++;
                printf("*ptr = %c\n", *abc.pqr.ptr);
                printf("c = %c\n", c);
        }

        return 0;
}
  • Step 1 : Define a single dimension array

char a[7] = "Laptop";
  • Step 2 : Allocate memory for structure pointers

struct ABC *sp;

sp = malloc(sizeof(struct ABC));
  • Step 3 : Point single character pointer to single dimension array

sp->pqr.ptr = a;
  • Step 4 : Iterate through single dimension array using pointer

while (*sp->pqr.ptr)
{
        char c;

        c = *sp->pqr.ptr++;
        printf("*ptr = %c\n", *sp->pqr.ptr);
        printf("c = %c\n", c);
}
  • Step 5 : Free memory after use

free(sp);
  • See full program below

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

struct PQR
{
        char *ptr;
};

struct ABC
{
        struct PQR pqr;
};

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

        struct ABC *sp;

        sp = malloc(sizeof(struct ABC));

        sp->pqr.ptr = a;

        while (*sp->pqr.ptr)
        {
                char c;

                c = *sp->pqr.ptr++;
                printf("*ptr = %c\n", *sp->pqr.ptr);
                printf("c = %c\n", c);
        }

        free(sp);

        return 0;
}
  • Step 1 : Define a single dimension array

char a[7] = "Laptop";
  • Step 2 : Allocate memory for structure pointers

struct ABC *abc;

abc = malloc(sizeof(struct ABC));
abc->pqr = malloc(sizeof(struct PQR));
  • Step 3 : Point single character pointer to single dimension array

abc->pqr->ptr = a;
  • Step 4 : Iterate through single dimension array using pointer

while (*abc->pqr->ptr)
{
        char c;

        c = *abc->pqr->ptr++;
        printf("*ptr = %c\n", *abc->pqr->ptr);
        printf("c = %c\n", c);
}
  • Step 5 : Free memory after use

free(abc->pqr);
free(abc);
  • See full program below

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

struct PQR
{
        char *ptr;
};

struct ABC
{
        struct PQR *pqr;
};

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

        struct ABC *abc;

        abc = malloc(sizeof(struct ABC));
        abc->pqr = malloc(sizeof(struct PQR));

        abc->pqr->ptr = a;

        while (*abc->pqr->ptr)
        {
                char c;

                c = *abc->pqr->ptr++;
                printf("*ptr = %c\n", *abc->pqr->ptr);
                printf("c = %c\n", c);
        }

        free(abc->pqr);
        free(abc);

        return 0;
}

Post Increment : Function Call

  • Step 1 : Define a single dimension array

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

char *ptr;

ptr = a;
  • Step 3 : Iterate array using ptr++ and Pass by Value

while (*ptr)
{
        fun(*ptr++);
}
  • Step 4 : Define a function fun which receives a character from caller

void fun(char c)
{
        printf("c = %c\n", c);
}

Let us Recall,

  • In case of *ptr++

    • Current value of *ptr is first assigned, then ptr incremented

  • Hence, in this case

    • Current value of *ptr is first passed to function fun, then ptr incremented

  • See full program below

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

void fun(char c)
{
        printf("c = %c\n", c);
}

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

        char *ptr;

        ptr = a;

        while (*ptr)
        {
                fun(*ptr++);
        }

        return 0;
}
  • Step 1 : Define a single dimension array

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

char *ptr;

ptr = a;
  • Step 3 : Iterate array using ptr++ and Pass by Value

while (*ptr)
{
        fun(ptr++);
}
  • Step 4 : Define a function fun which receives a character pointer from caller

void fun(char *c)
{
        printf("*c = %c\n", *c);
}

Let us Recall,

  • In case of ptr++

    • Current value of ptr is first assigned, then incremented

  • Hence, in this case

    • Current value of ptr is first passed to function fun, then incremented

  • See full program below

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

void fun(char *c)
{
        printf("*c = %c\n", *c);
}

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

        char *ptr;

        ptr = a;

        while (*ptr)
        {
                fun(ptr++);
        }

        return 0;
}