More help with pointers

This is a discussion on More help with pointers within the C Programming forums, part of the General Programming Boards category; I would like to transfer the byte in the memory chunk pData that is i bytes from the start of ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    14

    More help with pointers

    I would like to transfer the byte in the memory chunk pData that is i bytes from the start of pData to the memory chunk newData that is (i+offset) bytes from the start of newData:

    Is there a better way to code this:
    Code:
    	MemPtr	newData;
    	Char	*pData
    	Char	*offsetData;
    	offsetData = (Char *)newData;
    	offsetData += (i+offset);
    	*offsetData = *pData+i;
    this way:
    Code:
    	MemPtr	newData;
    	Char	*pData
    	((Char *)newData)[i+offset] = pData[i];

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Why the cast?

    Code:
    newData[i+offset] = pData[i];
    Edit: This assumes Char is the same as char (ie. typedef or at least 1 byte in length) or was a typo.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, the second method loooks sane. Alternatively:
    Code:
    	MemPtr	newData;
            Char    *pNewData = (Char *)newData;
    	Char	*pData
    	pNewData[i+offset] = pData[i];
    Just looks a bit tidier, probably makes very little difference in performance.

    --
    Mats

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MacGyver View Post
    Why the cast?

    Code:
    newData[i+offset] = pData[i];
    Edit: This assumes Char is the same as char (ie. typedef or at least 1 byte in length) or was a typo.
    I suspect MemPtr may be
    Code:
    typedef void *MemPtr;
    That would necessitate a cast to be able to set it to something.

    --
    Mats

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    14
    Mats,

    You are correct about MemPtr.

    Thank you for pointing me in the right direction.


    MacGyver,

    Char is char, Palm OS uses different names for reasons unknown to me.


    Thanks

    Greg

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gmrobert View Post
    Char is char, Palm OS uses different names for reasons unknown to me.


    Thanks

    Greg
    It allows the OS to have control over the type used, without relying on what the compiler may do with things - and if someone decided to make everything for example Unicode (16-bit chars), then Char could just be redefined once, and all else would essentially work straight away [probably some breakages, but still easier than searching for ALL "char" in the code and replacing them with something else].

    This is definitely very common in most OS's

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 06:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 11:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

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