arrays - Trying to understand the usage of multidimension dynamic fields in C -


i'm trying learn c @ moment , have encountered task cannot solve on own. simple minesweeper implementation.

short description program should do: gets input files via command line, opens them 1 @ time, creates minesweeper field each , if multiple files opened program saves them formatted in 1 file. don't want format them 1 beneath other, want use dynamic 2 dimensional array, matrix expands without losing saved mines.

each input file has same build:

3\t5\n 0\t3\n 3\t7\n ... 

the first line indicates size of n x m matrix , following lines coordinates of mines.

here code far:

#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h>  typedef struct game{     int n;     int field[]; }game;   int read_files(char **input, int length); int make_field(char *input, int i, int *ptr); void increment(int i, int j, game); int is_inside_bounds(int i, int j, game); void free_field(); void safe_field();   int main(int argc, char **argv){      read_files(argv, argc);     return 0; }     //read_files reads files given programm via commandline  int read_files(char **input, int length){     int i= 1;      char string[20];              //input goes via fgets     int filecount = 0;            //counts amount of files     int fieldptr[100];           // int array purpose of memorizing size of gamefields create     int j = 0;      (; i< length-1; i++){         file *in;         if(!(in = fopen(input[i], "r"))){             fprintf(stderr, "file not found.\n");              err = 2;             return err;         }         while (fgets(string,20,in)){               make_field(string, filecount, fieldptr); //gives relevan info make_field function             j++;         }         fclose(in);         filecount++;     }     return 0; }    int make_field(char *string, int file, int *fieldptr){     struct g = null;//this problem     int g.field = null;//this     char delimiter[] = "\t\n";     //the input file cuts chars \n , \t     char *ptr;                     //needed strtok     file= file * 2;               //this used filehandling     ptr = strtok(string, delimiter);     int = atoi(ptr);           //cast int have gamefield size int     ptr = strtok(null, delimiter);     int b = atoi(ptr);       //cast int have gamefield size int       if (fieldptr[file] == 0 && file == 0){//init field         fieldptr[0] = a; //memorize first call         fieldptr [1] = b;         g.field = malloc(a * sizeof(int*));         if (null == field){             fprintf(stderr, "memory error.\n");             return 3;         }         for(int j = 0; j < a; j++){             g.field[j] = malloc(b * sizeof(int));             if(null==g.field[j]){ allocated                 fprintf(stderr, "memory error.\n");                 return 3;             }         }     }else if (fieldptr[file] == 0 && file != 0){ //if first line , not first file         field = realloc(g.field, * sizeof(int*));         if (null == field){             fprintf(stderr, "memory error.\n");             return 3;         }         for(int j = 0; j < a; j++){             field[j] = realloc(field[j], b * sizeof(int));             if(null==g.field[j])                 fprintf(stderr, "memory error.\n");                 return 3;             }         }         fieldptr[file] = a;      //filehandling memory         fieldptr[file+1] = b;      }else if(fieldptr[file] != 0 && file == 0){    //fill mines +indicators array         g.field[a][b] = -1;         int m;         (m = -1; m <= 1; m++) {             int n;             (n = -1; n <= 1; n++) {                 increment(a + m, (fieldptr[file+b]) + n, g);             }         }      }else if (fieldptr[file] != 0 && file != 0){     //fill mines +indicators bigger array         g.field[a][fieldptr[file+b]] = -1;          int m;         (m = -1; m <= 1; m++) {             int n;             (n = -1; n <= 1; n++) {                 increment(a + m, (fieldptr[file+b]) + n, g);             }         }     }     return field; //not sure return yet }    void increment(int i, int j, game *g) {       //increments neighbours of minefield     if (is_inside_bounds(i, j, g) && field[i][j] != -1) {         ++g.field[i][j];     } }  int is_inside_bounds(int i, int j, game *g) {         //checks if access of increment inside array     int nlines = sizeof(g.field) / sizeof(g.field[0][0]);     int ncolumns = sizeof(g.field[0]) / sizeof(field[0][0]);     return >= 0 && < nlines && j >=0 && j < ncolumns; } 

coming java, in eyes should work, don't how c works multidim arrays. can point me right direction?

edit:

gcc has following output:

error: invalid use of flexible array member    g.field = realloc(g.field, * sizeof(int*));  in function ‘increment’: note: abi of passing struct flexible array member has changed in gcc 4.4  void increment(int i, int j, game g) { 

so problem don't know how declare , use array right way.

struct g = null;//this problem int g.field = null;//this 

you right, problem

1) mean game g instead of struct g

2) can not use null without pointer:

game *g = null; /* ok */ 

3) can not access member (g.field) of uninitialized struct (g)

more problems:

g.field = malloc(a * sizeof(int*)); 

you can not use malloc flexible array member, need reserve space whole struct including size of flexible array member:

game *g = malloc(sizeof(*g) + (n * sizeof(int))); 

or better yet

game *g = malloc(sizeof(*g) + (n * sizeof(g->field[0]))); 

note sizeof(int) instead of sizeof(int *), want reserve space n ints not n pointers ints

or change field

typedef struct game {     int n;     int *field; } game; 

in order use malloc member directly.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -