-1

In the Below code , each location can hold 32 bit signed data. So, why doesn't the value a1 i.e 20 get stored at consecutive address de9? Array should store the data at consecutive memory location right? I know that the sizeof(int) in this online gdb compiler is 4 bytes but how can it be related to the storing of 20 at 4 locations away? Am I missing some basic concepts?

  int main()
    {
     int a[2]= {214748368,20};
       
       void *ptr1 = &a;
       void *ptr2= &a;
       
       ptr1++;
       ptr2 = ptr2;
       
       printf("%d \n", *(int*)ptr2);
       printf("\n Pointer table");
       printf("\n Data : %d at address : %p", *(int*)ptr2, ptr2);
       ptr2=ptr2+1;
       printf("\n Data : %d at address : %p", *(int*)ptr2, ptr2);
       ptr2=ptr2+1;
       printf("\n Data : %d at address : %p", *(int*)ptr2, ptr2);
       ptr2=ptr2+1;
       printf("\n Data : %d at address : %p", *(int*)ptr2, ptr2);
       ptr2=ptr2+1;
       printf("\n Data : %d at address : %p", *(int*)ptr2, ptr2);
       return 0;
    }

Code Output

Steve
  • 27
  • 4
  • `ptr1++;` advances 1-past the end of `a`, `ptr2 = ptr2;` makes no sense. `void *ptr1 = a;` would allow `ptr1++;` to point to the 2nd element of `a`. – David C. Rankin Aug 07 '20 at 19:58
  • 2
    That is because `int` have `4`(here) bytes width so, it is going to be stored after `4` bytes i.e. `a[0]` at `0x7fff97fb9de9` and `a[1]` at `0x7fff97fb9dec` – Sourabh Choure Aug 07 '20 at 20:00
  • 1
    The problem with `ptr1` is when you use the `&` (address of) operator on `a`, e.g. `&a`, the resulting pointer is a *pointer-to-array* `int[2]` instead of allowing normal array/pointer conversion to take place via [C11 Standard - 6.3.2.1 Other Operands - Lvalues, arrays, and function designators(p3)](http://port70.net/~nsz/c/c11/n1570.html#6.3.2.1p3) – David C. Rankin Aug 07 '20 at 20:02
  • 2
    Constraint violation for trying to do arithmetic on `void*`. Your compiler was obligated to issue a diagnostic message about this, but many c compilers are non-conforming in this way. You may be able to un-break the compiler by giving it the right command line argument to actually do its job correctly. – EOF Aug 07 '20 at 20:04
  • By using untyped pointer (void *), arithmetic on pointer is done assuming single byte data. Declaring `ptr1` and `ptr2` as `int *` would correctly advance one slot by adding 1 (and as a bonus, would remove the need of casting). – Phil1970 Aug 07 '20 at 20:04
  • 2
    This program is broken. Pointer arithmetic on `void` pointers is a non-standard gcc extension. Accessing bytes in the middle of an `int` object via an `int` pointer is undefined behaviour. You can examine individual bytes but you need to do so via a `char` pointer. – n. m. could be an AI Aug 07 '20 at 20:07

1 Answers1

3

When you increase a pointer in C pointer arithmetic is used. So if your pointer a is of type int * it will be increased by sizeof(int). However your pointer is of type void *. This is not intended, apparently it is increased by one byte (see here).

As the size of an int on your system is 32 bits or 4 bytes you have to increase the pointer four times by one byte to reach the next element in your array.

You can avoid this issue by defining your pointers as int *:

int *ptr1 = &a;
int *ptr2 = &a;

In this case incrementing the pointer once (ptr2++) will give you the next element.

sevsev
  • 91
  • 6