-3

for a school project, we have to create a maze in c, I'm a beginner in that language, so I'm stuck at the very beginning: creating an array dynamically...

I read about malloc and calloc, and tried to use it but with no success...

here is my code:

/* define maze structure */

typedef struct _maze_t {
    int m, n;
    char **array;
} Maze;

void constructorArray(int m,int n)
{ 
    array = malloc(m * sizeof *array);

    for(i=0;i<m;i++){
        array[i]=(char *) malloc(n*sizeof(char)); 
    }
    array = calloc(m, sizeof *array);

    for (i=0; i<m; i++){
      array[i] = calloc(n, sizeof *(array[i]));
    }   
}
void createMaze(int ncolumn, int mlign)
{    
    int m=mlign;
    int n=ncolumn;
    int counter=0; 

    constructorArray(m,n) ;     

    char **array;            

    for(i=0;i<m;i++)
    {
        for(y=0;y<n;y++)
        {
            array[i][y]=counter;
            counter++;
        }
    }
}

int main()
{
    createMaze(100,100);    
    return 0;
}

Could someone explain to me how to do it right?

D Sam
  • 77
  • 1
  • 2
  • 11
  • 1
    1) I see no `main()` 2) missing return type is a sign of _bad_ source of learning. – Sourav Ghosh Nov 29 '17 at 09:23
  • 1
    `array=(char **) malloc(1m*sizeof(char *));` should be `array = malloc(m * sizeof *array);`. That `1m` is a syntax error. – unwind Nov 29 '17 at 09:24
  • 1
    Please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), and learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Nov 29 '17 at 09:24
  • Given typos like `1m`, it appears that this code was re-typed by hand rather than copy/pasted. As such, it only approximates the code that was actually run. That means that your results will not be reliably replicated. – Tom Karzes Nov 29 '17 at 09:28
  • I added the main and changed the typo, thanks – D Sam Nov 29 '17 at 09:30
  • Please post real code `constructorArray(m,n) {..` is not C. – Jabberwocky Nov 29 '17 at 09:31
  • Please learn how to indent your code. – Jabberwocky Nov 29 '17 at 09:38
  • @MichaelWalz `constructorArray(m,n)` it is valid K&R C. So if his compiler is really old... – JeremyP Nov 29 '17 at 09:43
  • @JeremyP OK, your're probably right, but normally nobody should have written K&R for the last 10 (or even 20?) years. – Jabberwocky Nov 29 '17 at 09:45
  • 1
    @MichaelWalz 19 years ago I was arguing at the company I worked for that we should stop using the weird parameter macros that we had to allow our code to compile in both K&R and ANSI C. So yes, people shouldn't have been using it for 20 years IMO. – JeremyP Nov 29 '17 at 09:50

1 Answers1

2

It seems you have some believes that are incorrect.

First, you do no correctly declare your C functions:

constructorArray(m,n)

should be:

void constructorArray(int m, int n)

Then it seems that you think a constructor will be called automatically in C, which is not so, so simply writing array[m][n] in CreateMaze won't work. You should write:

char **array;    // it will be allocated dynamically

and then have your function:

char **constructorArray(int m, int n)
{ 
    char **array= malloc(m*sizeof(char *));

    for(int i=0; i<m; i++)
        array[i]= malloc(n*sizeof(char));

    return array;
}

which you now can call as:

char **array= constructorArray(m, n);

Note: your use of the array suggests an array of ints could be more sutiable.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41