delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/05/02/11:05:24

From: Hans-Bernhard Broeker <broeker AT physik DOT rwth-aachen DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Array swapping.
Date: 2 May 2000 14:06:40 GMT
Organization: Aachen University of Technology (RWTH)
Lines: 43
Message-ID: <8emndg$b9s$1@nets3.rz.RWTH-Aachen.DE>
References: <390B75BB DOT 621F846A AT gtcom DOT net>
NNTP-Posting-Host: acp3bf.physik.rwth-aachen.de
X-Trace: nets3.rz.RWTH-Aachen.DE 957276400 11580 137.226.32.75 (2 May 2000 14:06:40 GMT)
X-Complaints-To: abuse AT rwth-aachen DOT de
NNTP-Posting-Date: 2 May 2000 14:06:40 GMT
Originator: broeker@
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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...

You want them to 'point', so obviously you need pointers. :-) 

Now, some keep saying that arrays are the same as pointers, in
C. That's not true, and this is exactly one of the cases where the
difference becomes important. You either have to convert to

	float (*abc)[50];
	float (*cba)[50];

and use 'malloc' to allocate them, or you can

	float real_abc[50][50];
	float real_cba[50][50];

	float (*abc)[50] = real_abc;
	float (*cba)[50] = real_cba;

In both cases the swap then works like this:

	{
	   float (*abc)[50] temp = abc;
	   abc = cba;
	   cba = temp;
        }

A side note: this is not really on-topic for this newsgroup. It's a
basic C question, and thus should be asked to comp.lang.c or a similar
group.
-- 
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.

- Raw text -


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