Hello, I am not able to understand whats happening behind "ptr-p", "*ptr-arr". Please explain

#include<stdio.h>

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

What are you really trying to do? Understand how pointers adn pointer arithmetic work and the relationship between pointers and arrays?

When you subtract two pointers, as long as they point into the same array, the result is the number of elements separating them and in this case each int is 4 byte for example.. then you get this otput on my pc:
size of int 4
ptr before changes 2293496
p before changes 2293496
ptr after increment 2293500

How many separate ptr and p [1] 1 1

2 2 2

3 3 3

3 4 4

Process returned 0 (0x0) execution time : 0.029 s
Press any key to continue.

#include<stdio.h>

int main()
{
        static int arr[]={0,1,2,3,4};
        int *p[]={arr,arr+1,arr+2,arr+3,arr+4};
        int **ptr=p;
        printf("size of int %d\n", sizeof(int));
        printf("ptr before changes %d\n", ptr);
        printf("p before changes %d\n", p);
        ptr++;
        printf("ptr after increment %d\n", ptr);
        printf("\n How many separate ptr and p [%d] %d %d \n",ptr-p,*ptr-arr,**ptr);
        *ptr++;
        printf("\n %d %d %d \n",ptr-p,*ptr-arr,**ptr);
        *++ptr;
        printf("\n %d %d %d \n",ptr-p,*ptr-arr,**ptr);
        ++*ptr;
        printf("\n %d %d %d \n",ptr-p,*ptr-arr,**ptr);
        return 0;
}

about *ptr-arr here is the debug code. Hope you figure this out need to sleep now. Sorry for no comments i can do it later if there are questions.

#include<stdio.h>

int main()
{
        static int arr[]={0,1,2,3,4};
        int *p[]={arr,arr+1,arr+2,arr+3,arr+4};
        int **ptr=p;
        printf("size of int %d\n", sizeof(int));
        printf("ptr before changes %d\n", ptr);
        printf("p before changes %d\n", p);
        printf("arr before changes %d\n", arr);
        printf("*ptr before changes(points to arr) %d\n", *ptr);
        ptr++;
        printf("ptr after increment %d\n", ptr);
        printf("*ptr after increment(arr+1) %d\n", *ptr);
        printf("\n How many separate ptr and p [%d] %d %d \n",ptr-p,*ptr-arr,**ptr);
        *ptr++;
        printf("\n %d %d %d \n",ptr-p,*ptr-arr,**ptr);
        *++ptr;
        printf("\n %d %d %d \n",ptr-p,*ptr-arr,**ptr);
        ++*ptr;
        printf("\n %d %d %d \n",ptr-p,*ptr-arr,**ptr);
        return 0;
}

http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence
http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/BitOp/pointer.html

Dont be afraid to add as much printf as possible.

Edited 3 Years Ago by Sokurenko: links

ptr-p should return a address. But here its returning data ?

For ,*ptr-arr,
lets say arr is pointing to address 200
ptr after ptr++ points to 204, then *(ptr-arr) would turn out as *(4) ?

ptr-p should return a address. But here its returning data ?

Subtracting two pointers returns the difference between the addresses, not another address.

Subtracting two pointers returns the difference between the addresses, not another address.

But subtracting an unsigned long integer from an address should result in another address. Whether or not it is a valid address depends... :-)

Edited 3 Years Ago by rubberman

Subtracting two pointers returns the difference between the addresses, not another address.

But even then here, the difference would be 4 (as its int) ?

This article has been dead for over six months. Start a new discussion instead.