SPUG: C question ;)
Scott Blachowicz
scott at mail.dsab.rresearch.com
Sun Mar 10 23:28:23 CST 2002
On Sun, Mar 10, 2002 at 05:35:38PM -0800, Benjamin Franks wrote:
> ...
> int shmid=shmget((ftok("blah/blah",1), 1000*sizeof(my_struct), IPC_CREAT | 0666);
>
> Then when I attach the shmid to a pointer, do I want to attach a pointer
> of the my_struct type, as in:
>
> my_struct *ptr;
> ptr=shmat(shmid,NULL,0);
>
> I guess that's where my confusion lies. Let's say I want to work on the
> 500th element of my shared memory array of my_struct's. How can I do it?
Well...in C, an array name is kind of like a constant ptr and array indexing
is just a form of pointer arithmetic, so once you have a pointer, you're in
business.
/* Set all of the elements to the current time. */
for (i = 0; i < 500; i++) ptr[i].a = ptr[i].b = time(NULL);
/* All of these should be equivalent (unless I'm remembering wrong). */
(ptr+499)->a = 0;
(*(ptr+499)).a = 0;
ptr[499].a = 0;
499[ptr].a = 0; /* !! */
Is that what you wanted?
--
Scott Blachowicz
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
Subscriptions; Email to majordomo at pm.org: ACTION LIST EMAIL
Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
For daily traffic, use spug-list for LIST ; for weekly, spug-list-digest
Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org
More information about the spug-list
mailing list