simple pointers question

This is a discussion on simple pointers question within the C Programming forums, part of the General Programming Boards category; Hello, I have two pointers: unsigned char *rdata and unsigned char *rdata_tail rdata_tail will always represent a memory location greater ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    2

    simple pointers question

    Hello, I have two pointers:

    unsigned char *rdata
    and unsigned char *rdata_tail

    rdata_tail will always represent a memory location greater than or equal to rdata

    Let's say the memory that rdata starts at is like such: "hello"

    (but the program doesnt know this)

    rdata_tail gives the memory address after "o"

    My question is, how do I get the number 5 using these 2 pointers?


    Thanks

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    What do you mean get the number 5? Like a value of 5(int) from a char?
    Woop?

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    2
    no =/

    like this:

    memory:
    -1-2-3-4-5-
    [h][e][l][l][o]

    i want to subtract memory address 2 from memory address 1

    and get 5 because memory address 2 is 5 unsigned chars ahead of memorry address 1

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Code:
    // did you try it?
         unsigned char *rdata = // the start of a block of memory
         unsigned char *rdata_tail = // supposedly the end of a block of memory or something.
         rdata = rdata+2 - rdata_tail;
    Note that pointer arithmatic is dangerous, but rdata now points to the area you want by assigning the pointer elsewhere.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're doing it backwards. The "tail" is greater than the "head". Thus you subtract head from tail, not the other way around. There is also a variable type for just this type of thing:
    Code:
    ptrdiff_t len = tail - head;
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers Question
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 09-08-2008, 10:17 AM
  2. simple question about pointers and casting
    By steve1_rm in forum C++ Programming
    Replies: 3
    Last Post: 03-28-2008, 02:25 PM
  3. Simple Half-Life Coding Question
    By bengreenwood in forum Game Programming
    Replies: 1
    Last Post: 11-07-2007, 01:18 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Pointers Question.....Null Pointers!!!!
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-28-2001, 10:13 PM

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