Explain me output of the below program

Pages: 12
Explain me output of the below program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{

	static int arr1[]={0,1,2,3,4};
	int *p1[]={arr1,arr1+1,arr1+2,arr1+3,arr1+4};
	int **ptr=p1;
	ptr++;
	printf("%d %d %d %d\n",ptr-p1,*ptr-arr1,**ptr);
	*ptr++;
	printf("%d %d %d %d\n",ptr-p1,*ptr-arr1,**ptr);
	*++ptr;
	printf("%d %d %d %d\n",ptr-p1,*ptr-arr1,**ptr);
	++*ptr;
	printf("%d %d %d %d\n",ptr-p1,*ptr-arr1,**ptr);
}

Output 
1 1 1
2 2 2
3 3 3
3 4 4
Sure, but fix all the errors first.
That program does not produce that output.
For starters, you have 4 "%d" in each printf and only 3 value parameters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 In function 'int main()':
9:47: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long int' [-Wformat=]
9:47: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long int' [-Wformat=]
9:47: warning: format '%d' expects a matching 'int' argument [-Wformat=]
10:8: warning: value computed is not used [-Wunused-value]
11:47: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long int' [-Wformat=]
11:47: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long int' [-Wformat=]
11:47: warning: format '%d' expects a matching 'int' argument [-Wformat=]
12:8: warning: value computed is not used [-Wunused-value]
13:47: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long int' [-Wformat=]
13:47: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long int' [-Wformat=]
13:47: warning: format '%d' expects a matching 'int' argument [-Wformat=]
15:47: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long int' [-Wformat=]
15:47: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long int' [-Wformat=]
15:47: warning: format '%d' expects a matching 'int' argument [-Wformat=]
 


*ptr++;
*++ptr;
Both these get you the "value computed is not used" warning.
Both increment the pointer, but the dereference afterwards is a waste of time.
int main()
{

static int arr1[]={0,1,2,3,4};
int *p1[]={arr1,arr1+1,arr1+2,arr1+3,arr1+4};
int **ptr=p1;
ptr++;
printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
*ptr++;
printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
*++ptr;
printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
++*ptr;
printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);


}

Output
1 1 1
2 2 2
3 3 3
3 4 4

Sorry for debugging purpose i added extra %d,same code pasted here.remove one extra %d run Microsoft Visual studio 2010 IDE..give the above output result try once...i have run the above program in visual studio c++2010 ultimate.its gives the above result..

*ptr++;
*++ptr;
here we are incrementing address it will move to next location..why waste of time..

i have one more question in printf statement.

static int arr1[]={0,1,2,3,4};
int *p1[]={arr1,arr1+1,arr1+2,arr1+3,arr1+4};
int **ptr=p1;
ptr++;
printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr); gives below output how
output = 1 1 1
ptr-p1 = 1 (how )
here subtracting two address.not dereference.It has to print difference of the address not value.how it is printing 1 value
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{

     static int arr1[]={0,1,2,3,4};
     int *p1[]={arr1,arr1+1,arr1+2,arr1+3,arr1+4};
     int **ptr=p1;
     ptr++;
     printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
    *ptr++;
     printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
    *++ptr;
    printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
    ++*ptr;
    printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
}


Output
1 1 1
2 2 2
3 3 3
3 4 4

I corrected the above program as per your comment
1) I removed extra %d in the printf statement.

2)i have run the above program in visual studio c++2010 ultimate.its gives the above
result.But you said it wont give above result

3)please Explain me output result.

4) as you said below statement is waste of time.why???
*ptr++;
*++ptr;

5)i have analyzed the code i found below analysis

printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);

analysis :

here subtracting two address.It has to print difference of the address
not value.how it is printing 1 value
Last edited on
> But you said it wont give above result
No, I said your program in post #1 wouldn't even compile.
When it's fixed, it produces the result you see.

> as you said below statement is waste of time.why???
Because *ptr++; is the same as ptr++;
The * bit is the bit which is the complete waste of time, and why you get the message
"warning: value computed is not used "
The same goes for the *++ptr;

