pre 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),++*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 
char a[7] = "Laptop";
- Step 2 : Point - ptrto 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;
}
- Output is as below 
*ptr = a
- Step 1 : Define a Single Dimension array 
char a[7] = "Laptop";
- Step 2 : Point - ptrto 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;
}
- Output is as below 
*ptr = L
*ptr = a
*ptr = p
*ptr = t
*ptr = o
*ptr = p
- Step 1 : Define a Single Dimension array 
char a[7] = "Laptop";
- Step 2 : Point - ptrto 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;
}
- Output is as below 
*ptr = L
*ptr = a
*ptr = p
*ptr = t
*ptr = o
*ptr = p
- Step 1 : Define a Single Dimension array 
char a[7] = "Laptop";
- Step 2 : Point - ptrto 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;
}
- Output is as below 
*ptr = a
*ptr = p
*ptr = t
*ptr = o
*ptr = p
*ptr =
Guess what is going on ?
- Statement - c = *++ptrcan be understood in two steps- Step 1 : Increment - ptr
- Step 2 : Assign - *ptrto- c
 
- Step 1 : Define a Single Dimension array 
char a[7] = "Laptop";
- Step 2 : Point - ptrto 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;
}
- Output is as below 
*ptr = a
*ptr = p
*ptr = t
*ptr = o
*ptr = p
*ptr =
Guess what is going on ?
- Statement - printf("*ptr = %c\n", *++ptr);can be understood in two steps- Step 1 : Increment - ptr
- Step 2 : Pass - *ptras arguement to- printf
 
- Consider statement 
p = ++ptr;
- There are two steps in this statement - ptris incremented
- Current value of - ptris assigned to- p
 
- We now derived a rule - First Increment, then Assign 
 
- 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("*p = %c\n", *p);
}
Can you guess what is happening ?
- Let us Recall 
p = ++ptr;
- There are two steps in this statement - ptris incremented
- Current value of - ptris assigned to- p
 
- We now derived a rule - First Increment, then Assign 
 
- 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("*p = %c\n", *p);
        }
        return 0;
}
Output is as below
*p = a
*p = p
*p = t
*p = o
*p = p
*p =
- Consider statement 
c = *(++ptr);
- There are two steps in this statement - ptris incremented
- Current value of - *ptris assigned to- c
- *ptris NOT incremented
 
- We now derived a rule - First Increment, then Assign 
 
- 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("c = %c\n", c);
}
Can you guess what is happening ?
- Let us Recall 
c = *(++ptr);
- There are two steps in this statement - ptris incremented
- Current value of - *ptris assigned to- c
- *ptris NOT incremented
 
- We now derived a rule - First Increment, then Assign 
 
- 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("c = %c\n", c);
        }
        return 0;
}
Output is as below
c = a
c = p
c = t
c = o
c = p
c =
- Consider statement 
c = *++ptr;
- There are two steps in this statement - ptris incremented
- Current value of - *ptris assigned to- c
- *ptris NOT incremented
 
- We now derived a rule - First Increment, then Assign 
 
- 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("c = %c\n", c);
}
Can you guess what is happening ?
- Let us Recall 
c = *++ptr;
- There are two steps in this statement - ptris incremented
- Current value of - *ptris assigned to- c
- *ptris NOT incremented
 
- We now derived a rule - First Increment, then Assign 
 
- 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("c = %c\n", c);
        }
        return 0;
}
Output is as below
c = a
c = p
c = t
c = o
c = p
c =
- Consider statement 
c = *++(ptr);
- There are two steps in this statement - ptris incremented
- Current value of - *ptris assigned to- c
- *ptris NOT incremented
 
- We now derived a rule - First Increment, then Assign 
 
- 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("c = %c\n", c);
}
Can you guess what is happening ?
- Let us Recall 
c = *++(ptr);
- There are two steps in this statement - ptris incremented
- Current value of - *ptris assigned to- c
- *ptris NOT incremented
 
- We now derived a rule - First Increment, then Assign 
 
- 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("c = %c\n", c);
        }
        return 0;
}
Output is as below
c = a
c = p
c = t
c = o
c = p
c =
- Consider statement 
c = ++*ptr;
- There are two steps in this statement - *ptris incremented
- Current value of - *ptris assigned to- c
- ptris NOT incremented
 
- We now derived a rule - First Increment, then Assign 
 
- Consider statement 
c = ++(*ptr);
- There are two steps in this statement - *ptris incremented
- Current value of - *ptris assigned to- c
- ptris NOT incremented
 
- We now derived a rule - First Increment, then Assign 
 
Summary of expressions
| Expression | Explanation | 
|---|---|
| c = ++ptr | 
 | 
| c = *++ptr | 
 | 
| c = *(++ptr) | 
 | 
| c = *++(ptr) | 
 | 
| c = ++*ptr | 
 | 
| c = ++*(ptr) | 
 | 
Pre Increment : char pointer inside structure
- 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("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("c = %c\n", c);
        }
        return 0;
}
Output is as below
c = a
c = p
c = t
c = o
c = p
c =
- 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("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 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("c = %c\n", c);
        }
        free(sp);
        return 0;
}
Output is as below
c = a
c = p
c = t
c = o
c = p
c =
- 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("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("c = %c\n", c);
        }
        free(*dp);
        free(dp);
        return 0;
}
Output is as below
c = a
c = p
c = t
c = o
c = p
c =
- 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("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("c = %c\n", c);
        }
        free(**dp);
        free(*dp);
        free(dp);
        return 0;
}
Output is as below
c = a
c = p
c = t
c = o
c = p
c =
- 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("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("c = %c\n", c);
        }
        return 0;
}
Output is as below
c = a
c = p
c = t
c = o
c = p
c =
- 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("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("c = %c\n", c);
        }
        free(sp);
        return 0;
}
Output is as below
c = a
c = p
c = t
c = o
c = p
c =
- 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("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("c = %c\n", c);
        }
        free(abc->pqr);
        free(abc);
        return 0;
}
Output is as below
c = a
c = p
c = t
c = o
c = p
c =
Pre 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 - funwhich receives a character from caller
void fun(char c)
{
        printf("c = %c\n", c);
}
Let us Recall,
- In case of - *++ptr- ptris incremented first, then current value of- *ptris assigned
 
- Hence, in this case - ptris incremented first, then Current value of- *ptris passed to function- fun
 
- 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;
}
Output is as below
c = a
c = p
c = t
c = o
c = p
c =
- 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 - funwhich receives a character pointer from caller
void fun(char *c)
{
        printf("*c = %c\n", *c);
}
Let us Recall,
- In case of - ++ptr- ptris incremented first, then incremented
 
- Hence, in this case - ptris incremented first, then passed to function- fun
 
- 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;
}
Output is as below
c = a
c = p
c = t
c = o
c = p
c =
- Current Module 
- Previous Module 
- Next Module 
- Other Modules