c语言交换二维数组的两行,如何交换二维数组的两行,为什么它会工作?

编程入门 行业动态 更新时间:2024-10-08 14:49:45

c语言交换二维<a href=https://www.elefans.com/category/jswz/34/1771288.html style=数组的两行,如何交换二维数组的两行,为什么它会工作?"/>

c语言交换二维数组的两行,如何交换二维数组的两行,为什么它会工作?

综述:

请查看David,Uwe和其他专家的评论。

================================================== ==============================

下面的代码在两个二维动态的double值数组中交换两行。我想知道:(1)以下代码是否是交换两维数组的两行的最佳做法?如果不是,那么做这种工作的最佳做​​法是什么? (2)为什么下面的代码有效?我的意思是,不是二维数组是连续的 strike>连续的内存段吗?下面的代码是否只能运气?任何建议表示赞赏!

unit Unit5;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs;

type

TAADouble = array of array of Double;

TForm5 = class(TForm)

procedure FormShow(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form5: TForm5;

procedure SwapRows(arr: TAADouble; row0, row1: Integer);

implementation

{$R *.dfm}

procedure SwapRows(arr: TAADouble; row0, row1: Integer);

var

Tmp: Integer;

begin

{$IFDEF FPC}

Tmp := PtrUInt(arr[row0]);

PtrUInt(arr[row0]) := PtrUInt(arr[row1]);

PtrUInt(arr[row1]) := Tmp;

{$ELSE}

Tmp := Integer(arr[row0]);

Integer(arr[row0]) := Integer(arr[row1]);

Integer(arr[row1]) := Tmp;

{$ENDIF}

end;

procedure TForm5.FormShow(Sender: TObject);

var

tmpArray: TAADouble;

I, J: Integer;

rowStr: string;

begin

SetLength(tmpArray, 10, 10);

rowStr := '';

for I := 0 to 9 do

for J := 0 to 9 do

tmpArray[I][J] := I * J;

for I := 0 to 9 do

begin

rowStr := '';

for J := 0 to 9 do

rowStr := rowStr + FloatToStr(tmpArray[I][J]) + ' ';

OutputDebugString(PWideChar(rowStr));

end;

SwapRows(tmpArray, 3, 4);

for I := 0 to 9 do

begin

rowStr := '';

for J := 0 to 9 do

rowStr := rowStr + FloatToStr(tmpArray[I][J]) + ' ';

OutputDebugString(PWideChar(rowStr));

end;

end;

end.

更多推荐

c语言交换二维数组的两行,如何交换二维数组的两行,为什么它会工作?

本文发布于:2024-02-06 07:24:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1747320.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   两行   它会   语言   工作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!