I'm having problems allocating space for a 2d array. Since I don't know precisely where the problem is, I've posted the exact content of the function I'm working with as well as the file that it references. Basically what happens is that I'm always ending up with junk values in my array. I figure it's either a memory size problem or something to do with the way that my typedef is set up. Any help would be appreciated.
Here's the kind of file that this function will be reading. The room descriptions are not used by this function, they're just there for completeness.Code:#include <stdio.h> #include <string.h> #include <stdlib.h> /* Each of the rooms is a row of the array and each room attribute is a column of the array */ /* Room attributes: * column 0 Room Number * column 1 Location of room in room file (character offset) * column 2 Number of the room to the north * column 3 Number of the room to the east * column 4 " " " " south * column 5 " " " " west * column 6 " " " " up * column 7 " " " " down */ #define ROOM_ATTRIBUTES 8 /* The pointer to the room array needs to be passed to other functions, * so this typedef is made in the header file */ typedef int (*rmArray)[ROOM_ATTRIBUTES]; rmArray Parse_Room_List(FILE *roomFile) { int nextChar; char nextLine[100]; int roomIndex = 0; /* This is supposed to initialize the size of the room array. * I expect my problem may be here. */ rmArray roomInfo = malloc(sizeof(int)*ROOM_ATTRIBUTES); for(;;) { nextChar = fgetc(roomFile); /* If the next character is a "#" character, then the number that follows * is a room number */ if (nextChar == '#') { /* This resizes the array based on the number of rooms logged. * I expect I may have a problem here too. */ roomInfo = realloc(roomInfo,(roomIndex+1)*sizeof(int)*ROOM_ATTRIBUTES); /* Gets the rest of the line, extracts the number, and stores it in * the 0th attribute column for the current room index. */ fgets(nextLine,sizeof(nextLine),roomFile); sscanf(nextLine,"%d",&roomInfo[roomIndex][0]); /* Gets the current position in the file and stores it in the 1st * attribute column. */ roomInfo[roomIndex][1]=(int) ftell(roomFile); roomIndex++; } /* If the next character is a "@" character, then the number that follows * is an attribue for the most recent room */ else if (nextChar == '@') { roomIndex--; /* nextChar stores the room attribute letter (n, e, s, w, u, or d) * and nextLine stores the rest of the line following that character */ nextChar = fgetc(roomFile); fgets(nextLine,sizeof(nextLine),roomFile); /* Depending on which character was read, the number in the line * is extracted and stored in the appropriate attribute column. */ switch(nextChar){ case 'n': sscanf(nextLine,"%d",&roomInfo[roomIndex][2]); break; case 'e': sscanf(nextLine,"%d",&roomInfo[roomIndex][3]); break; case 's': sscanf(nextLine,"%d",&roomInfo[roomIndex][4]); break; case 'w': sscanf(nextLine,"%d",&roomInfo[roomIndex][5]); break; case 'u': sscanf(nextLine,"%d",&roomInfo[roomIndex][6]); break; case 'd': sscanf(nextLine,"%d",&roomInfo[roomIndex][7]); break; } roomIndex++; } else if (nextChar == EOF) break; } return roomInfo; }
Code:#1 @n5 This is the room description for room 1. Room 5 is to the north of here. #2 @e5 This is the room description for room 2. Room 5 is to the east of here. #3 @s5 This is the room description for room 3. Room 5 is to the south of here. #4 @w5 This is the room description for room 4. Room 5 is to the west of here. #5 @n3 @e4 @s1 @w2 This is the room description for room 5. Rooms 1 through 4 are in each of the cardinal directions.



LinkBack URL
About LinkBacks



