My Producer/Consumer C-Code is not giving any real output - c

I've recently changed my OS from Windows to NitruxOS, and I'm currently struggling a bit with compiling my code.
I tried compiling with:
gcc main.c -Wall -pedantic -std=c99 -D_XOPEN_SOURCE=700 -pthread
but I'm not getting any real output; only the following warning:
*main.c: In function ‘main’: main.c:29:44: warning: ISO C forbids passing argument 3 of ‘pthread_create’ between function pointer and ‘void *’ [-Wpedantic] 29 | pthread_create(&producer[i], NULL, (void*)producer, (void*)&num[i]);
| ^~~~~~~~~~~~~~~ In file included from main.c:4: /usr/include/pthread.h:200:15: note: expected ‘void * (*)(void *)’ but argument is of type ‘void *’ 200 | void *(*__start_routine) (void *),
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~ main.c:34:44: warning: ISO C forbids passing argument 3 of ‘pthread_create’ between function pointer and ‘void *’ [-Wpedantic] 34 | pthread_create(&consumer[i], NULL, (void*)consumer, (void *)&num[i]);
| ^~~~~~~~~~~~~~~ In file included from main.c:4: /usr/include/pthread.h:200:15: note: expected ‘void * (*)(void *)’ but argument is of type ‘void *’ 200 | void *(*__start_routine) (void *),*
My code is as follows:
#include <stdlib.h>
#include <stdio.h>
#include <semaphore.h>
#include <pthread.h>
#define max 10
#define bufferlen 10
sem_t voll;
sem_t leer;
int input = 0;
int output = 0;
int buffer[bufferlen];
pthread_mutex_t mutex;
int main()
{
pthread_t producer[bufferlen], consumer[bufferlen];
pthread_mutex_init(&mutex, NULL);
sem_init(&voll, 0, 0);
sem_init(&leer, 0, bufferlen);
int num[max] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for(int i = 0; i < 10; i++)
{
pthread_create(&producer[i], NULL, (void*)producer, (void*)&num[i]);
}
for(int i = 0; i < 10; i++)
{
pthread_create(&consumer[i], NULL, (void*)consumer, (void *)&num[i]);
}
for(int i = 0; i < 10; i++)
{
pthread_join(producer[i], NULL);
}
for(int i = 0; i < 10; i++)
{
pthread_join(consumer[i], NULL);
}
pthread_mutex_destroy(&mutex);
sem_destroy(&leer);
sem_destroy(&voll);
return 0;
}
void *producer(void *pptr)
{
int item = 0;
for (int i = 0; i < max; i++)
{
sem_wait(&leer);
pthread_mutex_lock(&mutex);
buffer[input] = item;
printf("Der Producer No. %d hat %d an Stelle %d eingefügt\n", *((int*)pptr), buffer[input], input);
input = (input + 1) % bufferlen;
pthread_mutex_unlock(&mutex);
sem_post(&voll);
}
return NULL;
}
void *consumer(void *cptr)
{
for(int i = 0; i < max; i++)
{
sem_wait(&voll);
pthread_mutex_lock(&mutex);
int item = buffer[output];
printf("Der Consumer No. %d hat %d von Stelle %d entfernt\n", *((int*)cptr), item, output);
output = (output + 1) % bufferlen;
pthread_mutex_unlock(&mutex);
sem_post(&leer);
}
return NULL;
}

Related

How to parallelize knight tour problem with Pthreads?

I am trying to paralelize knight tour problem with pthreads but I am getting some errors. I am very new at c language. I guess the code has pointer related problem. can you give me some clue about how to solve it?
I am trying to apply paralel recursive approach. First i was supposed to create 8 threads, then based on moves, new threads are created. perDepth is the limit of creating new threads. i am planing to rewrite this code with multple number of "perDepth"s and compare performance based on perDepth.
\#define HAVE_STRUCT_TIMESPEC
\#include \<pthread.h\>
\#include \<stdio.h\>
\#include \<time.h\>
\#define N 5
int perDepth = 1;
pthread_t thread\[N\];
int count = 0;
int is_valid(int i, int j, int sol\[N + 1\]\[N + 1\])
{
if (i \>= 1 && i \<= N && j \>= 1 && j \<= N && sol\[i\]\[j\] == -1)
return 1;
return 0;
}
int knight_tour(int sol\[N + 1\]\[N + 1\], int i, int j, int step_count, int x_move\[\], int y_move\[\])
{
if (step_count == N \* N)
{
count++;
return 1;
}
int k;
for (k = 0; k \< 8; k++)
{
int next_i = i + x_move\[k\];
int next_j = j + y_move\[k\];
if (is_valid(i + x_move[k], j + y_move[k], sol))
{
sol[next_i][next_j] = step_count;
if (step_count <= perDepth)
{
pthread_create(&thread[k], NULL, knight_tour, NULL);
}
else
{
knight_tour(sol, next_i, next_j, step_count + 1, x_move, y_move);
sol[i + x_move[k]][j + y_move[k]] = -1; // backtracking
}
}
}
return 0;
}
int start_knight_tour()
{
int sol[N + 1][N + 1];
int i, j;
for (i = 1; i <= N; i++)
{
for (j = 1; j <= N; j++)
{
sol[i][j] = -1;
}
}
int x_move[] = {2, 1, -1, -2, -2, -1, 1, 2};
int y_move[] = {1, 2, 2, 1, -1, -2, -2, -1};
sol[1][1] = 0; // placing knight at cell(1, 1)
if (knight_tour(sol, 1, 1, 1, x_move, y_move))
{
for (i = 1; i <= N; i++)
{
for (j = 1; j <= N; j++)
{
printf("%d\t", sol[i][j]);
}
printf("\n");
}
return 1;
}
return 0;
}
int main()
{
clock_t t;
t = clock();
pthread_create(&thread\[N\], NULL, start_knight_tour, NULL);
pthread_join(thread\[N\], NULL);
t = clock() - t;
double time_taken = ((double)t) / CLOCKS_PER_SEC; // in seconds
printf("start_knight_tour() took %f seconds to execute \\n", time_taken);
printf("%d\\n", count);
return 0;
}
error message
main.c: In function ‘knight_tour’:
main.c:38:50: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type \[-Wincompatible-pointer-types\]
38 | pthread_create(&thread\[k\], NULL, knight_tour, NULL);
| ^\~\~\~\~\~\~\~\~\~\~
| |
| int (*)(int (*)\[6\], int, int, int, int *, int )
In file included from main.c:2:
/usr/include/pthread.h:204:36: note: expected ‘void \* (*)(void *)’ but argument is of type ‘int (*)(int ()\[6\], int, int, int, int \*, int *)’
204 | void (*\__start_routine) (void *),
| \~\~\~\~\~\~\~\~^\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~
main.c: In function ‘main’:
main.c:90:38: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type \[-Wincompatible-pointer-types\]
90 | pthread_create(&thread\[N\], NULL, start_knight_tour, NULL);
| ^\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~
| |
| int (*)()
In file included from main.c:2:
/usr/include/pthread.h:204:36: note: expected ‘void \* ()(void *)’ but argument is of type ‘int (*)()’
204 | void *(*\__start_routine) (void \*),
| \~\~\~\~\~\~\~\~^\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~

Sorting an Array with Selection Sort

#include<stdio.h>
#include <time.h>
int selectionSort(int data[], int count)
{
int a,b;
int tmp;
int minimum;
for(a=0; a<count-1;a++){
minimum = a;
for(b=a+1;b<count;b++){
if(data[minimum]>data[b]) {
minimum = b;
}
}
tmp = data[a];
data[a]=data[minimum];
data[minimum]=tmp;
}
return 0;
}
int main(){
int randomarray[10];
int i;
int k;
srand(time(NULL));
for(i=1; i<=10; i++){
randomarray[i]=rand()%100;
selectionSort(randomarray[i], k = 10 );
printf("%d\n",randomarray[i]);
}
return 0;
}
I am trying to create a program that sorts an array of random integers. I think there is a problem with the declaration of my function, but I am not used to using C. The errors are shown below:
semihosting_example.c:13:5: warning: implicit declaration of function 'srand' [-Wimplicit-function-declaration]
semihosting_example.c:15:5: warning: implicit declaration of function 'rand' [-Wimplicit-function-declaration]
semihosting_example.c:16:5: warning: passing argument 1 of 'selectionSort' from incompatible pointer type [enabled by default]
semihosting_example.c:6:6: note: expected 'int *' but argument is of type 'int (*)[10]'ing_example.c:6:6: note: expected 'int *' but argument is of type 'int (*)[10]
Here's the updated code with the appropriate HEADERS. I am trying to create a function with an array that calls the function with an array:
#include<stdio.h>
#include <time.h>
#include<stdlib.h>
int selectionSort(int (*)[10], int count)
{
int a,b;
int tmp;
int minimum;
for(a=0; a<count-1;a++){
minimum = a;
for(b=a+1;b<count;b++){
if(data[minimum]>data[b]) {
minimum = b;
}
}
tmp = data[a];
data[a]=data[minimum];
data[minimum]=tmp;
}
return 0;
}
int main(){
int randomarray[10];
int i;
int k;
srand(time(NULL));
for(i=1; i<=10; i++){
randomarray[i]=rand()%100;
selectionSort(randomarray[i], k = 10 );
printf("%d\n",randomarray[i]);
}
return 0;
}
Here's a revised version of your program:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
static void selectionSort(int data[], int count)
{
for (int a = 0; a < count - 1; a++)
{
int minimum = a;
for (int b = a + 1; b < count; b++)
{
if (data[minimum] > data[b])
minimum = b;
}
int tmp = data[a];
data[a] = data[minimum];
data[minimum] = tmp;
}
}
int main(void)
{
int randomarray[10];
srand(time(NULL));
for (int i = 0; i < 10; i++)
randomarray[i] = rand() % 100;
printf("Before:\n");
for (int i = 0; i < 10; i++)
printf("%d: %d\n", i, randomarray[i]);
selectionSort(randomarray, 10);
printf("After:\n");
for (int i = 0; i < 10; i++)
printf("%d: %d\n", i, randomarray[i]);
return 0;
}
When run, it produces output such as:
Before:
0: 73
1: 63
2: 73
3: 80
4: 28
5: 19
6: 63
7: 96
8: 82
9: 46
After:
0: 19
1: 28
2: 46
3: 63
4: 63
5: 73
6: 73
7: 80
8: 82
9: 96
Note that it prints the array before sorting as well as afterwards so that you can tell whether it sorted correctly.
The code doesn't use k; it declares variables as late as possible.
If you replace the line:
selectionSort(randomarray, 10);
with:
selectionSort(&randomarray, 10);
you get the error messages:
$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes \
> sort53.c -o sort53
sort53.c: In function ‘main’:
sort53.c:32:19: error: passing argument 1 of ‘selectionSort’ from incompatible pointer type [-Werror=incompatible-pointer-types]
32 | selectionSort(&randomarray, 10);
| ^~~~~~~~~~~~
| |
| int (*)[10]
sort53.c:5:31: note: expected ‘int *’ but argument is of type ‘int (*)[10]’
5 | static void selectionSort(int data[], int count)
| ~~~~^~~~~~
$
That's from GCC 10.1.0, but it is very similar to the error messages posted in the question. If you instead replace the call with:
selectionSort(randomarray[0], 10);
then the error messages are:
sort53.c: In function ‘main’:
sort53.c:32:30: error: passing argument 1 of ‘selectionSort’ makes pointer from integer without a cast [-Werror=int-conversion]
32 | selectionSort(randomarray[0], 10);
| ~~~~~~~~~~~^~~
| |
| int
sort53.c:5:31: note: expected ‘int *’ but argument is of type ‘int’
5 | static void selectionSort(int data[], int count)
| ~~~~^~~~~~
cc1: all warnings being treated as errors

C - Calculating statistical values from command line using multithreaded programing

I'm a beginner working on a C practice program that can calculate various statistical values for a list of numbers read in from the command line. But when trying to compile my code in Linux, I get a handful of errors that I'm not sure how to interpret, and was wondering if I could get some help from those more familiar with the C language than myself. I've included both my code and the errors below.
My code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int *values;
int size;
double avg=0.0;
int minValue=0;
int maxValue=0;
void *runAvg(void *param);
void *runMin(void *param);
void *runMax(void *param);
int main (int argc, char *argv[]){
if(argc != 2){
fprintf(stderr, "usage: %s <integer value>\n", argv[0]);
}
int i;
for(i=1; i < argc; i++){
values=&(atoi(argv[i]));
size++;
}
pthread_t avgPt[size];
pthread_t minPt[size];
pthread_t maxPt[size];
pthread_attr_t attr;
//create threads
pthread_create(&avgPt, &attr, runAvg, values);
pthread_create(&minPt, &attr, runMin, values);
pthread_create(&maxPt, &attr, runMax, values);
//wait for threads to exit
pthread_join(avgPt, NULL);
pthread_join(minPt, NULL);
pthread_join(maxPt, NULL);
//print results of threads
printf("\n Average: %f \n",avg);
printf("\n Minimum: %d \n",minValue);
printf("\n Maximum: %d \n",maxValue);
}
void *runAvg(void *param){
int sum=0;
int i=0;
int *values;
values=(int*)param;
for(i=0;i<size; i++) sum += values[i];
avg = sum / (double)size;
pthread_exit(0);
}
void *runMin(void *param){
int i=0;
int *values;
values=(int*)param;
minValue = values[0];
for(i=0;i<size;i++){
if(values[i]<minValue){
minValue=values[i];
}
}
pthread_exit(0);
}
void *runMax(void *param){
int i=0;
int *values;
values=(int*)param;
maxValue=values[0];
for(i=0;i<size;i++){
if(values[i] > maxValue){
maxValue = values[i];
}
}
pthread_exit(0);
}
My compile errors:
423assign.c:20: error: lvalue required as unary ‘&’ operand
423assign.c:30: warning: passing argument 1 of ‘pthread_create’ from
incompatible pointer type
/usr/include/pthread.h:227: note: expected ‘pthread_t * __restrict__’
but argument is of type ‘pthread_t (*)[(unsigned int)(size)]’
423assign.c:31: warning: passing argument 1 of ‘pthread_create’ from
incompatible pointer type
/usr/include/pthread.h:227: note: expected ‘pthread_t * __restrict__’
but argument is of type ‘pthread_t (*)[(unsigned int)(size)]’
423assign.c:32: warning: passing argument 1 of ‘pthread_create’ from
incompatible pointer type
/usr/include/pthread.h:227: note: expected ‘pthread_t * __restrict__’
but argument is of type ‘pthread_t (*)[(unsigned int)(size)]’
423assign.c:34: warning: passing argument 1 of ‘pthread_join’ makes
integer from pointer without a cast
/usr/include/pthread.h:244: note: expected ‘pthread_t’ but argument is
of type ‘pthread_t *’
423assign.c:35: warning: passing argument 1 of ‘pthread_join’ makes
integer from pointer without a cast
/usr/include/pthread.h:244: note: expected ‘pthread_t’ but argument is
of type ‘pthread_t *’
423assign.c:36: warning: passing argument 1 of ‘pthread_join’ makes
integer from pointer without a cast
/usr/include/pthread.h:244: note: expected ‘pthread_t’ but argument is
of type ‘pthread_t *’
Your thread functions were okay.
One issue was not allocating an int array to contain the values from atoi.
Also, your pthread_t variables should not be arrays. And your attr value is never initialized and not used.
Here's a cleaned up version with the bugs annotated and fixed [please pardon the gratuitous style cleanup]:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int *values;
int size;
double avg = 0.0;
int minValue = 0;
int maxValue = 0;
void *runAvg(void *param);
void *runMin(void *param);
void *runMax(void *param);
int
main(int argc, char **argv)
{
// NOTE/BUG: this should be "< 2" and not "!= 2"
if (argc < 2) {
fprintf(stderr, "usage: %s <integer value>\n", argv[0]);
exit(1);
}
int i;
--argc;
++argv;
// NOTE/BUG: we must allocate an int array of sufficient size
values = calloc(argc,sizeof(int));
for (i = 0; i < argc; i++) {
values[i] = atoi(argv[i]);
size++;
}
// NOTE/BUG: these should _not_ be arrays
pthread_t avgPt;
pthread_t minPt;
pthread_t maxPt;
// NOTE/BUG: this is unitialized and it's not set to anything, so the
// pthread_create 2nd argument can [and should be] NULL
#if 0
pthread_attr_t attr;
#endif
// create threads
#if 0
pthread_create(&avgPt, &attr, runAvg, values);
pthread_create(&minPt, &attr, runMin, values);
pthread_create(&maxPt, &attr, runMax, values);
#else
pthread_create(&avgPt, NULL, runAvg, values);
pthread_create(&minPt, NULL, runMin, values);
pthread_create(&maxPt, NULL, runMax, values);
#endif
// wait for threads to exit
pthread_join(avgPt, NULL);
pthread_join(minPt, NULL);
pthread_join(maxPt, NULL);
// print results of threads
printf("\n Average: %f \n", avg);
printf("\n Minimum: %d \n", minValue);
printf("\n Maximum: %d \n", maxValue);
}
void *
runAvg(void *param)
{
int sum = 0;
int i = 0;
int *values;
values = param;
for (i = 0; i < size; i++)
sum += values[i];
avg = sum / (double) size;
return (void *) 0;
}
void *
runMin(void *param)
{
int i = 0;
int *values;
values = param;
minValue = values[0];
for (i = 0; i < size; i++) {
if (values[i] < minValue)
minValue = values[i];
}
return (void *) 0;
}
void *
runMax(void *param)
{
int i = 0;
int *values;
values = param;
maxValue = values[0];
for (i = 0; i < size; i++) {
if (values[i] > maxValue)
maxValue = values[i];
}
return (void *) 0;
}

Error returning 2d array from a function in c

I need help please;
this is the sample code:
#include <stdio.h>
const int n = 4;
const int m = 4;
void display(int arr[n][m]){
for(int i=0; i<n; i++) {
for(int j=0;j<m;j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
int *constrast(int arr[n][m])
{
int temp[n][m];
int max = 25;
int min = 10;
int uBound = 255;
int lBound = 0;
for(int i=0; i<n; i++) {
for(int j=0;j<m;j++) {
temp[i][j] = ((arr[i][j] - max) *((int)(uBound-lBound)/(max-min))+uBound;
}
}
return temp;
}
int main()
{
int disp[4][4] = {
{10, 11, 12, 13},
{14, 15, 16, 17},
{18, 19, 20, 21},
{22, 23, 24, 25}
};
printf("Image Before Stretching:\n");
display(disp);
printf("Image After Stretching:\n");
display(constrast(disp));
return 0;
}
This is the error message I get after attempting to compile:
contrast.c: In function ‘display’:
contrast.c:6:4: error: ‘for’ loop initial declarations are only allowed in C99 mode
for(int i=0; i<n; i++) {
^
contrast.c:6:4: note: use option -std=c99 or -std=gnu99 to compile your code
contrast.c:7:7: error: ‘for’ loop initial declarations are only allowed in C99 mode
for(int j=0;j<m;j++) {
^
contrast.c: In function ‘constrast’:
contrast.c:21:4: error: ‘for’ loop initial declarations are only allowed in C99 mode
for(int i=0; i<n; i++) {
^
contrast.c:22:7: error: ‘for’ loop initial declarations are only allowed in C99 mode
for(int j=0;j<m;j++) {
^
contrast.c:23:82: error: expected ‘)’ before ‘;’ token
temp[i][j] = ((arr[i][j] - max) *((int)(uBound-lBound)/(max-min))+uBound;
^
contrast.c:24:7: error: expected ‘;’ before ‘}’ token
}
^
contrast.c:28:4: warning: return from incompatible pointer type [enabled by default]
return temp;
^
contrast.c:28:4: warning: function returns address of local variable [-Wreturn-local-addr]
contrast.c: In function ‘main’:
contrast.c:44:1: warning: passing argument 1 of ‘display’ from incompatible pointer type [enabled by default]
display(constrast(disp));
^
contrast.c:4:6: note: expected ‘int (*)[(sizetype)m]’ but argument is of type ‘int *’
void display(int arr[n][m]){
^
int *constrast(int arr[n][m])
{
int temp[n][m];
int max = 25;
int min = 10;
int uBound = 255;
int lBound = 0;
for(int i=0; i<n; i++) {
for(int j=0;j<m;j++) {
temp[i][j] = ((arr[i][j] - max) *((int)(uBound-lBound)/(max-min))+uBound;
}
}
return temp;
}
Besides the type of temp which is int[4][4] doesn't match the function type which is int * you shall never return the address of a local variable since such variables are gone when the function ends and a pointer to it won't be valid any longer.
You cannot return an array from a function in C. A workaround would be to either wrap the array in a struct or to allocate the memory dynamically. Even better: Pass the output array as a parameter.
When you are trying to execute row return temp;, it returns only the pointer to the array and then goes out of the function. But that array was allocated in system stack when your code went in int *constrast(int arr[n][m]) function. On return, it deletes that variable and you are trying to work with a bad pointer.
As a solution, you can pass result array pointer as a parameter too, or use global variables.

2D array in by reference

I have this...
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//definições de constantes usadas para agilizar a implementação.
#define MINA 'M'
#define SUB 'S'
#define JOG 'U'
#define CC 'I'
//Função que inicializa posição do jogador.
void posjog(char **mesa,int lin, int col){
srand(time(NULL));
int i = rand() % 2;
int j = rand() % 2;
if(i==1){
i = lin;
}
if(j==1){
j = col;
}
mesa[i][j] = JOG;
}
//Função que inicializa a matriz vazia.
void arfill(char **mesa,int lin, int col){
for(int i=0;i<=lin;i++){
for(int j=0;j<=col;j++){
mesa[i][j]='.';
}
}
}
void show(char **mesa,int lin,int col){
for(int i=0;i<=lin;i++){
for(int j=0;j<=col;j++){
printf("%c",mesa[i][j]);
}
printf("\n");
}
}
int main(void) {
char campo[9][9]; //matriz de jogo
arfill(campo,9,9);
posjog(campo,9,9);
show(campo,9,9);
return EXIT_SUCCESS;
}
My code always crashes, i dunno why.
Can some of you please help me with this?
I have some Warnings on the Eclipse too...
"passing argument 1 of 'arfill' from incompatible pointer type [enabled by default]"
"expected 'char **' but argument is of type 'char (*)[9]'"
Array indexing starts from 0 and ends in size of array - 1.
So, this
for(int i=0;i<=lin;i++){
for(int j=0;j<=col;j++){
will go from 0 to 9 for an array of size 9x9, thus it will go out of bounds.
Similarly the rest of the code has similar problems.
You should definitely enable the warning of your compiler (-Wall flag would suffice for a start). Warnings are good to be treated like errors by us. Here is what I got:
../main.c: In function ‘main’:
../main.c:45:5: warning: passing argument 1 of ‘arfill’ from incompatible pointer type [enabled by default]
../main.c:26:6: note: expected ‘char **’ but argument is of type ‘char (*)[9]’
../main.c:46:5: warning: passing argument 1 of ‘posjog’ from incompatible pointer type [enabled by default]
../main.c:12:6: note: expected ‘char **’ but argument is of type ‘char (*)[9]’
../main.c:47:5: warning: passing argument 1 of ‘show’ from incompatible pointer type [enabled by default]
../main.c:34:6: note: expected ‘char **’ but argument is of type ‘char (*)[9]’
Finished building: ../main.c
So one way to deal with this is to use define for the sizes of the matrix. Then your code should look like this:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//definições de constantes usadas para agilizar a implementação.
#define MINA 'M'
#define SUB 'S'
#define JOG 'U'
#define CC 'I'
#define N 9
#define M 9
//Função que inicializa posição do jogador.
void posjog(char mesa[N][M]) {
srand(time(NULL));
int i = rand() % 2;
int j = rand() % 2;
if (i == 1) {
i = N;
}
if (j == 1) {
j = M;
}
// if i and j have the value of N and M, this will go out of bounds
// mesa[i][j] = JOG; <---- modify this
}
//Função que inicializa a matriz vazia.
void arfill(char mesa[N][M]) {
// Replaces <= with < in both for loops
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
mesa[i][j] = '.';
}
}
}
void show(char mesa[N][M]) {
// Replaces <= with < in both for loops
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
printf("%c", mesa[i][j]);
}
printf("\n");
}
}
int main(void) {
char campo[N][M]; //matriz de jogo
arfill(campo);
posjog(campo);
show(campo);
return EXIT_SUCCESS;
}
Of course you could dynamically allocate the memory for the array, which would get rid of the define. In that case don't forget to de-allocate your memory.

Resources