what is the meaning of "offset"

This is a discussion on what is the meaning of "offset" within the C Programming forums, part of the General Programming Boards category; hi guys, what is the meaning of "offset" in pointers?...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    13

    what is the meaning of "offset"

    hi guys,
    what is the meaning of "offset" in pointers?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    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.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    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??

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    Quote Originally Posted by cromologic View Post
    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.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    thanks alot,have a great day!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. the meaning of " >> "
    By arian in forum C++ Programming
    Replies: 8
    Last Post: 03-30-2005, 09:40 AM
  2. Greatness, and its meaning
    By axon in forum A Brief History of Cprogramming.com
    Replies: 52
    Last Post: 11-18-2004, 09:53 PM
  3. The Meaning of Life: A Trick Question?
    By chix/w/guns in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 07-12-2004, 07:53 PM
  4. The meaning of "Duh"
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 02-26-2003, 12:46 PM
  5. would you help me with Linked list, please?
    By unhwan in forum C Programming
    Replies: 1
    Last Post: 06-11-2002, 12:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21