using modified structure in another function

Viewed 47
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define ogrNo "34"
#define hex "0123456789ABCDEF"

int bussNo;
void hexControlFonk();

struct Otobusler {
    int bussNumber;
    char bussHexNum[100][6];
};

int main() {
    srand(time(NULL));
    char numaraDeposu[10000][6] = { "" };
    struct Otobusler buss[1000];

    scanf("%d", &bussNo);
    for (int i = 1; i <= bussNo; i++) {
        for (int j = 0; j < 4; j++) {
            numaraDeposu[i][j] = hex[rand() % 16];
        }
        for (int k = 4; k >= 0; k--) {
            int l = k + 2;
            numaraDeposu[i][l] = numaraDeposu[i][k];
        }
        numaraDeposu[i][0] = ogrNo[0];
        numaraDeposu[i][1] = ogrNo[1];
        strcpy(buss[i].bussHexNum[i], numaraDeposu[i]);
        buss[i].bussNumber = i;
        printf("%d-%s\n", buss[i].bussNumber, buss[i].bussHexNum[i]);
    }
    hexControlFonk(buss[1000]);
    printf("%s\n", buss[10].bussHexNum[10]);
}

void hexControlFonk(struct Otobusler buss[1000]) {
    for (int m = 1; m < bussNo; m++) {
        for (int n = 1; n < bussNo; n++) {
            if (strcmp(buss[m].bussHexNum[m], buss[n].bussHexNum[n]) == 0 && n != m) {
                printf("%d,", n);
            }
        }
    }
    printf("%s\n", buss[10].bussHexNum[10]);
}

I made a program which makes hexadecimal numbers for each buss. And they start with 34 (my school Number) it works fine but I can't use modified version of structure in hexControlFonk(). This function checks if any 2 hex codes are the same. But buss 10 in main and buss 10 in hexControlFonk isn't same. Need help.

1 Answers

Here are some important issues with your code (compiled list from comments).
As you have confirmed by now, fixing these also solves your problem.

  • Fix wrong prototype: void hexControlFonk(); -> void hexControlFonk(struct Otobusler buss[1000]);

  • Move type declaration needed for the correct prototype to before the prototype.

  • Comment by WhozCraig: Call the function correctly, with the array (effectively with a pointer to it); hexControlFonk(buss[1000]); ==> hexControlFonk(buss);`

  • Comment by Werner Henze: (covered by above) Do not call the function with a value read from behind the array, i.e. with an index from the allowed range 0...999, not 1000.

Related