I have been trying to write a code for Conways Game of life, the thing is I need to generate a matrix of char filled with the elements * or o(o marks a dead cell,*marks an alive one).
The Rules:
For a cell that is alive:
Each cell with one or no neighbors dies, as if by loneliness.
Each cell with four or more neighbors dies, as if by overpopulation.
Each cell with two or three neighbors survives.
For a dead cell:
Each cell with three neighbors becomes alive.
OK i designed a function called Generation that receives a pointer to the matrix,a number of columns and lines,and the number of generations.(the number of generations represents how many times I have to aply the rules above for the matrix.
The only way for the rules to apply simultaneously is to create another empty matrix in which I record the modifications and then pass that one.
Apparently I'm unable to create the matrix in order to store the new Information and the pass it to the function.
I only want to display the matrix for the last generation.

char** generation(char **z,int T,int n,int m)
    if (T==0)  return z;
 int j,i;
 char sec[1000][1000];  //here I'm tryng to create a  matrix of char
 for (i=1;i<=n;i++) //this where I try to initialise it
      for (j=0;j<m;j++)
   for (i=1;i<=n;i++)
      for (j=0;j<m;j++)
    //the code where I apply the rules

z=sec;//Here is where the warning apears
return generation(z,T-1,n,m);/*I'm trying to pass the new modified matrix to the next generation*/

The warning I receive is: assignment from incompatible pointer type.

Recommended Answers

All 3 Replies

There are a couple of things wrong (probably more).
1. You can't return an array, no matter how you declare your return type, because you're only returning a pointer to the array, and the array goes out of scope when the function exits. You can get round this by making the array static.

#include <stdio.h>

char (*foo(void))[10] {
    static char array[10][10];
    return array;

int main( ) {
    char (*arr)[10] = foo();
    return 0;

2. Notice that the syntax for returning a pointer to a 2D array is really hairy. You don't want this if you can avoid it.

> char** generation(char **z,int T,int n,int m) So how did you declare the array you passed INTO the function (which would become the z parameter). If you did something like char oldgen[1000][1000] , then that would be wrong as well.
Whilst char arr[n] and char * can be used interchangeably as formal function parameters when passing a 1D array, the same cannot be said for 2D arrays. To pass char oldgen[1000][1000] to this function, you would need char (*z)[1000] 3. Which brings up the next point. 1000x1000 is 1MB of array space. If you declare this as a local, then it will likely blow up the default stack allocation of most machines. And since you have 'old' and 'new' generations' worth, that's twice as much.

So, rather than trying to return an array, look at something like strcpy, which takes parameters of where to copy from, and where to copy to.

#include <stdio.h>

void generation(char (*oldgen)[100], char (*newgen)[100], int T, int n, int m ) {

int main( ) {
    char oldgen[100][100], newgen[100][100];
    generation( oldgen, newgen, 1, 100, 100 );
    return 0;

I am so stupid! the problem is that I thought it was more difficult to declare a 2D array of char than int because when i printed the array of matrix here is how I wrote the code

for (i=0;i<nl;i++)

for (j=0;j<nc;j++)
printf("%c ",a[i][j]); //the extra space after %c made my matrix look akward


Here is the code I used because of that:

int main()
char **z;
char *mat[1000];
char sir[1000];	
int  n,i,j,m,T;
printf("nr of generationsi:");
printf("nr of lines:");
printf("nr of cols: ");
for (i=0;i<=n;i++)
for (i=0;i<=n;i++)

return 0;

I wasn't passing an array I was passing it's adress.
It is because I went to the university of computer science without learning C before and here they don't teach you anything they only expect results,I had to learn by myself everything in a very short time and as you can see I'm not very good at it.
Thank you for your help.I may ask something else later so I m not marking it as solved as I may have further problems.

I may ask something else later so I m not marking it as solved as I may have further problems.

When you do, please consider formatting your code. It will help you in the long run. It will help us in the short run. :icon_wink:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.