-1

I was experimenting with array and pointers in C. And here

http://r4r.co.in/c/c_topics/c_array_basics/array_representation_in_memory.shtml

I read that

ar[2]=*(ar+(sizeof(datatype)*2));

However after writing following test program in C:-

#include<stdio.h>
#include<conio.h>

int main()
{
    int array[]={10,11,12,13,14};

    printf("\nArray:%d",array);
    printf("\nArray+4:%p",array+4);
    printf("\nArray+(sizeof(int)*4):%p",array+((sizeof(int))*4));
    printf("\n*(Array+4):%d",*(array+4));
    printf("\n*(Array+(sizeof(int)*4)):%d",*(array+((sizeof(int))*4)));

    getch();
    return 0;
}

I get following output:-

Array:2686700
Array+4:0028FEFC
Array+(sizeof(int)*4):0028FF2C
*(Array+4):14
*(Array+(sizeof(int)*4)):4214784

So from here I concluded that the expression

array[4]=*(array+(sizeof(int)*4));

is not working rather expression

array[4]=*(array+4);

is true.

So is my conclusion right or wrong? If it is wrong then why is that so?

Language:-C Version:-gcc (tdm-1) 4.7.1 Platform:-Windows 8.1

Edit:
1). Changed following lines:
((sizeof(int*))*4 to ((sizeof(int))*4
2). Changed title to : Various ways of array representation using pointers in C.
3). Fixed grammar.

DCP
  • 111
  • 1
  • 10
  • 1
    Note the datatype of `array`. Pointer aritmatic involves the data type of the pointer. and please, use `%p` for printing pointers. – Sourav Ghosh Jun 05 '16 at 11:58
  • Yes, for any array (or pointer) `a` and index `i` the expression `a[i]` is exactly the same as `*(a + i)`. It's even in the C specification. – Some programmer dude Jun 05 '16 at 12:02
  • The why it is written at some places that ar[2]=*(ar+(sizeof(datatype)*2));. Is that wrong.Please Explain – DCP Jun 05 '16 at 12:06
  • 1
    `ar[2]=*(ar+(sizeof(datatype)*2));` vs. `array[2]=*(array+(sizeof(int)*4));`: Why do you use `2`in the 1st snippet and `4` in the second? Typo or intention? :-S – alk Jun 05 '16 at 12:09
  • Please try next asset : `*((char *)array+((sizeof(int*))*4)))` and `*(int *)((char *)array+((sizeof(int*))*4)))` The idea is that when you add some value to pointer it doesn't add addresses. It just re-position to element X of datasize according to pointer type – George Gaál Jun 05 '16 at 12:09
  • 1
    @alk sorry it my mistake I am correcting it – DCP Jun 05 '16 at 12:11
  • OT: To print a pointer value use `p` and only `p`, not `d`. – alk Jun 05 '16 at 12:11
  • 1
    Pointer arithmetics: Incrementing a pointer of type `T` by `N` increments the pointer by `N * sizeof (T)` bytes. `void`-pointer may not be operands to this arithmetics. – alk Jun 05 '16 at 12:14
  • Related: http://stackoverflow.com/q/232303/694576 – alk Jun 05 '16 at 12:20
  • @ George Gaál Thanks for you comment.Now I understand it clearly – DCP Jun 05 '16 at 12:23

4 Answers4

1

E1[E2] is defined to be equivalent to *((E1)+(E2)) (N1570 6.5.2.1), so array[4] will always be *(array+4) given that it is in valid range.

+ operator used between a pointer to integer is defined to give (the integer) elements (not bytes) after what is pointerd by the pointer. (N1570 6.5.6), so *(array+(sizeof(int)*4)) will be out-of-range if sizeof(int) >= 2 and moving pointer to out-of-range will invoke undefined behavior.

Also note that you should use correct format specifiers for printf(), or you will invoke undefined behavior.

#include<stdio.h>

int main(void)
{
    int array[]={10,11,12,13,14};

    printf("\nArray:%p",(void*)array);
    printf("\nArray+4:%p",(void*)(array+4));
    printf("\nArray+(sizeof(int)*4):%p",(void*)(array+((sizeof(int*))*4))); /* maybe undefined behavior */
    printf("\n*(Array+4):%d",*(array+4));
    printf("\n*(Array+(sizeof(int)*4)):%d",*(array+((sizeof(int*))*4))); /* maybe undefined behavior */

    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

You originally misinterpreted the way addition works with pointers. A pointer is an address plus the type of object it points to. So, addition knows that it has to add sizeof() bytes.

See Difference Between *(Pointer + Index) and Pointer[] and this.

Plus, in statement

printf("\nArray+(sizeof(int)*4):%p",array+((sizeof(int*))*4));

you are actually using sizeof(int*) instead of sizeof(int). Likewise in another line. So, you are not printing what you think you are.

Note also the interesting facts about commutation.

Community
  • 1
  • 1
0

Pointer arithmetic takes the size of the pointed-to type into account. Given a pointer p to an object of type T, the expression p+1 will give the address of the next object of type T. So, if p == 0x8000, p+1 may yield 0x8001, 0x8002, 0x8004, 0x8008, or any other value depending on the size of T.

Thus, a[i] == *(a + i); given an address a, offset i elements (not bytes) from that address and dereference the result. You do not need to multiply the offset by the size of the pointed-to type.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0
 printf("\n*(Array+(sizeof(int)*4)):%d",*(array+((sizeof(int*))*4)));

should be

printf("\n*(Array+(sizeof(int)*4)):%d",*(array+((sizeof(int))*4)));

and the above is only valid when the array of int is treated each byte, which i think is unlikely.