returning an array from a function

Started by
6 comments, last by kman12 19 years, 3 months ago
I have this function that loads a file and generates an array from that, but I can't figure out how to make it so other functions can access the array. I can't make the array global because its dimensions come from data in the file, so I don't know the dimensions until in the function. I just need a way to return the array or somehow be able to access it with other functions. Here's the function in case you're interested:

void mapfileparse(char filename[20]) {
	
	//initialize all needed variables
	int x;
	int y;
	char line[80];
	char mapname[80];
	char author[80];
	int tempstring;
	FILE *file;
	int i;
	int j;
	char mapwidths[5];
	char mapheights[5];

	
	//open the file 
	if((file = fopen(filename,"r")) == NULL) {
		printf("Failed to open %s\n", filename);
		exit(1);
	}
	//get metadata
	fgets(mapname,80,file);
	fgets(author,80,file);
	fgets(mapwidths,80,file);
	fgets(mapheights,80,file);
	//now we can initialize the map array
	mapwidth = atoi(mapwidths);
	mapheight = atoi(mapheights);
	int maparray[mapwidth][mapheight];
	//zero the map array
	for(i = 0; i < mapwidth; i++) {
		for(j = 0; j < mapheight; j++) {
			maparray[j] = 0;
		}
	}
	//grab the real data
	while(fgets(line,80,file)) {
		if(strlen(line) > 4) {
			char delimiters[1] = ",";
			x = atoi(strtok(line, delimiters));
			y = atoi(strtok(NULL, delimiters));
			tempstring = strtok(NULL, delimiters);
			maparray[x][y] = atoi(tempstring);
		}
	}
	fclose(file);
}
Am I even approaching this the right way? I'm still learning C and I'm not really sure of myself yet. I used to have this code together with the function that used it to draw the tiles, but this doesn't work because I need to be able to redraw the tiles without re-reading the file. thanks, kevin
Advertisement
You need what is known as 'dynamic allocation', or std::vector. Google std::vector.
Quote:Original post by CJH
You need what is known as 'dynamic allocation', or std::vector. Google std::vector.

if he's learning C, how would std::vector help him??
people need to stop thinking of C and C++ are the same thing...
they're not especially as of C(19)99

Beginner in Game Development?  Read here. And read here.

 

I believe what CJH is referring to is the line:

int maparray[mapwidth][mapheight];

This line is actually illegal. The size of an array must be known at compile time. So, the mapwidth and mapheight values must be a constant value. This means they either have to be #defines, const variables, or an actual integer value (like 5).

std::vector is part of the STL, and is essentially an array that can change size during runtime (standard arrays cannot). STL is usable in C++, but it sounds like you're only using C, so it will be unavailable.

You will need to use a double pointer for the maparray variable definition:

int **maparray;

and do a bunch of mallocs to set up the map array. To return the maparray data, the function definition would be:

int **mapfileparse(char filename[20])
It's not often that someone uses pure C, especially a beginner. In fact, gamedev is almost devoid of C.
you might be able to pass it as an in/out param for the function. create a pointer to the data type, pass the pointer to the function, fill the array inside the function, and then you might need to use the return value for the size of the array, if its not known prior to funcion. i wanted to find some code so i could show how i did it when i pulled a vairable size array of strings for a file, but i changed over to a single texture per mesh and deleted that struct and function.

good luck!
Quote:Original post by CJH
It's not often that someone uses pure C, especially a beginner. In fact, gamedev is almost devoid of C.
Not true.

The AP points you in the right direction. You'll need to allocate the memory yourself, but there are complications. This is the traditional approach:
int ** maparray = (int **)malloc(mapwidth * sizeof(int *));for(int i = 0; i < mapwidth; ++i){  maparray = (int *)malloc(mapheight * sizeof(int));  // assign row at i}
To obtain the same in-memory characteristics as a statically allocated array, however, you need to allocate the entire 2D array linearly, and then find a means for correctly referencing the individual elements:
int ** maparray = (int **)malloc(mapwidth * mapheight);...int index(int i, int j, int width, int height){  return (i % width) * height + (j % height);}...// access elements as follows:maparray[index(i, j, mapwidth, mapheight)];
But there's more.

Dynamically allocated memory must be released, and you're going to have to come up with a scheme to deal with that. One approach is to return the memory to the calling code and have it take responsibility for releasing it (lots of CRT functions do this, with sometimes disasterous consequences). Another alternative is to require that the calling code pass in a memory buffer that it has previously allocated. The third alternative is to statically maintain the memory within the function, but that prevents it from being invoked multiple times without special care being taken in the calling code.

Good luck.
Thanks a lot guys! I just got it working. I did int **maparray at the top so it was global, then allocated the memory in the function. Now it works just like it's supposed to.

thanks,
kevin

This topic is closed to new replies.

Advertisement