post increment int single pointer
In this section, you are going to learn
How to iterate integer array 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)++
?
Basics of Post Increment
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;
}
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 integer array by iterating through all integers using
ptr++
for (int i = 0; i < 7; 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 < 7; i++)
{
printf("*ptr = %d\n", *ptr);
ptr++;
}
return 0;
}
Consider statement
p = ptr++;
There are two steps in this statement
Current value of
ptr
is assigned top
ptr
is incremented
We now derived a rule
First Assign, then increment
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
for (int i = 0; i < 7; i++)
{
int *p;
p = ptr++;
printf("*p = %d\n", *p);
}
Output is as below
*p = 0
*p = 1
*p = 2
*p = 3
*p = 4
*p = 5
*p = 6
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 incremented
We now derived a rule
First Assign, then increment
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 < 7; i++)
{
int *p;
p = ptr++;
printf("*p = %d\n", *p);
}
return 0;
}
Consider statement
c = *(ptr++);
There are two steps in this statement
Current value of
*ptr
is assigned toc
ptr
is incremented*ptr
is NOT incremented
We now derived a rule
First Assign, then increment
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
for (int i = 0; i < 7; i++)
{
int c;
c = *(ptr++);
printf("c = %d\n", c);
}
Output is as below
c = 0
c = 1
c = 2
c = 3
c = 4
c = 5
c = 6
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 incremented*ptr
is NOT incremented
We now derived a rule
First Assign, then increment
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 < 7; i++)
{
int c;
c = *(ptr++);
printf("c = %d\n", c);
}
return 0;
}
Consider statement
c = *ptr++;
There are two steps in this statement
Current value of
*ptr
is assigned toc
ptr
is incremented*ptr
is NOT incremented
We now derived a rule
First Assign, then increment
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
for (int i = 0; i < 7; i++)
{
int c;
c = *ptr++;
printf("c = %d\n", c);
}
Output is as below
c = 0
c = 1
c = 2
c = 3
c = 4
c = 5
c = 6
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 incremented*ptr
is NOT incremented
We now derived a rule
First Assign, then increment
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 < 7; i++)
{
int c;
c = *ptr++;
printf("c = %d\n", c);
}
return 0;
}
Consider statement
c = *(ptr)++;
There are two steps in this statement
Current value of
*ptr
is assigned toc
ptr
is incremented*ptr
is NOT incremented
We now derived a rule
First Assign, then increment
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
for (int i = 0; i < 7; i++)
{
int c;
c = *(ptr)++;
printf("c = %d\n", c);
}
Output is as below
c = 0
c = 1
c = 2
c = 3
c = 4
c = 5
c = 6
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 incremented*ptr
is NOT incremented
We now derived a rule
First Assign, then increment
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 < 7; i++)
{
int c;
c = *(ptr)++;
printf("c = %d\n", c);
}
return 0;
}
Consider statement
c = (*ptr)++;
There are two steps in this statement
Current value of
*ptr
is assigned toc
ptr
is NOT incremented*ptr
is incremented
We now derived a rule
First Assign, then increment
Summary of expressions
Expression |
Explanation |
---|---|
c = ptr++ |
|
c = *(ptr++) |
|
c = *ptr++ |
|
c = *(ptr)++ |
|
c = (*ptr)++ |
|
Post Increment : 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;
Step 4 : Iterate through single dimension array using pointer
for (int i = 0; i < 7; i++)
{
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;
for (int i = 0; i < 7; i++)
{
int c;
c = *abc.ptr++;
printf("c = %d\n", c);
}
return 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;
Step 4 : Iterate through single dimension array using pointer
for (int i = 0; i < 7; i++)
{
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;
for (int i = 0; i < 7; i++)
{
int c;
c = *sp->ptr++;
printf("c = %d\n", c);
}
free(sp);
return 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;
Step 4 : Iterate through single dimension array using pointer
for (int i = 0; i < 7; i++)
{
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;
for (int i = 0; i < 7; i++)
{
int c;
c = *(*dp)->ptr++;
printf("c = %d\n", c);
}
free(*dp);
free(dp);
return 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;
Step 4 : Iterate through single dimension array using pointer
for (int i = 0; i < 7; i++)
{
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;
for (int i = 0; i < 7; i++)
{
int c;
c = *(**dp)->ptr++;
printf("c = %d\n", c);
}
free(**dp);
free(*dp);
free(dp);
return 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;
Step 4 : Iterate through single dimension array using pointer
for (int i = 0; i < 7; i++)
{
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;
for (int i = 0; i < 7; i++)
{
int c;
c = *abc.pqr.ptr++;
printf("c = %d\n", c);
}
return 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;
Step 4 : Iterate through single dimension array using pointer
for (int i = 0; i < 7; i++)
{
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;
for (int i = 0; i < 7; i++)
{
int c;
c = *sp->pqr.ptr++;
printf("c = %d\n", c);
}
free(sp);
return 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;
Step 4 : Iterate through single dimension array using pointer
for (int i = 0; i < 7; i++)
{
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;
for (int i = 0; i < 7; i++)
{
int c;
c = *abc->pqr->ptr++;
printf("c = %d\n", c);
}
free(abc->pqr);
free(abc);
return 0;
}
Post 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
for (int i = 0; i < 7; i++)
{
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++
Current value of
*ptr
is first assigned, thenptr
incremented
Hence, in this case
Current value of
*ptr
is first passed to functionfun
, thenptr
incremented
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;
for (int i = 0; i < 7; i++)
{
fun(*ptr++);
}
return 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;
Step 3 : Iterate array using ptr++ and Pass by Value
for (int i = 0; i < 7; i++)
{
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++
Current value of
ptr
is first assigned, then incremented
Hence, in this case
Current value of
ptr
is first passed to functionfun
, then incremented
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;
for (int i = 0; i < 7; i++)
{
fun(ptr++);
}
return 0;
}
Current Module
Previous Module
Next Module
Other Modules