pre increment 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), ++*ptr ?

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

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

What is the difference between c = ++ptr, 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

int *ptr;

ptr = a;
  • Step 3 : Increment 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;

        ++ptr;

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

        return 0;
}
  • Output is as below

*ptr = 1
  • 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;
  • Step 3 : Print the 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;

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

        return 0;
}
  • Output is as below

*ptr = 0
*ptr = 1
*ptr = 2
*ptr = 3
*ptr = 4
*ptr = 5
*ptr = 6
  • 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;
  • Step 3 : Print the integer array by iterating through all integers using ++ptr

while (ptr <= (a + sizeof(a)/sizeof(a[0])) - 1)
{
        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;

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

        return 0;
}
  • Output is as below

*ptr = 0
*ptr = 1
*ptr = 2
*ptr = 3
*ptr = 4
*ptr = 5
*ptr = 6
  • 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;
  • Step 3 : Print the integer array by iterating through all integers using ++ptr

while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
{
        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;

        while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
        {
                int c;

                c = *++ptr;

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

        return 0;
}
  • Output is as below

*ptr = 1
*ptr = 2
*ptr = 3
*ptr = 4
*ptr = 5
*ptr = 6
  • 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;
  • Step 3 : Print the integer array by iterating through all integers using ++ptr

while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
{
        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;

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

        return 0;
}
  • Output is as below

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

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

    • ptr is incremented

    • Current value of ptr is assigned to p

  • We now derived a rule

    • First Increment, then Assign

  • 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;

OR

int *ptr;

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

while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
{
        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

    • ptr is incremented

    • Current value of ptr is assigned to p

  • We now derived a rule

    • First Increment, then Assign

  • See full program below

#include <stdio.h>

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

        int *ptr;

        ptr = a;

        while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
        {
                int *p;

                p = ++ptr;

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

        return 0;
}

Output is as below

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

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

    • ptr is incremented

    • Current value of *ptr is assigned to c

    • *ptr is NOT incremented

  • We now derived a rule

    • First Increment, then Assign

  • 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;

OR

int *ptr;

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

while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
{
        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

    • ptr is incremented

    • Current value of *ptr is assigned to c

    • *ptr is NOT incremented

  • We now derived a rule

    • First Increment, then Assign

  • See full program below

#include <stdio.h>

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

        int *ptr;

        ptr = a;

        while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
        {
                int c;

                c = *(++ptr);

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

        return 0;
}

Output is as below

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

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

    • ptr is incremented

    • Current value of *ptr is assigned to c

    • *ptr is NOT incremented

  • We now derived a rule

    • First Increment, then Assign

  • 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;

OR

int *ptr;

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

while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
{
        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

    • ptr is incremented

    • Current value of *ptr is assigned to c

    • *ptr is NOT incremented

  • We now derived a rule

    • First Increment, then Assign

  • See full program below

#include <stdio.h>

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

        int *ptr;

        ptr = a;

        while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
        {
                int c;

                c = *++ptr;

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

        return 0;
}

Output is as below

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

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

    • ptr is incremented

    • Current value of *ptr is assigned to c

    • *ptr is NOT incremented

  • We now derived a rule

    • First Increment, then Assign

  • 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;

OR

int *ptr;

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

while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
{
        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

    • ptr is incremented

    • Current value of *ptr is assigned to c

    • *ptr is NOT incremented

  • We now derived a rule

    • First Increment, then Assign

  • See full program below

#include <stdio.h>

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

        int *ptr;

        ptr = a;

        while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
        {
                int c;

                c = *++(ptr);

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

        return 0;
}

Output is as below

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

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

    • *ptr is incremented

    • Current value of *ptr is assigned to c

    • ptr is NOT incremented

  • We now derived a rule

    • First Increment, then Assign

  • Consider statement

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

    • *ptr is incremented

    • Current value of *ptr is assigned to c

    • ptr is NOT incremented

  • We now derived a rule

    • First Increment, then Assign

Summary of expressions

Expression

Explanation

c = ++ptr

  • Increment ptr

  • Assign ptr to c

c = *++ptr

  • Increment ptr

  • Assign *ptr to c

  • DO NOT increment *ptr

c = *(++ptr)

  • Increment ptr

  • Assign *ptr to c

  • DO NOT increment *ptr

c = *++(ptr)

  • Increment ptr

  • Assign *ptr to c

  • DO NOT increment *ptr

c = ++*ptr

  • Increment *ptr

  • Assign *ptr to c

  • DO NOT increment ptr

c = ++*(ptr)

  • Increment *ptr

  • Assign *ptr to c

  • DO NOT increment 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;
  • Step 4 : Iterate through single dimension array using pointer

while (abc.ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
{
        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;

        while (abc.ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
        {
                int c;

                c = *++abc.ptr;

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

        return 0;
}

Output is as below

c = 1
c = 2
c = 3
c = 4
c = 5
c = 6
  • 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;
  • Step 4 : Iterate through single dimension array using pointer

while (sp->ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
{
        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;

        while (sp->ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
        {
                int c;

                c = *++sp->ptr;

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

        free(sp);

        return 0;
}

Output is as below

c = 1
c = 2
c = 3
c = 4
c = 5
c = 6
  • 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;
  • Step 4 : Iterate through single dimension array using pointer

while ((*dp)->ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
{
        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;

        while ((*dp)->ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
        {
                int c;

                c = *++(*dp)->ptr;

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

        free(*dp);
        free(dp);

        return 0;
}

Output is as below

c = 1
c = 2
c = 3
c = 4
c = 5
c = 6
  • 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;
  • Step 4 : Iterate through single dimension array using pointer

while ((**dp)->ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
{
        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;

        while ((**dp)->ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
        {
                int c;

                c = *++(**dp)->ptr;

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

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

        return 0;
}

Output is as below

c = 1
c = 2
c = 3
c = 4
c = 5
c = 6
  • 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;
  • Step 4 : Iterate through single dimension array using pointer

while (abc.pqr.ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
{
        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;

        while (abc.pqr.ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
        {
                int c;

                c = *++abc.pqr.ptr;

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

        return 0;
}

Output is as below

c = 1
c = 2
c = 3
c = 4
c = 5
c = 6
  • 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;
  • Step 4 : Iterate through single dimension array using pointer

while (sp->pqr.ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
{
        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;

        while (sp->pqr.ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
        {
                int c;

                c = *++sp->pqr.ptr;

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

        free(sp);

        return 0;
}

Output is as below

c = 1
c = 2
c = 3
c = 4
c = 5
c = 6
  • 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;
  • Step 4 : Iterate through single dimension array using pointer

while (abc->pqr->ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
{
        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;

        while (abc->pqr->ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
        {
                int c;

                c = *++abc->pqr->ptr;

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

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

        return 0;
}

Output is as below

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

Pre Increment : 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;
  • Step 3 : Iterate array using ++ptr and Pass by Value

while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
{
        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

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

  • Hence, in this case

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

  • 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;

        while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
        {
                fun(*++ptr);
        }

        return 0;
}

Output is as below

c = 1
c = 2
c = 3
c = 4
c = 5
c = 6
  • 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;
  • Step 3 : Iterate array using ++ptr and Pass by Value

while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
{
        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

    • ptr is incremented first, then incremented

  • Hence, in this case

    • ptr is incremented first, then passed to function fun

  • 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;

        while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1)
        {
                fun(++ptr);
        }

        return 0;
}

Output is as below

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