delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/30/00:31:22

Message-ID: <34D12BC6.6369@compuserve.com>
Date: Thu, 29 Jan 1998 20:24:22 -0500
From: Laurent Turcotte <lturcott AT compuserve DOT com>
MIME-Version: 1.0
Subject: Re: HELP: accessing astructure members via VOID pointer??
References: <34CD4681 DOT 3747 AT cam DOT org>
Newsgroups: comp.os.msdos.djgpp
Lines: 44
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Vic wrote:
> 
> Hello. I'd like to be able to do something like this:
> I have a struct foo (int x,y,z) and a void* bar=&foo;
> So bar holds the adress of foo. I know that at the beginning of the
> struct lies the first member. so why can't I just say *bar=55 or
> something?
> I'd like to be able to access any member of a struct using a pointer.
> Like, if I want to access the second member, I add 4(or whatever) to the
> pointer and write the value to that adress. How could I do that? HELP!!
> TIA,
> --
> --> http://www.cam.org/~tudor <--


Hi Vic

You should look at the assembly output of your C code
because I think the compiler generate code like what you are trying to
do
example:

struct {
  int  a;
  int  b;
} S;

struct S  *Ptr;

Ptr->a = 3; 

will be compiled to something like
 movl  $3, (Ptr)

Ptr->b = 5; 

will be compiled to
 movl  $5, 4(Ptr)

.. I hope the ATT syntax is ok !


Regards
Laurent Turcotte

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019