2D array in by reference - c

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.

Related

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

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;
}

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

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.

c: segmentation fault (selection sort)

#include<stdio.h>
void selsort(int a[],int s)
{
int min,temp,i,j;
for(i = 0; i < s ; i++)
{
min = 0;
for(j = i+1 ; j < s; j++)
{
if(min > a[j])
{
min = j;
}
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
for (j=0;j<s;j++)
{
printf("%d",a[i]);
}
printf("\n");
}
}
main()
{
int i,a[5];
int size = 5;
printf("Enter elements of the array");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
selsort(a[5],size);
}
The error is as follows:
selsort.c:35:2: warning: passing argument 1 of ‘selsort’ makes pointer from integer without a cast [enabled by default]
selsort.c:2:1: note: expected ‘int *’ but argument is of type ‘int’
any tips on how to avoid this problem in the future will be really helpful
You should be calling your function like this:
selsort(a, size);
a[5] means "element at index 5" (past the end of your array, by the way: the largest legal element in int a[5] is at index 4).
You should also replace
min = 0;
with
min = i;
and use it like this:
if(a[min] > a[j]) ...
In the function call selsort(a[5], size); you are only passing the 5th [nonexistent] member of a. You want selsort(a, size);

Problems passing struct pointers and "conflicting types" errors

I am writing a program that permutes a list of names based on a given input. Here is my code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 19 //names no longer than 19 chars
#define MAXPEOPLE 10
struct genderinfo {
char** winning;
char** names;
int** scores;
};
char** allocnames(int num);
int** allocratings(int num, int num2);
void inputdata(int numpeople, struct genderinfo *male, struct genderinfo *female, FILE* fp);
void permute(int permuteset[], int k, int numpeople, struct genderinfo *male, struct genderinfo *female, int *maxLindex);
void swap(int permuteset[],int i, int j);
void compare(int permuteset[], struct genderinfo *male, struct genderinfo *female, int *maxLindex, int numpeople);
///write free functions for struct arrays
int main () {
FILE* fp = fopen("matching.txt", "r");
struct genderinfo male;
struct genderinfo female;
//loop variables
int numdates, i, j, k;
//other variables
int numpeople, maxLindex = 0, difference;
fscanf(fp, "%d", &numdates);
for(i=1; i <= numdates; i++) {
fscanf(fp, "%d", &numpeople);
//printf("%d possible couples\n", numpeople);
//allocate memory for arrays of names
male.names = allocnames(numpeople);
female.names = allocnames(numpeople);
male.winning = allocnames(numpeople);
female.winning = allocnames(numpeople);
//allocate memory for score arrays
male.scores = allocratings(numpeople, numpeople);
female.scores = allocratings(numpeople, numpeople);
int permuteset[numpeople];
//fill permute set with 0-k, these will serve as array indexes to compare against malenames array
//format used will be malenames[i][permuteset[i]] femalenames[permuteset[i]][i] to index the scores
for(k=0; k<numpeople; k++)
permuteset[k] = k;
inputdata(numpeople, &male, &female, fp);
permute(permuteset, 0, numpeople, &male, &female &maxLindex);
printf("Matching #%d: Maximum Score = %d\n\n", i, maxLindex);
for (j=0; j<numpeople; j++){
printf("%s %s\n", male.winning[j], female.winning[j]);
}
printf("\n\n");
}
return 0;
}
char** allocnames(int num) {
int i;
char** names = (char**)malloc(num*sizeof(char*));
for(i=0; i < num; i++)
names[i] = (char*)malloc(MAX_LEN+1);
return names;
}
int** allocratings(int num, int num2) {
int i;
int** ratings = (int**)malloc(num*sizeof(int*));
for(i=0; i < num; i++)
ratings[i] = (int*)malloc(num2*sizeof(int));
return ratings;
}
void inputdata(int numpeople, struct genderinfo *male, struct genderinfo *female, FILE* fp) {
int i, j;
for (i=0; i < numpeople; i++) {
fscanf(fp, "%s", male->names[i]);
//printf("%s ", malenames[i]);
}
for (i=0; i < numpeople; i++) {
fscanf(fp, "%s", female->names[i]);
//printf("%s ", femalenames[i]);
}
for (i=0; i < numpeople; i++) {
for (j=0; j < numpeople; j++) {
fscanf(fp, "%d", &male->scores[i][j]);
//printf("%d ", malescores[i]);
}
}
for (i=0; i < numpeople; i++) {
for(j=0; j < numpeople; j++) {
fscanf(fp, "%d", &female->scores[i][j]);
//printf("%d ", femalescores[i][j]);
}
}
}
void permute(int permuteset[], int k, int numpeople, struct genderinfo *male, struct genderinfo *female, int *maxLindex) {
int i;
if (k == numpeople) {
compare(permuteset, &male, &female, &maxLindex, numpeople);
}
else {
// Loop through each possible starting letter for index k,
// the first index for which we have a choice.
for (i=k; i<numpeople; i++) {
// Place the character stored in index j in location k.
swap(permuteset, k, i);
// Print out all of the permutations with that character
// just chosen above fixed.
permute(permuteset, 0, numpeople, &male, &female &maxLindex);
// Put the original character that used to be there back
// in its place.
swap(permuteset, i, k);
} //end i for
} //end else
}
void swap(int permuteset[], int i, int j) {
int temp = permuteset[i];
permuteset[i] = permuteset[j];
permuteset[j] = temp;
}
//This function will take a permutation in and compare malescores[i] to
//femalescores[permuteset[i]]
//if malescores[i] > femalescores[permuteset[i]] scoresum += bla blah else if.... etc.
//copy malenames[i] and femalenames[permuteset[i]] into winning couples
//with if statements above
//malescores[i][permuteset[i]]
//femalescores[permuteset[i]][i]]
void compare(int permuteset[], struct genderinfo *male, struct genderinfo *female, int *maxLindex, int numpeople) {
int temp_maxLindex, i, j, minlike;
for(i=0; i<numpeople; i++){
if (male->scores[i][permuteset[i]] > female->scores[permuteset[i]][i])
minlike = female->scores[permuteset[i]][i];
else
minlike = male->scores[permuteset[i]][i];
temp_maxLindex += minlike;
if (temp_maxLindex > maxLindex) {
maxLindex = temp_maxLindex;
for(j=0; j<numpeople; j++){
strcpy(male->winning[j], male->names[i]);
strcpy(female->winning[j], female->names[permuteset[i]]);
} //j for
} //if
} //i for
}
I am getting these errors, one having to do with what im passing into my inputdata function, and a bunch of others about conflicting types?? I have been tinkering around with the code for a bit now and can't get anywhere. At this point I'm just trying to get the code to run so I can debug the algorithm and whatnot, any help is appreciated greatly.
D:\School\Summer 2012\CS1\Assignments\2\main.c||In function 'main':|
D:\School\Summer 2012\CS1\Assignments\2\main.c|61|error: invalid operands to binary & (have 'struct genderinfo *' and 'int')|
D:\School\Summer 2012\CS1\Assignments\2\main.c|61|error: too few arguments to function 'permute'|
D:\School\Summer 2012\CS1\Assignments\2\main.c|17|note: declared here|
D:\School\Summer 2012\CS1\Assignments\2\main.c|35|warning: unused variable 'difference' [-Wunused-variable]|
D:\School\Summer 2012\CS1\Assignments\2\main.c||In function 'permute':|
D:\School\Summer 2012\CS1\Assignments\2\main.c|139|warning: passing argument 2 of 'compare' from incompatible pointer type [enabled by default]|
D:\School\Summer 2012\CS1\Assignments\2\main.c|19|note: expected 'struct genderinfo *' but argument is of type 'struct genderinfo **'|
D:\School\Summer 2012\CS1\Assignments\2\main.c|139|warning: passing argument 3 of 'compare' from incompatible pointer type [enabled by default]|
D:\School\Summer 2012\CS1\Assignments\2\main.c|19|note: expected 'struct genderinfo *' but argument is of type 'struct genderinfo **'|
D:\School\Summer 2012\CS1\Assignments\2\main.c|139|warning: passing argument 4 of 'compare' from incompatible pointer type [enabled by default]|
D:\School\Summer 2012\CS1\Assignments\2\main.c|19|note: expected 'int *' but argument is of type 'int **'|
D:\School\Summer 2012\CS1\Assignments\2\main.c|153|error: invalid operands to binary & (have 'struct genderinfo **' and 'int *')|
D:\School\Summer 2012\CS1\Assignments\2\main.c|153|warning: passing argument 4 of 'permute' from incompatible pointer type [enabled by default]|
D:\School\Summer 2012\CS1\Assignments\2\main.c|133|note: expected 'struct genderinfo *' but argument is of type 'struct genderinfo **'|
D:\School\Summer 2012\CS1\Assignments\2\main.c|153|error: too few arguments to function 'permute'|
D:\School\Summer 2012\CS1\Assignments\2\main.c|133|note: declared here|
D:\School\Summer 2012\CS1\Assignments\2\main.c||In function 'compare':|
D:\School\Summer 2012\CS1\Assignments\2\main.c|194|warning: comparison between pointer and integer [enabled by default]|
D:\School\Summer 2012\CS1\Assignments\2\main.c|195|warning: assignment makes pointer from integer without a cast [enabled by default]|
||=== Build finished: 10 errors, 7 warnings ===|
--- permute.c.OLD 2012-06-14 22:48:06.760926525 +0200
+++ permute.c 2012-06-14 22:47:37.344810744 +0200
## -21,7 +21,7 ##
///write free functions for struct arrays
-int main () {
+int main (void) {
FILE* fp = fopen("matching.txt", "r");
## -32,7 +32,7 ##
int numdates, i, j, k;
//other variables
-int numpeople, maxLindex = 0, difference;
+int numpeople, maxLindex = 0; // difference;
fscanf(fp, "%d", &numdates);
## -58,7 +58,7 ##
permuteset[k] = k;
inputdata(numpeople, &male, &female, fp);
- permute(permuteset, 0, numpeople, &male, &female &maxLindex);
+ permute(permuteset, 0, numpeople, &male, &female, &maxLindex);
printf("Matching #%d: Maximum Score = %d\n\n", i, maxLindex);
## -136,7 +136,7 ##
if (k == numpeople) {
- compare(permuteset, &male, &female, &maxLindex, numpeople);
+ compare(permuteset, male, female, maxLindex, numpeople);
}
else {
## -150,7 +150,7 ##
// Print out all of the permutations with that character
// just chosen above fixed.
- permute(permuteset, 0, numpeople, &male, &female &maxLindex);
+ permute(permuteset, 0, numpeople, male, female, maxLindex);
// Put the original character that used to be there back
// in its place.
## -191,8 +191,8 ##
temp_maxLindex += minlike;
- if (temp_maxLindex > maxLindex) {
- maxLindex = temp_maxLindex;
+ if (temp_maxLindex > *maxLindex) {
+ *maxLindex = temp_maxLindex;
for(j=0; j<numpeople; j++){
Summary: missing kommaas, too many ampersands, missing asterixes.
I have not checked for semantic errors, I presume they are still present. (the unbounded recursive call in permute() looks suspect)
I also advise to remove the casts from malloc()s return value and use the idiom p = malloc N * sizeof *p); But that would increase the size of the diff even more.
I am only guessing here as the code that is causing the error is not given by you at time of posting this, but as the list of your parameters to these functions
void permute(int permuteset[], int k, int numpeople, char** winningmales, char** winningfemales, char** malenames, char** femalenames, int** malescores,
int** femalescores, int *maxLindex);
void compare(int permuteset[], char** winningmales, char** winningfemales, char** malenames, char** femalenames, int** malescores, int**femalescores,
int *maxLindex, int numpeople);
is very large and as you report an error between an int* and int** I wonder if you have accidentally got the order of your arguments wrong in the places that you call the above functions???
As I say this is only a guess but it might be worth investigating.

Resources