post decrement int single pointer

In this section, you are going to learn

How to iterate Integer Arrays 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

int a[7] = { 0, 1, 2, 3, 4, 5, 6 };
  • Step 2 : Point ptr to Single dimension array at last printable integer

int *ptr;

ptr = a + sizeof(a)/sizeof(a[0]) - 1;
  • Step 3 : Decrement ptr

ptr--;
  • Step 4 : Print *ptr

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

#include <stdio.h>

int main(void)
{
        int a[7] = { 0, 1, 2, 3, 4, 5, 6 };

        int *ptr;

        ptr = a + sizeof(a)/sizeof(a[0]) - 1;

        ptr--;

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

        return 0;
}
  • Output is as below

*ptr = 5
  • Step 1 : Define a Single Dimension array

int a[7] = { 0, 1, 2, 3, 4, 5, 6 };
  • Step 2 : Point ptr to Single dimension array

int *ptr;

ptr = a + sizeof(a)/sizeof(a[0]) - 1;
  • Step 3 : Print Integer Array by iterating through all integers using ptr--

for (int i = 0; i < sizeof(a)/sizeof(a[0]); i++)
{
        printf("*ptr= %d\n", *ptr);
        ptr--;
}
  • See full program below

#include <stdio.h>

int main(void)
{
        int a[7] = { 0, 1, 2, 3, 4, 5, 6 };

        int *ptr;

        ptr = a + sizeof(a)/sizeof(a[0]) - 1;

        for (int i = 0; i < sizeof(a)/sizeof(a[0]); i++)
        {
                printf("*ptr= %d\n", *ptr);
                ptr--;
        }

        return 0;
}
  • Output is as below

*ptr = 6
*ptr = 5
*ptr = 4
*ptr = 3
*ptr = 2
*ptr = 1
*ptr = 0
  • Step 1 : Define a Single Dimension array

int a[7] = { 0, 1, 2, 3, 4, 5, 6 };
  • Step 2 : Point ptr to Single dimension array

int *ptr;

ptr = a + sizeof(a)/sizeof(a[0]) - 1;
  • Step 3 : Print Integer Array by iterating through all integers using ptr--

while (ptr >= a)
{
        printf("*ptr= %d\n", *ptr);
        ptr--;
}
  • See full program below

#include <stdio.h>

int main(void)
{
        int a[7] = { 0, 1, 2, 3, 4, 5, 6 };

        int *ptr;

        ptr = a + sizeof(a)/sizeof(a[0]) - 1;

        while (ptr >= a)
        {
                printf("*ptr= %d\n", *ptr);
                ptr--;
        }

        return 0;
}
  • Output is as below

*ptr = 6
*ptr = 5
*ptr = 4
*ptr = 3
*ptr = 2
*ptr = 1
*ptr = 0
  • Step 1 : Define a Single Dimension array

int a[7] = { 0, 1, 2, 3, 4, 5, 6 };
  • Step 2 : Point ptr to Single dimension array

int *ptr;

ptr = a + sizeof(a)/sizeof(a[0]) - 1;
  • Step 3 : Print Integer Array by iterating through all integers using ptr--

