pre 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)
,--*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
ptr
to Single dimension array
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>
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);
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);
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
Guess what is going on ?
Statement
c = *--ptr
can be understood in two stepsStep 1 : Decrement
ptr
Step 2 : Assign
*ptr
toc
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);
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);
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
Guess what is going on ?
Statement
printf("*ptr = %c\n", *--ptr);
can be understood in two stepsStep 1 : Decrement
ptr
Step 2 : Pass
*ptr
as arguement toprintf
Consider statement
p = --ptr;
There are two steps in this statement
ptr
is decrementedCurrent value of
ptr
is assigned top
We now derived a rule
First Decrement, 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 + strlen(a);
OR
char *ptr;
ptr = &a[0];
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
ptr
is decrementedCurrent value of
ptr
is assigned top
We now derived a rule
First Decrement, then Assign
See full program below
#include <stdio.h>
#include <string.h>
int main(void)
{
char a[7] = "Laptop";
char *ptr;
ptr = a + strlen(a);
while (ptr > a)
{
char *p;
p = --ptr;
printf("*p = %c\n", *p);
}
return 0;
}
Output is as below
*p = p
*p = o
*p = t
*p = p
*p = a
*p = L
Consider statement
c = *(--ptr);
There are two steps in this statement
ptr
is decrementedCurrent value of
*ptr
is assigned toc
*ptr
is NOT decremented
We now derived a rule
First Decrement, 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 + strlen(a);
OR
char *ptr;
ptr = &a[0];
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
ptr
is decrementedCurrent value of
*ptr
is assigned toc
*ptr
is NOT decremented
We now derived a rule
First Decrement, then Assign
See full program below
#include <stdio.h>
#include <string.h>
int main(void)
{
char a[7] = "Laptop";
char *ptr;
ptr = a + strlen(a);
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
ptr
is decrementedCurrent value of
*ptr
is assigned toc
*ptr
is NOT decremented
We now derived a rule
First Decrement, 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 + strlen(a);
OR
char *ptr;
ptr = &a[0];
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
ptr
is decrementedCurrent value of
*ptr
is assigned toc
*ptr
is NOT decremented
We now derived a rule
First Decrement, then Assign
See full program below
#include <stdio.h>
#include <string.h>
int main(void)
{
char a[7] = "Laptop";
char *ptr;
ptr = a + strlen(a);
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
ptr
is decrementedCurrent value of
*ptr
is assigned toc
*ptr
is NOT decremented
We now derived a rule
First Decrement, 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 + strlen(a);
OR
char *ptr;
ptr = &a[0];
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
ptr
is decrementedCurrent value of
*ptr
is assigned toc
*ptr
is NOT decremented
We now derived a rule
First Decrement, then Assign
See full program below
#include <stdio.h>
#include <string.h>
int main(void)
{
char a[7] = "Laptop";
char *ptr;
ptr = a + strlen(a);
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
*ptr
is decrementedCurrent value of
*ptr
is assigned toc
ptr
is NOT decremented
We now derived a rule
First Decrement, then Assign
Consider statement
c = --(*ptr);
There are two steps in this statement
*ptr
is decrementedCurrent value of
*ptr
is assigned toc
ptr
is NOT decremented
We now derived a rule
First Decrement, then Assign
Summary of expressions
Expression |
Explanation |
---|---|
c = –ptr |
|
c = *–ptr |
|
c = *(–ptr) |
|
c = *–(ptr) |
|
c = –*ptr |
|
c = –*(ptr) |
|
Pre 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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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
Pre 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);
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
ptr
is decremented first, then current value of*ptr
is assigned
Hence, in this case
ptr
is decremented first, then Current value of*ptr
is passed to functionfun
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);
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);
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
ptr
is decremented first, then decremented
Hence, in this case
ptr
is decremented first, then passed to functionfun
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);
while (ptr > a)
{
fun(--ptr);
}
return 0;
}
Output is as below
*c = p
*c = o
*c = t
*c = p
*c = a
*c = L
Current Module
Previous Module
Next Module
Other Modules