delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/05/13/14:44:27

Message-ID: <33734562.2756@mailbox.swipnet.se>
Date: Tue, 13 May 1997 20:40:18 -2000
From: Krystyna de Heras <krystyna DOT de DOT heras AT mailbox DOT swipnet DOT se>
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: HELP! Pointers and structs

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;
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?

- Raw text -


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