Thread: passing array of integer to void function which modifies it

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    1

    passing array of integer to void function which modifies it

    Hi, I'm trying to write a minesweeper C code, this is how I started. I sadly can't figure out how to pass the field array to the create_field function. I've tried pointers,&,void brackets,no brackets,one void bracket and one with a dimension,and more, but the compiler returns lots of errors, such as this thing is neither a pointer nor an array nor an integer, or segmentation fault, or expected declaration before some input or bracket...I honestly can't see the error, thanks in advance!
    P.S. I first created the field directly in the main() and then it worked.In order to make the program more elegant to see, or at least the main() more concise, I tried to subdivide it in several functions, of which this is the second, and the first problematic.
    Code:
    #include<stdio.h>
    #include<time.h>
    #include<stdlib.h>
    main() {
    int A,B,mines; // rows and columns and number of mines
    askNumbers(&A,&B,&mines);
    
    int field[A][B];
    create_field(A,B,mines, FIELD????); // how is this to be written?
    
    ...
    
    }
    
    void create_field(P,Q,mines,FIELD??) { // same question
      int i,j;
      srand(time(NULL));
      for (i=0; i<P; i++)
        for (j=0; j<Q; j++)
          field[i][j] = 0; // unmined field
    
      do {
        for (i=0; i<P; i++)
          for (j=0; j<Q; j++)
        if (0==rand() && mines && 9!=field[i][j]) {
          field[i][j] = 9; // puts mines
          mines--;
        }
      } while (mines);
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to declare your array in main(), then you pass it around to all the other functions. Every array and other variables in C is case sensitive, so watch your capitalization.

    An example:

    Code:
    #include <stdio.h>
    
    #define MAX 4
    
    void status(int swapped[MAX][MAX]);
    
    int main(void) {
    
       int swapped[MAX][MAX]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
       //other code in here
       //try and work out what the call to the status function, would be, OK
     
       return 0
    }
    void status(int swapped[MAX][MAX]) {
       int i, j;
          
       //swap status:
       for(i=0;i<MAX;i++) {
          printf("person: %2d: ",i);
          for(j=0;j<MAX;j++)
             printf(" %2d ",swapped[i][j]);
          printf("\n");
       }
    }
    Last edited by Adak; 12-23-2011 at 05:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-15-2010, 09:14 AM
  2. Replies: 2
    Last Post: 02-25-2010, 04:20 PM
  3. Passing a variable in void to another void function
    By stevedawg85 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 06:17 PM
  4. Replies: 4
    Last Post: 05-08-2005, 10:12 AM
  5. Passing a double to function with void *
    By foniks munkee in forum C Programming
    Replies: 5
    Last Post: 03-08-2002, 10:09 PM