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!

line 29.

you can't use variables to declare an array.

line 29.

you can't use variables to declare an array.

Hm. In the case of the actual assignment, those variables are program arguments... how would I get around that dilemma? Also, why does it appear to declare the array properly in main, but not hold up once passed to the function?

i dont know what your actual assignment says. but in any event you just can't use integer variables to declare array sizes. unless you dynamically allocate arrays, they are allocated at compile time and those variables are meaningless until runtime.

you can do something like this:

#define BLOCK_SIZE     4
#define NUM_SETS       2
#define BLOCKS_PER     1

int i, j;
cacheItem cache[BLOCK_SIZE*NUM_SETS][BLOCKS_PER];

//...

as to passing arguments, either pass constants as arguments directly, or use the #define'd constants as globals, or assign the local variables to the #define'd constant values before passing them.

you could also use "sizeof" to determine the size of the arrays during runtime.

finally, just a note, your "block size" and "number of sets" as separate entities seem to be a meaningless distinction -- you never use them individually, you always use them multiplied together.


.

commented: Good suggestion +6

>you can't use variables to declare an array.

C99 support variable length array. A variable length array, which is a C99 feature, is an array of automatic storage duration whose length is determined at run time.

>Passing multidimensional array to a function
Text from c-faq :

The most straightforward way of passing a multidimensional array to a function is to declare it in exactly the same way in the function as it was declared in the caller.

void printCache(cacheItem cache[8][1], int blockSize, int numSets, int blocksPer)
{
  ....
}
commented: boom. +5

So I am trying to malloc the array in the following way, but I am still getting segfault errors if I try to access which means I am probably not doing it correctly. This is incredibly frustrating! Am I completely off base?

cacheItem** cache;

    cache = (cacheItem **) malloc(blockSize*numSets*sizeof(cacheItem*));
    for(i = 0; i < blocksPer; i++)
    {
        cache[i] = (cacheItem *) malloc(blocksPer*sizeof(cacheItem*));
    }

C99 support variable length array

damn C99

:icon_redface:

So I am trying to malloc the array in the following way, but I am still getting segfault errors if I try to access which means I am probably not doing it correctly. This is incredibly frustrating! Am I completely off base?

it looks right to me... although you know it's no longer necessary to cast calls to malloc since ISO C was introduced. And it's recommended that you don't.

as for the seg fault error, did you try adatapost's suggestion?


.

Gah, what was I thinking? I just ended up changing the declaration of the cache array to have the fixed width and length of 256 (as y'all suggested) and problem solved. I must have been over-thinking this dilemma, or not thinking about it enough. Dumb newb post of the day? Thanks for all the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.