little problem of pointer

do arr[a+4] and arr[a]+4 have same meaning ??
also *(p+3) and*p+3 ??
Last edited on
shahzaib1111 wrote:
do arr[a+4] and arr[a]+4 have same meaning ??


No

EDIT (to match OP's edit)
*(p+3) and*p+3 are not the same
Last edited on
if
not
then what is the difference
arr[a+4] - this retrieves the value at index a+4 of the array.
so if a is 10, then arr[a+4] will retrieve the value at index 14 of the array.

arr[a]+4 means retireve the value at index a of the array and add 4 to this value.
So again if a had the value 10, then arr[a]+4 will retrive the value at index 10 of the array then adds 4 to that particular value.

As you can see, they are two different actions- (also if you remember that the array subscript operator [] has higher precedence than the + operator you can immediately see that they are different actions )

Last edited on
Try running this code, see what is the output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    int arr[8];

    arr[0] = 2;
    arr[1] = 3;
    arr[2] = 5;
    arr[3] = 7;
    arr[4] = 11;
    arr[5] = 13;
    arr[6] = 17;
    arr[7] = 19;

    int a = 2;

    cout << "arr[a+4] is " << arr[a+4] << endl;
    cout << "arr[a]+4 is " << arr[a]+4 << endl;

note, you will need to add the usual main(), #includes etc.

And try editing it, change some of the values, see what happens.
Last edited on
Topic archived. No new replies allowed.