Warum Access violation?



  • int main() {
    maze mymaze, *mazezeiger;
    int i,j,k,l;
    int **feldpointer;
    mymaze.width = 79;
    mymaze.height = 25;
    
    //=====================FELD ANLEGEN======================//
    ANLEGEN(feldpointer);
    //=======================================================//
    
    //=====================FELD BELEGEN======================//
    for(i=0;i<def_hoehe;i++){
          for(j=0;j<def_breite;j++){
                feldpointer[i][j] = 1;
          }
    }
    //=======================================================//
    

    das unterprogramm sieht so aus:

    void ANLEGEN (int **doppelpointer){
    int i,j;
    doppelpointer = malloc(def_hoehe*sizeof(int));
    
      for(i=0;i<def_hoehe;i++){
        doppelpointer[i]=malloc(def_breite*sizeof(int));
      }
    }
    

    Beim compilieren mit borland 6 c++ builder krieg ich ne fehlermeldung wegen einer Accessviolation in der zeile:

    feldpointer[i][j] = 1;
    

    woran liegt das?



  • könntest du mal den kompletten quelltext posten?

    Da bleiben nämlich fragen offen: was ist def_breite/hoehe etc...



  • labyrinth.c:

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <math.h>
    #include "maze.h"
    
    int main() {
    maze mymaze, *mazezeiger;
    int i,j,k,l;
    int **feldpointer;
    mymaze.width = 79;
    mymaze.height = 25;
    
    //=====================FELD ANLEGEN======================//
    ANLEGEN(feldpointer);
    //=======================================================//
    
    //=====================FELD BELEGEN======================//
    for(i=0;i<def_hoehe;i++){
          for(j=0;j<def_breite;j++){
                feldpointer[i][j] = 1;
          }
    }
    //=======================================================//
    
    //=====================FELD AUSGEBEN======================//
    for(i=0;i<mymaze.height;i++){
          for(j=0;j<mymaze.width;j++){
                printf("%c",feldpointer[i][j]);
          }
    printf("\n");
    }
    //=======================================================//
    
      getch();
    
    return;
    
    }
    

    maze.c

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <math.h>
    #include "maze.h"
    
    void ANLEGEN (int **doppelpointer){
    int i,j;
    doppelpointer = malloc(def_hoehe*sizeof(int));
    
      for(i=0;i<def_hoehe;i++){
        doppelpointer[i]=malloc(def_breite*sizeof(int));
      }
    }
    

    maze.h:

    #define def_breite 79
    #define def_hoehe 25

    struct maze_s{
    int width;
    int height;
    }

    typedef maze_s, maze;

    void ANLEGEN (int **doppelpointer);



  • [quote="egal
    maze.c

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <math.h>
    #include "maze.h"
    
    void ANLEGEN (int **doppelpointer){
    int i,j;
    doppelpointer = malloc(def_hoehe*sizeof(int));
    
      for(i=0;i<def_hoehe;i++){
        doppelpointer[i]=malloc(def_breite*sizeof(int));
      }
    }
    

    Richtig sollte die Funktion so aussehen:

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <math.h>
    #include "maze.h"
    
    void ANLEGEN (int **doppelpointer){
    int i,j;
    doppelpointer = malloc(def_hoehe*sizeof(int*));// int* anstatt int!
    
      for(i=0;i<def_hoehe;i++){
        doppelpointer[i]=malloc(def_breite*sizeof(int));
      }
    }
    

    Einmal ausprobieren bitte!


Anmelden zum Antworten