int x[] = { 1, 4, 8, 5, 1, 4 }; 
int *ptr, y; 
ptr  = x + 4; 
y = ptr - x;

why y is coming 4 in this code
i think that it should be 8

Recommended Answers

All 12 Replies

Try substituting a memory value for x and working out the values...It works out to 4.

i am unable to understand

Well... first of all, it should not come out 8, but 5.

Going into your code, y is not defined as a pointer, but it should, because what you are performing inside your code is pointer arithmetic.

Next, you are subtracting the address of the array, as x actually means &x[0] . So what you want to do is y = ptr - *x .

Performing the modifications, we end up with

#include<stdio.h>

int main(void) {

    int x[] = {1, 4, 8, 5, 1, 4};
    int *ptr, *y;
    ptr = x + 4;
    y = ptr - *x;

    printf("ptr: %d\n", *ptr);
    printf("y: %d\n", *y);

    return 0;
}

It outputs

ptr: 1
y: 5

Well... first of all, it should not come out 8, but 5.

Going into your code, y is not defined as a pointer, but it should, because what you are performing inside your code is pointer arithmetic.

Next, you are subtracting the address of the array, as x actually means &x[0] . So what you want to do is y = ptr - *x .

Performing the modifications, we end up with

#include<stdio.h>

int main(void) {

    int x[] = {1, 4, 8, 5, 1, 4};
    int *ptr, *y;
    ptr = x + 4;
    y = ptr - *x;

    printf("ptr: %d\n", *ptr);
    printf("y: %d\n", *y);

    return 0;
}

It outputs

ptr: 1
y: 5

Your line 8 should be:
y = ptr - x;

It's my fault, I didn't completely understand his post. I think what he wants to do is have ptr point to the fifth element in the array and then have y point to the forth one, by subtracting element 1 from element 5. In this case, the following code applies

#include<stdio.h>
     
int main(void) {
     
    int x[] = {1, 4, 8, 5, 1, 4};
    int *ptr, y;
    ptr = x + 4;
    y = ptr - 1;
     
    printf("ptr: %d\n", *ptr);
    printf("y: %d\n", *(x + y));
     
    return 0;
}

Now to try to explain things a bit, y is a simple integer variable (not a pointer), which stores the result of subtracting 1 from the index of ptr . The original version was redundant because x = x[0] , and ptr = x[4] . Therefore, we have x[4] - x[0] , which, in pointer arithmetic, means 4 - 0, which is redundant. But you want to go one element before the one ptr points to, so you have to subtract 1 from its index. The bug here is that arrays are indexed from 0 to N-1, where N is the size of the array. Therefore, if you try to subtract the first element's index, you fall into redundancy. So the only way around this is to subtract one, as I did in the above code. Next, the *(x + y) part in the last printf(); actually means x[y] , so that's how you do something very simple with pointers and pointer arithmetic.

It's my fault, I didn't completely understand his post. I think what he wants to do is have ptr point to the fifth element in the array and then have y point to the forth one, by subtracting element 1 from element 5. In this case, the following code applies

#include<stdio.h>
     
int main(void) {
     
    int x[] = {1, 4, 8, 5, 1, 4};
    int *ptr, y;
    ptr = x + 4;
    y = ptr - 1;
     
    printf("ptr: %d\n", *ptr);
    printf("y: %d\n", *(x + y));
     
    return 0;
}

Now to try to explain things a bit, y is a simple integer variable (not a pointer), which stores the result of subtracting 1 from the index of ptr . The original version was redundant because x = x[0] , and ptr = x[4] . Therefore, we have x[4] - x[0] , which, in pointer arithmetic, means 4 - 0, which is redundant. But you want to go one element before the one ptr points to, so you have to subtract 1 from its index. The bug here is that arrays are indexed from 0 to N-1, where N is the size of the array. Therefore, if you try to subtract the first element's index, you fall into redundancy. So the only way around this is to subtract one, as I did in the above code. Next, the *(x + y) part in the last printf(); actually means x[y] , so that's how you do something very simple with pointers and pointer arithmetic.

I don't know where your getting all this information from...The original post hasn't changed, has it?

No, it hasn't. But the OP hasn't told us what he or she is trying to achieve, so I'm trying to figure it out. Or is it something wrong with my code/explanation? If that's the case, please tell me, I'm still learning, and I'd like to know if I got anything wrong regarding the pointers.

There are a couple of things

If you have

int x=[1,2,3,4,5];
printf("%u\n",x);        // Address of the first element of the arry
printf("%d\n",*x);       // Value of the element at the address x ie the first element of the array

So if you write the statement

y = ptr - *x;
// ptr contains an address, but *x contains a value

Also I think you made a typo in the line

int *ptr, y;
// Should have been
int *ptr, *y;

@daredevil786

The most simplest explanation for you code

y= ptr- x;
= (x+4)- x;
= 4

On a more serious note, have you studied pointer arithmetic ?

first of all thanks for all this
and i want to know is it legal to have one side pointer arithmetic and on one side a int variable
and i was saying it to be 8 because ptr is pointing to a integer variable so there should be a difference of 8 between them

and i want to know is it legal to have one side pointer arithmetic and on one side a int variable

I guess what you are asking is, is it legal to have a pointer and an integer value in the same arithmetic expression

You can have something of this sort

int x[]={1,2,3,4,5};
int *p=x;

printf("%d\n",*p);    // Prints out 1
p=p+1;
printf("%d\n",*p);    // Prints out 2

first of all thanks for all this
and i want to know is it legal to have one side pointer arithmetic and on one side a int variable
and i was saying it to be 8 because ptr is pointing to a integer variable so there should be a difference of 8 between them

The value of the difference between two pointers pointing to elements of the same array or one past the last element of the array is the difference between the corresponding indexes. The size of the difference expression result type is implementation defined. The type name for this signed integer type is ptrdiff_t which is defined in <stddef.h>.

So your example showed

int x[] = { 1, 4, 8, 5, 1, 4 }; 
int *ptr, y; 
ptr = x + 4; 
y = ptr - x;

Here is an update (that I have not tested).

#include <stddef.h>

int x[] = { 1, 4, 8, 5, 3, 6 }; 

int main(void)
{
  int *ptr
  ptrdiff_t y;
 
  ptr = x + 4; 
  y = ptr - x;
  return 0;
}

I changed the second 1 and 4 in the array so that every element has a different value.

I changed the type of y from int to ptrdiff_t to make the point. However, int is a signed type that will have enough size for this example in any implementation.

After ptr = x + 4; is executed, then ptr points to x[4] and thus *ptr would have the value 3. After y = ptr - x; is executed y will have the value 4.

You were assuming that the difference is the differences in the addresses. You were also assuming that the sizeof(int) is 2 in your implementation. However, there are common implementations where sizeof(int) is 4. There are implementations with other values for sizeof(int).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.