2016-05-27 18 views
-2
double** makeit(int, int); 
void showit(double**, int, int, int); 

int main() 
{ 
    int i,j; 
    int x,y; 
    printf("x="); 
    scanf("%d",&x); 
    printf("y="); 
    scanf("%d",&y); 

    double (*mas2d)[x]; 
    mas2d=makeit(x,y); 
    printf("%4.0f ir %4.0f \n",mas2d[0][0],mas2d[1][0]); 
    showit(&mas2d, x, y); 

    return 0; 
} 

double** makeit(int x, int y) 
{ 
    double (*masp)[x]; 
    int i,j; 
    double skc; 
    masp= malloc((x*y)*sizeof(double)); 
    skc=1; 

    for (i=0;i<x;i++) 
    { 
     for (j=0;j<y;j++) 
     { 
      masp[i][j]=skc; 
      skc++; 
     } 
    } 
    return masp; 
} 

void showit(double** mas[], int x, int y) 
{ 
    int i,j; 
    printf("%4.0f ir %4.0f \n",mas[0][0],mas[1][0]); 
    printf("x===%d",x); 

    for(i=0;i<x;i++) 
    { 
     printf("\n"); 

     for(j=0;j<y;j++) 
     { 
      printf("%4.0f \n",mas[i][j]); 
     } 
    } 
} 

zu funktionieren Was ich 1. I Doppel Array mas2d in Funktion makeit dynamisch zuzuteilen. 2. Ich möchte den mas2d Array-Zeiger an die Funktion showit senden und dort ausdrucken.Passing Doppelzeiger (der dynamisch zugewiesenen Doppel Array)

Was ist das Problem ich mas2d Array Zeiger von main Funktion ohne Probleme drucken können, aber wenn ich es auf eigene Funktion showit, gebe ich kippe es nur die Arbeit ... Ich habe zu senden versucht, es als 3D-Zeiger und vielleicht 100 andere Möglichkeiten ohne Glück überhaupt.

+1

Hier rufen Sie zeigen wie diese 'ShowIt (& mas2d, x, y);' aber Sie erklären es so = >> 'void showit (doppelt **, int, int, int);', Warum? – Michi

+2

Wo sind 'makeit()' und 'showit()'? – alk

+0

Drücken Sie die Warnstufe Ihres Compilers hoch und korrigieren Sie den Code, bis keine weiteren Warnungen mehr ausgegeben werden. – alk

Antwort

1

Der sauberste Weg, dies zu tun ist, dass alle Instanzen der 2D den Typ double ** verwenden. Wenn Sie Ihre Zuordnungen vornehmen, müssen Sie zuerst ein Array von xdouble * zuordnen und dann für jede Zeile ydouble zuweisen.

double** makeit(int x, int y){ 
    double **masp; 
    int i,j; 
    double skc; 

    masp= malloc(x*sizeof(double *)); 
    skc=1; 

    for (i=0;i<x;i++) { 
     masp[i]= malloc(y*sizeof(double)); 
     for (j=0;j<y;j++){ 
      masp[i][j]=skc; 
      skc++; 
     } 
    } 

    return masp; 
} 
+0

danke !! Ich verstehe, dass das Zuweisen Ihres Weges der richtige Weg ist, aber trotzdem, ist es möglich, dies auf meine Art zu tun? vielleicht falsch vorbei oder smtg? – rokastokas

0

Versuchen Sie dies (siehe Details über die Änderungen als Kommentar zu der Quelle):

#include <stdlib.h> 
#include <stdint.h> 
#include <stdio.h> 

double (*makeit(size_t x, size_t y))[] /* Make it return what it creates, 
                 a pointer to an array of double. */ 
{ 
    double (*masp)[x]; 
    size_t i, j; /* Index arrays and address storage using size_t. */ 
    double skc = 1.; /* Always initialise on definition if possible; Use a double literal 
                   to initialise a double. */ 
    masp = malloc(y * sizeof *masp); /* Allocate y times what masp points to, 
                 that is an array of x doubles. */ 
    if (NULL == masp) /* Return is we got nothing. */ 
    { 
    return NULL; 
    } 
    /* skc = 1; */ /* Not needed any more (see definition of skc). */ 

    for (i = 0; i < x; i++) 
    { 
    for (j = 0; j < y; j++) 
    { 
     masp[i][j] = skc; 
     skc++; 
    } 
    } 
    return masp; 
} 

void showit(size_t x, size_t y, double (*mas)[x]) /* Pass the address of a VLA 
               after passing it primary dimension (x). */ 
{ 
    size_t i, j; /* Index arrays and address storage using size_t. */ 
    printf("%4.0f ir %4.0f \n",mas[0][0], mas[1][0]); 
    printf("x===%zu", x); /* Use the appropriate conversion specifier for size_t */ 

    for (i = 0; i < x; i++) 
    { 
    printf("\n"); 

    for (j = 0; j < y; j++) 
    { 
     printf("%4.0f \n", mas[i][j]); 
    } 
    } 
} 

int main(void) /* It ought to be this at least. */ 
{ 
    //int i, j; /* Unused. */ 
    size_t x,y; /* Index array and address storage using size_t. */ 

    printf("x="); 
    fflush(stdout); /* Make sure the output is shown. */ 
    scanf("%zu",&x); /* Use the appropriate specifier for size_t */ 

    printf("y="); /* Make sure the output is shown. */ 
    fflush(stdout); /* Use the appropriate specifier for size_t */ 
    scanf("%zu",&y); /* Make sure the output is shown. */ 

    double (*mas2d)[x] = makeit(x, y); /* Always initialise on definition if possible. */ 

    if (NULL == mas2d) /* Do error checking! */ 
    { 
    perror("makeit() failed"); 
    exit(EXIT_FAILURE); 
    } 

    printf("%4.0f ir %4.0f \n", mas2d[0][0], mas2d[1][0]); 

    showit(x, y, mas2d); /* Adjust order of parameters suiting the changes. */ 

    return 0; 
}