what is the meaning of "offset"

• 05-01-2008
cromologic
what is the meaning of "offset"
hi guys,
what is the meaning of "offset" in pointers?
• 05-01-2008
matsp
An offset is the distance from (say) the start of a struct to a particular element, e.g.:
Code:

```struct a {   int x;   int y; };```
offset of y would be (most likely) 4.

--
Mats
• 05-01-2008
cromologic
is the numbers used to indentify the array element?
example in:

Code:

```for(i=0;i<3;i++) {   for(j=0;j<3;j++)   {     printf("Array element matrix ", a[i][j]);   } }```
the values of matrix a => a[0][0] a[0][1]....so here the offsets are the index of the memory locations right??
• 05-01-2008
matsp
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
• 05-01-2008
brewbuck
Quote:

Originally Posted by cromologic
hi guys,
what is the meaning of "offset" in pointers?

You can add integral values to pointers, to get new pointers. For instance:

Code:

```char *ptr = ...; char *ptr2 = ptr + 10;```
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."

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.
• 05-02-2008
cromologic
thanks alot,have a great day!