while (ptr >= a)
{
        int c;

        c = *ptr--;

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

#include <stdio.h>

int main(void)
{
        int a[7] = { 0, 1, 2, 3, 4, 5, 6 };

        int *ptr;

        ptr = a + sizeof(a)/sizeof(a[0]) - 1;

        while (ptr >= a)
        {
                int c;

                c = *ptr--;

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

        return 0;
}
  • Output is as below

*ptr = 6
*ptr = 5
*ptr = 4
*ptr = 3
*ptr = 2
*ptr = 1
*ptr = 0
  • Step 1 : Define a Single Dimension array

int a[7] = { 0, 1, 2, 3, 4, 5, 6 };
  • Step 2 : Point ptr to Single dimension array

int *ptr;

ptr = a + sizeof(a)/sizeof(a[0]) - 1;
  • Step 3 : Print Integer Array by iterating through all integers using ptr--

while (ptr >= a)
{
        printf("*ptr= %d\n", *ptr--);
}
  • See full program below

#include <stdio.h>

int main(void)
{
        int a[7] = { 0, 1, 2, 3, 4, 5, 6 };

        int *ptr;

        ptr = a + sizeof(a)/sizeof(a[0]) - 1;

        while (ptr >= a)
        {
                printf("*ptr= %d\n", *ptr--);
        }

        return 0;
}
  • Output is as below

*ptr = 6
*ptr = 5
*ptr = 4
*ptr = 3
*ptr = 2
*ptr = 1
*ptr = 0
  • Consider statement

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

    • Current value of ptr is assigned to p

    • ptr is decremented

  • We now derived a rule

    • First Assign, then decrement

  • Step 1 : Define a single dimension array

int a[7] = { 0, 1, 2, 3, 4, 5, 6 };
  • Step 2 : Define a single pointer and point to array

int *ptr;

ptr = a + sizeof(a)/sizeof(a[0]) - 1;

OR

int *ptr;

ptr = &a[0] + sizeof(a)/sizeof(a[0]) - 1;
  • Step 3 : Iterate and print the Integer Array

while (ptr >= a)
{
        int *p;

        p = ptr--;
        printf("*p= %d\n", *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 decremented

  • We now derived a rule

    • First Assign, then decrement

  • See full program below

#include <stdio.h>

int main(void)
{
        int a[7] = { 0, 1, 2, 3, 4, 5, 6 };

        int *ptr;

        ptr = a + sizeof(a)/sizeof(a[0]) - 1;

        while (ptr >= a)
        {
                int *p;

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

        return 0;
}
  • Output is as below

*p = 6
*p = 5
*p = 4
*p = 3
*p = 2
*p = 1
*p = 0
  • Consider statement

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

    • Current value of *ptr is assigned to c

    • ptr is decremented

    • *ptr is NOT decremented

  • We now derived a rule

    • First Assign, then decrement

  • Step 1 : Define a single dimension array

int a[7] = { 0, 1, 2, 3, 4, 5, 6 };
  • Step 2 : Define a single pointer and point to array

int *ptr;

ptr = a + sizeof(a)/sizeof(a[0]) - 1;

OR

int *ptr;

ptr = &a[0] + sizeof(a)/sizeof(a[0]) - 1;
  • Step 3 : Iterate and print the Integer Array

while (ptr >= a)
{
        int c;

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

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 decremented

    • *ptr is NOT decremented

  • We now derived a rule

    • First Assign, then decrement

  • See full program below

#include <stdio.h>

int main(void)
{
        int a[7] = { 0, 1, 2, 3, 4, 5, 6 };

        int *ptr;

        ptr = a + sizeof(a)/sizeof(a[0]) - 1;

        while (ptr >= a)
        {
                int c;

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

        return 0;
}
  • Output is as below

c = 6
c = 5
c = 4
c = 3
c = 2
c = 1
c = 0
  • Consider statement

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

    • Current value of *ptr is assigned to c

    • ptr is decremented

    • *ptr is NOT decremented

  • We now derived a rule

    • First Assign, then decrement

  • Step 1 : Define a single dimension array

int a[7] = { 0, 1, 2, 3, 4, 5, 6 };
  • Step 2 : Define a single pointer and point to array

int *ptr;

ptr = a + sizeof(a)/sizeof(a[0]) - 1;

OR

int *ptr;

ptr = &a[0] + sizeof(a)/sizeof(a[0]) - 1;
  • Step 3 : Iterate and print the Integer Array

while (ptr >= a)
{
        int c;

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

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 decremented

    • *ptr is NOT decremented

  • We now derived a rule

    • First Assign, then decrement

  • See full program below

#include <stdio.h>

int main(void)
{
        int a[7] = { 0, 1, 2, 3, 4, 5, 6 };

        int *ptr;

        ptr = a + sizeof(a)/sizeof(a[0]) - 1;

        while (ptr >= a)
        {
                int c;

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

        return 0;
}
  • Output is as below

c = 6
c = 5
c = 4
c = 3
c = 2
c = 1
c = 0
  • Consider statement

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

    • Current value of *ptr is assigned to c

    • ptr is decremented

    • *ptr is NOT decremented

  • We now derived a rule

    • First Assign, then decrement

  • Step 1 : Define a single dimension array

int a[7] = { 0, 1, 2, 3, 4, 5, 6 };
  • Step 2 : Define a single pointer and point to array

int *ptr;

ptr = a + sizeof(a)/sizeof(a[0]) - 1;

OR

int *ptr;

ptr = &a[0] + sizeof(a)/sizeof(a[0]) - 1;
  • Step 3 : Iterate and print the Integer Array

while (ptr >= a)
{
        int c;

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

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 decremented

    • *ptr is NOT decremented

  • We now derived a rule

    • First Assign, then decrement

  • See full program below

#include <stdio.h>

int main(void)
{
        int a[7] = { 0, 1, 2, 3, 4, 5, 6 };

        int *ptr;

        ptr = a + sizeof(a)/sizeof(a[0]) - 1;

        while (ptr >= a)
        {
                int c;

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

        return 0;
}
  • Output is as below

c = 6
c = 5
c = 4
c = 3
c = 2
c = 1
c = 0
  • Consider statement

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

    • Current value of *ptr is assigned to c

    • ptr is NOT decremented

    • *ptr is decremented

  • We now derived a rule

    • First Assign, then decrement

Summary of expressions

Expression

Explanation

c = ptr–

  • Assign ptr to c

  • Decrement ptr

c = *(ptr–)

  • Assign *ptr to c

  • Decrement ptr

  • DO NOT decrement *ptr

c = *ptr–

  • Assign *ptr to c

  • Decrement ptr

  • DO NOT decrement *ptr

c = *(ptr)–

  • Assign *ptr to c

  • Decrement ptr

  • DO NOT decrement *ptr

c = (*ptr)–

  • Assign *ptr to c

  • DO NOT decrement ptr

  • Decrement *ptr

  • Step 1 : Define a single dimension array

int a[7] = { 0, 1, 2, 3, 4, 5, 6 };
  • Step 2 : Create a structure object

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

abc.ptr =  a + sizeof(a)/sizeof(a[0]) - 1;;
  • Step 4 : Iterate through single dimension array using pointer

while (abc.ptr >= a)
{
        int c;

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

#include <stdio.h>

struct ABC
{
        int *ptr;
};

int main(void)
{
        int a[7] = { 0, 1, 2, 3, 4, 5, 6 };

        struct ABC abc;

        abc.ptr =  a + sizeof(a)/sizeof(a[0]) - 1;;

        while (abc.ptr >= a)
        {
                int c;

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

        return 0;
}
  • Output is as below

c = 6
c = 5
c = 4
c = 3
c = 2
c = 1
c = 0
  • Step 1 : Define a single dimension array

int a[7] = { 0, 1, 2, 3, 4, 5, 6 };
  • Step 2 : Allocate memory for structure pointers

struct ABC *sp;

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

sp->ptr =  a + sizeof(a)/sizeof(a[0]) - 1;;
  • Step 4 : Iterate through single dimension array using pointer

while (sp->ptr >= a)
{
        int c;

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

free(sp);
  • See full program below

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

struct ABC
{
        int *ptr;
};

int main(void)
{
        int a[7] = { 0, 1, 2, 3, 4, 5, 6 };

        struct ABC *sp;

        sp = malloc(sizeof(struct ABC));

        sp->ptr =  a + sizeof(a)/sizeof(a[0]) - 1;;

        while (sp->ptr >= a)
        {
                int c;

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

        free(sp);

        return 0;
}
  • Output is as below

c = 6
c = 5
c = 4
c = 3
c = 2
c = 1
c = 0
  • Step 1 : Define a single dimension array

int a[7] = { 0, 1, 2, 3, 4, 5, 6 };
  • Step 2 : Allocate memory for structure pointers

struct ABC **dp;

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

(*dp)->ptr =  a + sizeof(a)/sizeof(a[0]) - 1;;
  • Step 4 : Iterate through single dimension array using pointer

while ((*dp)->ptr >= a)
{
        int c;

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

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

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

struct ABC
{
        int *ptr;
};

int main(void)
{
        int a[7] = { 0, 1, 2, 3, 4, 5, 6 };

        struct ABC **dp;

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

        (*dp)->ptr =  a + sizeof(a)/sizeof(a[0]) - 1;;

        while ((*dp)->ptr >= a)
        {
                int c;

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

        free(*dp);
        free(dp);

        return 0;
}
  • Output is as below

c = 6
c = 5
c = 4
c = 3
c = 2
c = 1
c = 0
  • Step 1 : Define a single dimension array

int a[7] = { 0, 1, 2, 3, 4, 5, 6 };
  • 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 integer pointer to single dimension array

(**dp)->ptr =  a + sizeof(a)/sizeof(a[0]) - 1;;
  • Step 4 : Iterate through single dimension array using pointer

while ((**dp)->ptr >= a)
{
        int c;

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

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

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

struct ABC
{
        int *ptr;
};

int main(void)
{
        int a[7] = { 0, 1, 2, 3, 4, 5, 6 };

        struct ABC ***dp;

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

        (**dp)->ptr =  a + sizeof(a)/sizeof(a[0]) - 1;

        while ((**dp)->ptr >= a)
        {
                int c;

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

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

        return 0;
}
  • Output is as below

c = 6
c = 5
c = 4
c = 3
c = 2
c = 1
c = 0
  • Step 1 : Define a single dimension array

int a[7] = { 0, 1, 2, 3, 4, 5, 6 };
  • Step 2 : Create a structure object

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

abc.pqr.ptr =  a + sizeof(a)/sizeof(a[0]) - 1;;
  • Step 4 : Iterate through single dimension array using pointer

while (abc.pqr.ptr >= a)
{
        int c;

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

#include <stdio.h>

struct PQR
{
        int *ptr;
};

struct ABC
{
        struct PQR pqr;
};

int main(void)
{
        int a[7] = { 0, 1, 2, 3, 4, 5, 6 };

        struct ABC abc;

        abc.pqr.ptr =  a + sizeof(a)/sizeof(a[0]) - 1;;

        while (abc.pqr.ptr >= a)
        {
                int c;

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

        return 0;
}
  • Output is as below

c = 6
c = 5
c = 4
c = 3
c = 2
c = 1
c = 0
  • Step 1 : Define a single dimension array

int a[7] = { 0, 1, 2, 3, 4, 5, 6 };
  • Step 2 : Allocate memory for structure pointers

struct ABC *sp;

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

sp->pqr.ptr =  a + sizeof(a)/sizeof(a[0]) - 1;
  • Step 4 : Iterate through single dimension array using pointer

while (sp->pqr.ptr >= a)
{
        int c;

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

free(sp);
  • See full program below

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

struct PQR
{
        int *ptr;
};

struct ABC
{
        struct PQR pqr;
};

int main(void)
{
        int a[7] = { 0, 1, 2, 3, 4, 5, 6 };

        struct ABC *sp;

        sp = malloc(sizeof(struct ABC));

        sp->pqr.ptr =  a + sizeof(a)/sizeof(a[0]) - 1;;

        while (sp->pqr.ptr >= a)
        {
                int c;

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

        free(sp);

        return 0;
}
  • Output is as below

c = 6
c = 5
c = 4
c = 3
c = 2
c = 1
c = 0
  • Step 1 : Define a single dimension array

int a[7] = { 0, 1, 2, 3, 4, 5, 6 };
  • 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 integer pointer to single dimension array

abc->pqr->ptr =  a + sizeof(a)/sizeof(a[0]) - 1;
  • Step 4 : Iterate through single dimension array using pointer

while (abc->pqr->ptr >= a)
{
        int c;

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

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

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

struct PQR
{
        int *ptr;
};

struct ABC
{
        struct PQR *pqr;
};

int main(void)
{
        int a[7] = { 0, 1, 2, 3, 4, 5, 6 };

        struct ABC *abc;

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

        abc->pqr->ptr =  a + sizeof(a)/sizeof(a[0]) - 1;

        while (abc->pqr->ptr >= a)
        {
                int c;

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

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

        return 0;
}
  • Output is as below

c = 6
c = 5
c = 4
c = 3
c = 2
c = 1
c = 0

Post Decrement : Function Call

  • Step 1 : Define a single dimension array

int a[7] = { 0, 1, 2, 3, 4, 5, 6 };
  • Step 2 : Define a single pointer and point to array

int *ptr;

ptr =  a + sizeof(a)/sizeof(a[0]) - 1;
  • Step 3 : Iterate array using ptr– and Pass by Value

while (ptr >= a)
{
        fun(*ptr--);
}
  • Step 4 : Define a function fun which receives a integer from caller

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

Let us Recall,

  • In case of *ptr--

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

  • Hence, in this case

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

  • See full program below

#include <stdio.h>

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

int main(void)
{
        int a[7] = { 0, 1, 2, 3, 4, 5, 6 };

        int *ptr;

        ptr =  a + sizeof(a)/sizeof(a[0]) - 1;

        while (ptr >= a)
        {
                fun(*ptr--);
        }

        return 0;
}
  • Output is as below

c = 6
c = 5
c = 4
c = 3
c = 2
c = 1
c = 0
  • Step 1 : Define a single dimension array

int a[7] = { 0, 1, 2, 3, 4, 5, 6 };
  • Step 2 : Define a single pointer and point to array

int *ptr;

ptr =  a + sizeof(a)/sizeof(a[0]) - 1;
  • Step 3 : Iterate array using ptr– and Pass by Value

while (ptr >=a)
{
        fun(ptr--);
}
  • Step 4 : Define a function fun which receives a integer pointer from caller

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

Let us Recall,

  • In case of ptr--

    • Current value of ptr is first assigned, then decremented

  • Hence, in this case

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

  • See full program below

#include <stdio.h>

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

int main(void)
{
        int a[7] = { 0, 1, 2, 3, 4, 5, 6 };

        int *ptr;

        ptr =  a + sizeof(a)/sizeof(a[0]) - 1;

        while (ptr >=a)
        {
                fun(ptr--);
        }

        return 0;
}
  • Output is as below

c = 6
c = 5
c = 4
c = 3
c = 2
c = 1
c = 0