pre 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)
,--*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 + sizeof(a)/sizeof(a[0]);
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]);
--ptr;
printf("*ptr = %d\n", *ptr);
return 0;
}
Output is as below
*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 + sizeof(a)/sizeof(a[0]) - 1;
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 + 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 the 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]);
Step 3 : Print the 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]);
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
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
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]);
Step 3 : Print the 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]);
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
Guess what is going on ?
Statement
printf("*ptr = %d\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
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]);
OR
int *ptr;
ptr = &a[0] + sizeof(a)/sizeof(a[0]);
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
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>
int main(void)
{
int a[7] = {0, 1, 2, 3, 4, 5, 6};
int *ptr;
ptr = a + sizeof(a)/sizeof(a[0]);
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
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
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]);
OR
int *ptr;
ptr = &a[0] + sizeof(a)/sizeof(a[0]);
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
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>
int main(void)
{
int a[7] = {0, 1, 2, 3, 4, 5, 6};
int *ptr;
ptr = a + sizeof(a)/sizeof(a[0]);
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
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
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]);
OR
int *ptr;
ptr = &a[0] + sizeof(a)/sizeof(a[0]);
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
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>
int main(void)
{
int a[7] = {0, 1, 2, 3, 4, 5, 6};
int *ptr;
ptr = a + sizeof(a)/sizeof(a[0]);
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
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
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]);
OR
int *ptr;
ptr = &a[0] + sizeof(a)/sizeof(a[0]);
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
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>
int main(void)
{
int a[7] = {0, 1, 2, 3, 4, 5, 6};
int *ptr;
ptr = a + sizeof(a)/sizeof(a[0]);
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
*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 : int pointer inside structure
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]);
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]);
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]);
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]);
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]);
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]);
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]);
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]);
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]);
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]);
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]);
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]);
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]);
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]);
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
Pre 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]);
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
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>
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]);
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]);
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
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>
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]);
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
Current Module
Previous Module
Next Module
Other Modules