hi guys,
what is the meaning of "offset" in pointers?
hi guys,
what is the meaning of "offset" in pointers?
An offset is the distance from (say) the start of a struct to a particular element, e.g.:
offset of y would be (most likely) 4.Code:struct a { int x; int y; };
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
is the numbers used to indentify the array element?
example in:
the values of matrix a => a[0][0] a[0][1]....so here the offsets are the index of the memory locations right??Code:for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("Array element matrix ", a[i][j]); } }
Well, the offset would be sizeof(element) * (i * n + j), where n is the number of elements in the "j" side of the array, and element is the type that the array holds elements of.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
You can add integral values to pointers, to get new pointers. For instance:
In that code fragment, 10 is the "offset" being added to the pointer. It makes a new pointer which points ten bytes further into memory than the original one did. The pointer the value is being added to is often called the "base."Code:char *ptr = ...; char *ptr2 = ptr + 10;
When you index an array, for instance x[i], you can think of i as an offset, and the base is x.
In general, an offset is like an address, but it is always relative to some other absolute location. The offset is only meaningful with respect to some base.
thanks alot,have a great day!