Pointer as permater

Okay so I am trying to have a function fill a 2d char array with values, but I keep getting an error of incompatible pointer type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>

void plusone(int *p) {
  ++*p;
}

void generateboard(char *array[10][10]) {
  for (int y = 0; y < 10; ++y) {
    for (int x = 0; 2 < 10; ++x) {
      *array[y][x] = '*';
    }
  }
}

void printboard(char array[10][10]) {
  for (int y = 0; y < 10; ++y) {
    for (int x = 0; 2 < 10; ++x) {
      printf("%c", array[y][x]);
    }
  }
}

int main(void) {
  int i = 10;
  char board[10][10];
  printf("i is %d\n", i);

  plusone(&i);
  printf("i is now %d\n", i);

  generateboard(&board[0][0]);
  printboard(board);

  return 0;
}


The error is only happening with the 2d char array.
Can someone help me pass the pointer to the function properly?
Last edited on
board is an array of chars, so board[0][0] is a char and &board[0][0] is a char*, rather than char* array[10][10] (which is a 10*10 array of char pointers, by the way).
You can make it work the same way as printboard. Arrays are passed by reference. If you think that you need a pointer to an array, though, the argument is char (*array)[10][10] and you need to pass &board and access elements with (*array)[x][y], I think..
I will try that, I am poiting to the array because I want the changes of the array to make it back to the main function. The generateboard function will only change a copy of the original array if I just pass it in like with the printfboard function.
Now it compiles without errors but the application itself crashes, here is the new code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>

void plusone(int *p) {
  ++*p;
}

void generateboard(char (*array)[10][10]) {
  for (int y = 0; y < 10; ++y) {
    for (int x = 0; 2 < 10; ++x) {
      *array[y][x] = '*';
    }
  }
}

void printboard(char array[10][10]) {
  for (int y = 0; y < 10; ++y) {
    for (int x = 0; 2 < 10; ++x) {
      printf("%c", array[y][x]);
    }
  }
}

int main(void) {
  int i = 10;
  char board[10][10];
  printf("i is %d\n", i);

  plusone(&i);
  printf("i is now %d\n", i);

  generateboard(&board);
  printboard(board);

  return 0;
}
Noticed that I didn't use (*array)[y][x]. Changeing it didn't stop the crashing though
closed account (z05DSL3A)
for (int x = 0; 2 < 10; ++x) would be a big problem.

You will also want to deference array before offsetting it; (*array)[y][x] = '*';

NB: array is not a very good name for the array.
Last edited on
OMG how did I ever miss that!
Now the program is still crashing and the output is horrable. I get really wierd symbols filling the console and my computer starts beeping. Never had that before
closed account (z05DSL3A)
Did you notice my edit about dereferencing array?
Using a different compiler I am warned about an infinite loop and unreachable code. But I can't figure out where that is.
Here is my up-to-date code. Yes I know that array is not a very good variable name, but this is just a test program because I am trying to get this to work for a different application.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>

void generateboard(char (*array)[10][10]) {
  for (int y = 0; y < 10; ++y) {
    for (int x = 0; x < 10; ++x) {
      (*array)[y][x] = '*';
    }
  }
}

void printboard(char array[10][10]) {
  for (int y = 0; y < 10; ++y) {
    for (int x = 0; 2 < 10; ++x) {
      printf("%c", array[y][x]);
    }
  }
}

int main(void) {
  char board[10][10];

  generateboard(&board);
  printboard(board);

  return 0;
}


EDIT:Wow i could have sworn I fixed the for loop and yet above it is still their
Last edited on
closed account (z05DSL3A)
Here is an edit of your code that should work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>

void plusone(int *p) {
  ++*p;
}


void generateboard(char (*board)[10][10]) {
  for (int y = 0; y < 10; ++y) {
    for (int x = 0; x < 10; ++x) {  // Fixed
      (*board)[y][x] = '*';         // Fixed
    }
  }
}

void printboard(char board[10][10]) {
  for (int y = 0; y < 10; ++y) {
    for (int x = 0; x < 10; ++x) {  //Fixed
      printf("%c", board[y][x]);
    }
  }
}

int main(void) {
  int i = 10;
  char board[10][10];
  printf("i is %d\n", i);

  plusone(&i);
  printf("i is now %d\n", i);

  generateboard(&board);
  printboard(board);

  return 0;
}
Thankyou it works now. I am guessing that i had copied the first for loop with the the problem you pointed out and didn't notice I had that problem in two places.
Topic archived. No new replies allowed.