Hello, I am trying to pass a 2d array of a struct to a function. Sadly, when I pass the array to the function, only the first row appears to get passed. Every other row's values are set to zero (or in the case of the code snippet I provided to show only the relevant stuff, segfault). Does anybody know why this is? I thought everything was passed by reference in C...but this seems to be some weird behavior. I was thinking a way to remedy this would pass a pointer to the array to the function, but I haven't been able to get the syntax right for that. Any help would be much appreciated.
Here is some relevant code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct cacheStruct {
int word;
int data;
int dirty;
int lr;
} cacheItem;
void printCache(cacheItem cache[256][256], int blockSize, int numSets, int blocksPer)
{
int i, j;
printf("passed to function: \n");
for(i = 0; i < blockSize*numSets; i++){
for(j = 0; j < blocksPer; j++){
printf("%d\n", cache[i][j].lr);
}
}
}
int
main(int argc, char *argv[])
{
int i, j;
int blockSize = 4, numSets = 2, blocksPer = 1;
cacheItem cache[blockSize*numSets][blocksPer];
for(i = 0; i < blockSize*numSets; i++)
{
for(j = 0; j < blocksPer; j++)
{
cache[i][j].word = -1;
cache[i][j].lr = -1;
cache[i][j].dirty = 0;
}
}
printf("in function\n");
for(i = 0; i < blockSize*numSets; i++){
for(j = 0; j < blocksPer; j++){
printf("%d\n", cache[i][j].lr);
}
}
printCache(cache, blockSize, numSets, blocksPer);
}
Upon running the program:
in function
-1
-1
-1
-1
-1
-1
-1
-1
passed to function:
-1
Segmentation fault
Any help would be much appreciated!