Because the pre and post increments are the same by the time the code hits the ;
the first 3 pointer increments and prints are just
1
2
3
4
5
6
     ptr = ptr + 1;
     printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
     ptr = ptr + 1;
     printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
     ptr = ptr + 1;
     printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);


> here subtracting two address.It has to print difference of the address
> not value.how it is printing 1 value
Because pointer subtraction knows about the types of pointers, and is bound to how array indexing works.
1
2
3
int a[10];
int n = 5;
int *pa = &a[n];

At this point, if you did pa - a, you would get 5 as the answer.

Or maybe write it in a more basic form.
1
2
3
int a[10];
int n = 5;
int *pa = a + n;  // what &a[n] means to the compiler. 

Now it's just algebra.
pa = a + n can be rearranged as n = pa - a

Try this as a fun exercise, what does it print?
1
2
3
4
5
#include <stdio.h>
int main ( ) {
  int a[10];
  printf("%zd\n", &a[7] - &a[2] );  // or just %d if your compiler is crusty
}

1
2
3
4
5
6
7
8
9
     ptr++;
     printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
    *ptr++;
     printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
    *++ptr;
    printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
    ++*ptr;
    printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);


Really i am not understanding these printf operation.how it works.
Please could you explain me above printf statement operation with example.
ie
ptr-p1 = 1(how)
*ptr-arr1 = 1(how)
Look at your starting condition.
1
2
3
     static int arr1[]={0,1,2,3,4};
     int *p1[]={arr1,arr1+1,arr1+2,arr1+3,arr1+4};
     int **ptr=p1;


Satisfy yourself that ptr-p1 is zero (this much should be trivially obvious given the initialisation of ptr).

Doing x = something on one line, then doing x - something on the next line is always going to get you back to zero.

Now if you increment ptr, what else other than 1 would ptr-p1 be?

To understand these concept.which book I need to refer or give me any online document to understand
Last edited on
https://www.cplusplus.com/doc/tutorial/pointers/
its a long read, but jump down to the pointer arithmetic & arrays/pointers sections deeper in.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  #include<stdio.h>
   int main() 
     {  
		static int a[2][2] = {1,2,3,4};
		int i , j;
		static int *p[]={(int*)a,(int*)a+1,(int*)a+2,};
		for(i =0 ;i<2;i++) {
			for(j =0;j<2;j++)
				printf("%d %d %d %d \n",
                                          *(*(p+i)+j),*(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i));
                }
            return 0;
    }


output

1 1 1 1
2 2 2 2
2 2 2 2
3 3 3 3

Explain me output of the above program
Last edited on
> Explain me output of the above program
You try to explain it, and we'll see how much you've understood in this thread so far.

Just explaining another example isn't going to transform your knowledge.

FWIW, your teacher/book is poor.
No one serious about writing code writes code that bad.

Hi Salem,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>

int main() {

     static int arr1[]={0,1,2,3,4};
     int *p1[]={arr1,arr1+1,arr1+2,arr1+3,arr1+4};
     int **ptr=p1;
     ptr++;
     printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
    *ptr++;
     printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
    *++ptr;
    printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
    ++*ptr;
    printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
   return 0;
}


My understanding for the first program is that
++ptr (increment the pointer means move next location( ie arr1+1 location ))
>>
1
2
3
            ptr++;
            printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
    


really i didn't understand the
1st parameter ptr-p1 = 1(how)
2nd parameter *ptr-arr1 =1;(how)
below parameter i understood
3rd parameter **ptr (derefencce the pointer it will print the value that
location )

>>
1
2
3
4
       int a[10];
       int n = 5;
       int *pa = a + n;  // what &a[n] means to the compiler
       

here
I understood *pa will hold the address of the nth element.

>> this as a fun exercise, what does it print?
1
2
3
4
5
         #include <stdio.h>
         int main ( ) {
         int a[10];
         printf("%zd\n", &a[7] - &a[2] );  // or just %d if your compiler is crusty
       }

here 7-2 = 5 ???.but my question , it is has to print difference of the address right

ie 7th location address(1020) - 2th location address(1000) = 20





