[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] tilgaa 2-dim array



sslug@sslug wrote:
> hvordan erklærer jeg et 2-dim-array uden fast længde og sender det med et
> funktionskald, og tilgår det?

Er matrixen Nx x Ny stor så kan du først allokere
ram:

double *matrix=(double*)calloc(Nx*Ny*sizeof(double));

Bemærk at du vil ikke blot allokere Nx*Ny bytes,
fordi en dobbelt variable fylder en del bytes,
8 så vidt jeg husker, men det kan være platform
afhængigt og alt muligt. Men sizeof(double) returnere
altid størrelsen.

Denne er 1D, men du kan folde et 2D index til 1D
ved at konvertere fra i,j til index=i+j*Nx;
hvor i løber fra 0,..,Nx-1, og j fra 0,..,Ny-1.

Skal du overføre matricen så kan du 
double GetIndex(int i,int j,double* matrix)
{
  int index=i+Nx*j;
  if (index<0 || index>=Nx*Ny) printf("fejl!\n");

  return matrix[index];
}

Eller hvis du vil skrive til den.

SetIndex(int i,int j,double d,double** matrix)
{
  int index=i+Nx*j;
  if (index<0 || index>=Nx*Ny) printf("fejl!\n");

  (*matrix)[index]=d;
}

Og så kalde med SetIndex(0,0, 42.0 ,&m);

-- 
          Mvh. Carsten Svaneborg
       http://www.softwarepatenter.dk
 hvor fremtidens idemonopoler bekæmpes idag.


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:09 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *