• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Strangeness when moving data through an array.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Strangeness when moving data through an array.


  • Subject: Re: Strangeness when moving data through an array.
  • From: Sherm Pendley <email@hidden>
  • Date: Wed, 1 Dec 2004 03:46:10 -0500

On Dec 1, 2004, at 2:36 AM, Andrew Farmer wrote:

On 30 Nov 2004, at 23:23, Sherm Pendley wrote:
On Nov 30, 2004, at 11:55 PM, John Draper wrote:
Now, I take the data from "pkt" and I want to later stick it into a
16 bit (u_short),  and would expect...   short_test=258,  but
after I execute this statement...

   bcopy((u_short)&pkt[MSEQ], &short_test, 2);
short_test = 160

WHY? What's going on? How can I extract data from an array in any arbitrary
index into it (even or odd), and still get back that 2 byte int correctly?

You're typecasting a pointer (&pkt[MSEQ]) to an integer (u_short), which smells fishy.


I'd say you probably need to cast to a different pointer type:

bcopy((u_short *)&pkt[MSEQ], &short_test, 2);

Or do something rather simpler.

pkt[MSEQ] = short_test;

(or is there a reason you aren't doing it this way?)

That's backwards - he's assigning *to* short_test.

short_test = pkt[MSEQ];

But regardless, that kind of straightforward assignment would only work if pkt[] happens to be an array of u_shorts. If it's an array of chars (for example), it would take the char at index MSEQ, and assign the value of that char to short_test.

But I don't think that's what John wants to do - I think he wants to assign the two bytes pkt[MSEQ] and pkt[MSEQ+1] to a u_short. To do that with a direct assignment, you'd need to take the address of the array element you want, typecast that pointer to a u_short*, and then dereference it:

short_test = *((u_short*)&pkt[MSEQ]);

That's a *little* simpler than the function call, but not a whole heck of a lot.

sherm--

Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: OutlineView
  • Next by Date: Re: Off-screen Printing
  • Previous by thread: Re: OutlineView
  • Next by thread: RE: Strangeness when moving data through an array.
  • Index(es):
    • Date
    • Thread