post decrement 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 at last printable character
char *ptr;
ptr = a + strlen(a) - 1;
Step 3 : Decrement
ptr
ptr--;
Step 4 : Print
*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 + strlen(a) - 1;
ptr--;
printf("*ptr = %c\n", *ptr);
return 0;
}
Output is as below
*ptr = o
Step 1 : Define a Single Dimension array
char a[7] = "Laptop";
Step 2 : Point
ptr
to Single dimension array
char *ptr;
ptr = a + strlen(a) - 1;
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 + strlen(a) - 1;
for (int i = 0; i < strlen(a); i++)
{
printf("*ptr = %c\n", *ptr);
ptr--;
}
return 0;
}
Output is as below
*ptr = p
*ptr = o
*ptr = t
*ptr = p
*ptr = a
*ptr = L
Step 1 : Define a Single Dimension array
char a[7] = "Laptop";
Step 2 : Point
ptr
to Single dimension array
char *ptr;
ptr = a + strlen(a) - 1;
Step 3 : Print string by iterating through all characters using
ptr--
while (ptr >= a)
{
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 + strlen(a) - 1;
while (ptr >= a)
{
printf("*ptr = %c\n", *ptr);
ptr--;
}
return 0;
}
Output is as below
*ptr = p
*ptr = o
*ptr = t
*ptr = p
*ptr = a
*ptr = L
Step 1 : Define a Single Dimension array
char a[7] = "Laptop";
Step 2 : Point
ptr
to Single dimension array
char *ptr;
ptr = a + strlen(a) - 1;
Step 3 : Print string by iterating through all characters using
ptr--
while (ptr >= a)
{
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 + strlen(a) - 1;
while (ptr >= a)
{
char c;
c = *ptr--;
printf("*ptr = %c\n", c);
}
return 0;
}
Output is as below
*ptr = p
*ptr = o
*ptr = t
*ptr = p
*ptr = a
*ptr = L
Step 1 : Define a Single Dimension array
char a[7] = "Laptop";
Step 2 : Point
ptr
to Single dimension array
char *ptr;
ptr = a + strlen(a) - 1;
Step 3 : Print string by iterating through all characters using
ptr--
while (ptr >= a)
{
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 + strlen(a) - 1;
while (ptr >= a)
{
printf("*ptr = %c\n", *ptr--);
}
return 0;
}
Output is as below
*ptr = p
*ptr = o
*ptr = t
*ptr = p
*ptr = a
*ptr = L
Consider statement
p = ptr--;
There are two steps in this statement
Current value of
ptr
is assigned top
ptr
is decremented
We now derived a rule
First Assign, then decrement
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 + strlen(a) - 1;
OR
char *ptr;
ptr = &a[0] + strlen(a) - 1;
Step 3 : Iterate and print the string
while (ptr >= a)
{
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
Current value of
ptr
is assigned top
ptr
is decremented
We now derived a rule
First Assign, then decrement
See full program below
#include <stdio.h>
#include <string.h>
int main(void)
{
char a[7] = "Laptop";
char *ptr;
ptr = a + strlen(a) - 1;
while (ptr >= a)
{
char *p;
p = ptr--;
printf("*p = %c\n", *p);
}
return 0;
}
Output is as below
*ptr = p
*ptr = o
*ptr = t
*ptr = p
*ptr = a
*ptr = L
Consider statement
c = *(ptr--);
There are two steps in this statement
Current value of
*ptr
is assigned toc
ptr
is decremented*ptr
is NOT decremented
We now derived a rule
First Assign, then decrement
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 + strlen(a) - 1;
OR
char *ptr;
ptr = &a[0] + strlen(a) - 1;
Step 3 : Iterate and print the string
while (ptr >= a)
{
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
Current value of
*ptr
is assigned toc
ptr
is decremented*ptr
is NOT decremented
We now derived a rule
First Assign, then decrement
See full program below
#include <stdio.h>
#include <string.h>
int main(void)
{
char a[7] = "Laptop";
char *ptr;
ptr = a + strlen(a) - 1;
while (ptr >= a)
{
char c;
c = *(ptr--);
printf("c = %c\n", c);
}
return 0;
}
Output is as below
c = p
c = o
c = t
c = p
c = a
c = L
Consider statement
c = *ptr--;
There are two steps in this statement
Current value of
*ptr
is assigned toc
ptr
is decremented*ptr
is NOT decremented
We now derived a rule
First Assign, then decrement
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 + strlen(a) - 1;
OR
char *ptr;
ptr = &a[0] + strlen(a) - 1;
Step 3 : Iterate and print the string
while (ptr >= a)
{
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
Current value of
*ptr
is assigned toc
ptr
is decremented*ptr
is NOT decremented
We now derived a rule
First Assign, then decrement
See full program below
#include <stdio.h>
#include <string.h>
int main(void)
{
char a[7] = "Laptop";
char *ptr;
ptr = a + strlen(a) - 1;
while (ptr >= a)
{
char c;
c = *ptr--;
printf("c = %c\n", c);
}
return 0;
}
Output is as below
c = p
c = o
c = t
c = p
c = a
c = L
Consider statement
c = *(ptr)--;
There are two steps in this statement
Current value of
*ptr
is assigned toc
ptr
is decremented*ptr
is NOT decremented
We now derived a rule
First Assign, then decrement
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 + strlen(a) - 1;
OR
char *ptr;
ptr = &a[0] + strlen(a) - 1;
Step 3 : Iterate and print the string
while (ptr >= a)
{
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
Current value of
*ptr
is assigned toc
ptr
is decremented*ptr
is NOT decremented
We now derived a rule
First Assign, then decrement
See full program below
#include <stdio.h>
#include <string.h>
int main(void)
{
char a[7] = "Laptop";
char *ptr;
ptr = a + strlen(a) - 1;
while (ptr >= a)
{
char c;
c = *(ptr)--;
printf("c = %c\n", c);
}
return 0;
}
Output is as below
c = p
c = o
c = t
c = p
c = a
c = L
Consider statement
c = (*ptr)--;
There are two steps in this statement
Current value of
*ptr
is assigned toc
ptr
is NOT decremented*ptr
is decremented
We now derived a rule
First Assign, then decrement
Summary of expressions
Expression |
Explanation |
---|---|
c = ptr– |
|
c = *(ptr–) |
|
c = *ptr– |
|
c = *(ptr)– |
|
c = (*ptr)– |
|
Post Decrement : 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 + strlen(a) - 1;;
Step 4 : Iterate through single dimension array using pointer
while (abc.ptr >= a)
{
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 + strlen(a) - 1;;
while (abc.ptr >= a)
{
char c;
c = *abc.ptr--;
printf("c = %c\n", c);
}
return 0;
}
Output is as below
c = p
c = o
c = t
c = p
c = a
c = L
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 + strlen(a) - 1;;
Step 4 : Iterate through single dimension array using pointer
while (sp->ptr >= a)
{
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 + strlen(a) - 1;;
while (sp->ptr >= a)
{
char c;
c = *sp->ptr--;
printf("c = %c\n", c);
}
free(sp);
return 0;
}
Output is as below
c = p
c = o
c = t
c = p
c = a
c = L
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 + strlen(a) - 1;;
Step 4 : Iterate through single dimension array using pointer
while ((*dp)->ptr >= a)
{
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 + strlen(a) - 1;;
while ((*dp)->ptr >= a)
{
char c;
c = *(*dp)->ptr--;
printf("c = %c\n", c);
}
free(*dp);
free(dp);
return 0;
}
Output is as below
c = p
c = o
c = t
c = p
c = a
c = L
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 + strlen(a) - 1;;
Step 4 : Iterate through single dimension array using pointer
while ((**dp)->ptr >= a)
{
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 + strlen(a) - 1;
while ((**dp)->ptr >= a)
{
char c;
c = *(**dp)->ptr--;
printf("c = %c\n", c);
}
free(**dp);
free(*dp);
free(dp);
return 0;
}
Output is as below
c = p
c = o
c = t
c = p
c = a
c = L
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 + strlen(a) - 1;;
Step 4 : Iterate through single dimension array using pointer
while (abc.pqr.ptr >= a)
{
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 + strlen(a) - 1;;
while (abc.pqr.ptr >= a)
{
char c;
c = *abc.pqr.ptr--;
printf("c = %c\n", c);
}
return 0;
}
Output is as below
c = p
c = o
c = t
c = p
c = a
c = L
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 + strlen(a) - 1;
Step 4 : Iterate through single dimension array using pointer
while (sp->pqr.ptr >= a)
{
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 + strlen(a) - 1;;
while (sp->pqr.ptr >= a)
{
char c;
c = *sp->pqr.ptr--;
printf("c = %c\n", c);
}
free(sp);
return 0;
}
Output is as below
c = p
c = o
c = t
c = p
c = a
c = L
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 + strlen(a) - 1;
Step 4 : Iterate through single dimension array using pointer
while (abc->pqr->ptr >= a)
{
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 + strlen(a) - 1;
while (abc->pqr->ptr >= a)
{
char c;
c = *abc->pqr->ptr--;
printf("c = %c\n", c);
}
free(abc->pqr);
free(abc);
return 0;
}
Output is as below
c = p
c = o
c = t
c = p
c = a
c = L
Post Decrement : 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 + strlen(a) - 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 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, thenptr
decremented
Hence, in this case
Current value of
*ptr
is first passed to functionfun
, thenptr
decremented
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 + strlen(a) - 1;
while (ptr >= a)
{
fun(*ptr--);
}
return 0;
}
Output is as below
c = p
c = o
c = t
c = p
c = a
c = L
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 + strlen(a) - 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 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 decremented
Hence, in this case
Current value of
ptr
is first passed to functionfun
, then decremented
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 + strlen(a) - 1;
while (ptr >=a)
{
fun(ptr--);
}
return 0;
}
Current Module
Previous Module
Other Modules