Last edited on
> but my question , it is has to print difference of the address right
Pointer subtraction knows about the type
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
int main ( ) {
  char a[10];
  int b[10];
  double c[10];
  char d[10][1000];
  printf("%zd\n", &a[7] - &a[2] );
  printf("%zd\n", &b[7] - &b[2] );
  printf("%zd\n", &c[7] - &c[2] ); 
  printf("%zd\n", &d[7] - &d[2] ); 
}

The actual numeric difference in the raw addresses is always scaled by the type that is pointed to.

>>The actual numeric difference in the raw addresses is always scaled by the type that is pointed to.

You mean 1020-1000=20 is scaled to 5

int *ptr;
example : ++ptr (increase the address by 4 bytes if integer pointer) same in difference

is my understanding is right

1
2
3
4
5
     static int arr1[]={0,1,2,3,4};
     int *p1[]={arr1,arr1+1,arr1+2,arr1+3,arr1+4};
     int **ptr=p1;
     ptr++;
     printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);


consider p1=1000 ptr = 1000

incrementing ptr pointer by one

so ptr will become 1004(since it is integer type)

then difference of ptr-p1= 4 is scaled to 1
Yes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  #include<stdio.h>
   int main() 
     {  
		static int a[2][2] = {1,2,3,4};
		int i , j;
		static int *p[]={(int*)a,(int*)a+1,(int*)a+2,};
		for(i =0 ;i<2;i++) {
			for(j =0;j<2;j++)
				printf("%d %d %d %d \n",
                                          *(*(p+i)+j),*(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i));
                }
            return 0;
    }


output
1 1 1 1
2 2 2 2
2 2 2 2
3 3 3 3

can you explain me output of the above program
> Explain me output of the above program
You try to explain it, and we'll see how much you've understood in this thread so far.

Just explaining another example isn't going to transform your knowledge.

FWIW, your teacher/book is poor.
No one serious about writing code writes code that bad.
I understood for the below program And i explained you in previous post
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>

int main() {

     static int arr1[]={0,1,2,3,4};
     int *p1[]={arr1,arr1+1,arr1+2,arr1+3,arr1+4};
     int **ptr=p1;
     ptr++;
     printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
    *ptr++;
     printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
    *++ptr;
    printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
    ++*ptr;
    printf("%d %d %d \n",ptr-p1,*ptr-arr1,**ptr);
   return 0;
}


consider p1=1000 ptr = 1000

incrementing ptr pointer by one

so ptr will become 1004(since it is integer type)

then difference of ptr-p1= 4 is scaled to 1

i want explanation for the next program
1
2
3
4
5
6
7
8
9
10
11
12
13
  #include<stdio.h>
   int main() 
     {  
		static int a[2][2] = {1,2,3,4};
		int i , j;
		static int *p[]={(int*)a,(int*)a+1,(int*)a+2,};
		for(i =0 ;i<2;i++) {
			for(j =0;j<2;j++)
				printf("%d %d %d %d \n",
                                          *(*(p+i)+j),*(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i));
                }
            return 0;
    }


as per my understanding in above program
a = 1st row
a+1 = 2nd row
But in the array of pointer it is storing
p[0] = a(1st element of 1st row );
p[1] =a+1(2nd element 1st row);
i want know why it is storing 1st and 2nd element of 1st row


I suggest you start analysing
1
2
3
4
5
static int a[2][2] = {1,2,3,4};
int i = 0 , j = 0;
static int *p[]={(int*)a,(int*)a+1,(int*)a+2,};
printf("%d %d %d %d \n",
         *(*(p+i)+j),*(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i));


It also might help to give much better names to help you comprehend more.
1
2
3
4
5
6
7
8
static int twoDarray[2][2] = {1,2,3,4};
int i = 0 , j = 0;
static int *pointersToRows[]={(int*)twoDarray,(int*)twoDarray+1,(int*)twoDarray+2,};
printf("%d %d %d %d \n",
        *(*(pointersToRows+i)+j),
        *(*(j+pointersToRows)+i),
        *(*(i+pointersToRows)+j),
        *(*(pointersToRows+j)+i));

Pages: 12