Q:

Affect of tem assignment in last for loop

In the last "for" loop of below code, I expected same output for the assignment of "tem" above the last "for " loop as well as within the last "for" loop. But the outputs are different. Could anyone explain why?
I tried assigning the tem between while and for loop as well as within the last for loop.
#include<iostream>
#include<vector>
using namespace std;

  void temp(int a[][4],int m,int n,int k){

        int temp[m*n];
        int row=0,col=0;
        int *tem=temp;
        int r=m,l=n;
        int *start=temp;

 while(row<m&&col<n){
                      int *end=start;

   for(int i=row;i<m;i++){

                     *tem=a[i][col];
                    tem++;
                    end++;

                    }col++;
    for(int i=col;i<n;i++){

                     *tem=a[m-1][i];
                    tem++;
                    end++;

                          }m--;

    for(int i=m-1;i>=row;i--){

                            *tem=a[i][n-1];
                                tem++;
                               end++;
                              }n--;
    for(int i=n-1;i>=col;i--){

                          *tem=a[row][i];
                            tem++;
                            end++;
                            }row++;  

    if(end-start>k){reverse(start,k,end-1);

               start=end;
                    }}   
    for(int i=0;i<r*l;i++){ 

                       tem=temp;

                    cout<<*tem<<" ";
                    tem++;

                }

     }
   void reverse(int *start,int k,int *end){
                      int *s=start;
                      int *e=start+k-1;
    while(s<e){ 
        int tem=*s;
        *s=*e;
         *e=tem;
        s++;e--;
        }
                        s=start+k; e=end;
    while(s<e){

                          int tem=*s;
                           *s=*e;
                        s++;e--;
        }
                       s=start; e=end;
    while(s<e){
                        int tem=*s;
                       *s=*e;
                       *e=tem;
                        s++;e--;

        }   

 }

    int main(){

 int b[][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};int j=0;

   temp(b,4,4,3);

   }

If the assignment of tem is inside 
13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 
I expected 
13 14 15 16 12 8 4 3 2 1 5 9 7 6 10 11
which is the output if i assign tem between while and for loop.

A:

In this code
for (int i=0; i<r*l; i++)
{
    tem = temp;
    cout<< *tem << " ";
    tem++;
}

tem is made to point to the beginning of the temp array each time around the loop. Because tem is always pointing to the beginning of the temp array when you print the value of *tem you always see the same value output.
But in this code 
tem = temp;
for (int i=0; i<r*l; i++)
{
    cout << *tem <<" ";
    tem++;
}

tem is assigned to the beginning of the temp array once only before the loop. Then in the loop tem is incremented so that it points to the next element of the temp array. So because tem is pointing to a different element of the temp array each time round the loop, you see different values printed.

