I still do not understand offset, please give me examples

This is a discussion on I still do not understand offset, please give me examples within the C++ Programming forums, part of the General Programming Boards category; Okay, I am not jumping the gun on this one, I have been trying to figure out exactly what an ...

  1. #1
    Shadow12345
    Guest

    I still do not understand offset, please give me examples

    Okay, I am not jumping the gun on this one, I have been trying to figure out exactly what an offset is. I read it like every other page and I try to figure out what it means but it doesn't ever make sense.

    Can someone give me some examples of an offset? try to explain them as specific as possible

    thanks.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Consider an array declared as

    int a[5];

    In memory it looks like a very poor attempt at ascii art (really, it does!)
    Code:
    |------|  |------|  |------|  |------|  |------|
    | a[0] |  | a[1] |  | a[2] |  | a[3] |  | a[4] |
    |------|  |------|  |------|  |------|  |------|
    The first element, a[0] can be considered the base memory address for the array. To access further elements, you use an offset from that base address:

    int *p = a + 2;

    This pointer now points to a[2] because you took the base address and added the offset of 2 to it, moving the pointer down in memory two spaces the size of an int. This is basically all an offset is, a certain amount of space past a base memory address.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Shadow12345
    Guest
    Okay that is a very good explanation and thanks for putting the time into the ascii art ( looks good enough and i know it is time consuming)

    seriously though i have never seen that defined in my book, they just assume that i know what it is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows - how to get started... examples don't help
    By nonoob in forum Windows Programming
    Replies: 6
    Last Post: 09-26-2008, 06:45 AM
  2. Thread Pool libraries or examples
    By Mastadex in forum Windows Programming
    Replies: 6
    Last Post: 08-24-2008, 09:58 PM
  3. Give me some opinions on setting up a server
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 04-19-2004, 11:38 AM
  4. Treeview examples.
    By Sebastiani in forum Windows Programming
    Replies: 0
    Last Post: 09-21-2003, 01:16 PM
  5. How To Give A Font Colour ?
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 09-14-2001, 02:22 PM

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