delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/04/30/04:15:40

From: horst DOT kraemer AT gmx DOT de (Horst Kraemer)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Array swapping.
Date: Sun, 30 Apr 2000 08:54:39 GMT
Lines: 50
Message-ID: <390bf4ba.51457508@news.cis.dfn.de>
References: <390B75BB DOT 621F846A AT gtcom DOT net>
NNTP-Posting-Host: brln-3e35756e.pool.mediaways.net (62.53.117.110)
X-Trace: fu-berlin.de 957084810 9437979 62.53.117.110 (16 [27606])
X-Newsreader: Forte Free Agent 1.11/32.235
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Sat, 29 Apr 2000 19:52:27 -0400, Krogg <krogg DOT no DOT to DOT spam AT gtcom DOT net>
wrote:

> 
> I got 2 arrays of same type/size.
> 
> float abc[50][50];
> float cba[50][50];
> 
> how can i "swap" them?
> 
> so that abc[x][y] will now point to cba[x][y] and
> vice versa...

abc and cba don't point. Don't believe in fairy tales sine
grandmothers tell you. Arrays are _not_ pointers nor "constant"
pointers. They are arrays. They have meat. They are "data" like ints
or floats or structs.

Therefore you can't "swap" statically allocated arrays without
swapping the data float by float.

You have to define pointers which "correspond" to these arrays - in
the same ways as a pointer to char "corresponds" to a an array of
char. float[50][50] if an array of array of 50 floats. The
corresponding pointer type is the pointer type where the "outer array
type" is transformed to pointer, i.e. a pointer to array of 50 float:

	float x[100][50];
	float y[100][50];


	float (*p1)[50]; 
	float (*p2)[50]; 

	p1 = x;
	p2 = y;

Now you may use p1[3][5]|p2[3][5] in place of x[3][5]|y[3][5] in every
context. In order to "swap" you just swap the pointers:

	void *temp;

	temp = p1 ; p1 = p2 ; p2 = temp:



Regards
Horst

- Raw text -


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