Date: Wed, 14 May 1997 01:24:45 -0400 (EDT) From: Michael Phelps To: Krystyna de Heras cc: djgpp AT delorie DOT com Subject: Re: HELP! Pointers and structs In-Reply-To: <33734562.2756@mailbox.swipnet.se> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk On Tue, 13 May 1997, Krystyna de Heras wrote: > Hi all! > > I have been coding in C++ for soon four days and while I am porting some > Pascal code I had this idea of optimizing things a little bit also. > > The problem is this: Incrementing pointers in a structure. > > In my thick C++ book it says. > > type *variable=new type[n]; > variable[a]=2; <=> *(variable+a)=2; > > So I began to think. Let's say we have this structure: > > typedef struct clowntype > { > unsigned long int amount_of_pies, balloons; > } clowntype; > > typedef struct cirkustype > { > unsigned long int amount_of_clowns, ticket_cost; > clowntype *clowns; > } cirkustype; > > main bla bla > > cirkustype *cirkus_max= new cirkustype; > > ok here is something I don't know if it's legal > but it compiles allright. > > clowntype *leasing= new clowntype[5]; > cirkus_max->clowns=leasing; // Clowns Points at same as adress as > leasing. > > Now for the problem. > leasing[10].amount_of_pies=12; > THIS WON'T COMPILE > cirkus_max->(*(clowns+10)).amount_of_pies=12; That line doesn't make sense. What you really want is: (cirkus_max->clowns)[10].amount_of_pies = 12; ^^^^^^^^^^^^^^^^^^ This portion equals the pointer leasing, which is what you really want. > THIs will compile > cirkus_max->clowns=cirkus_max->clowns+10; > cirkus_max->clowns->amount_of_pies=12; > > > Is there a way of indexing clowns without changing its value? > ---Michael Phelps morphine AT cs DOT jhu DOT edu CH3 | N / | ______/ | / \ CH2 _____/ \__|__ // \\ / | \\ // \\______/___CH2 \\ \ / \ / \______/ \_____/ / ------ \ / \ OH \ / OH O Morphine