I call a file with the list of months from January-December and store them in a structure of its own.
Then I'm wanting to allocate a 2d array so I can store the counts of the months for each year seen in the next file I'm going to open.
Anyways I'm having issues with this part of my code i believe...
The point of this is to initialize my 2d array of ints
Code:
int **array_of_years = malloc( 11* sizeof(**array_of_years));
The point of this is to give my 2d array of ints enough space to store a count for each month for the years 2000-2009.
I use calloc because I want the memory to have the value of 0 so I can just up the counts as I go.
Code:
for(i = 0; i < counter1; i++)
array_of_years[i] = calloc(10 , sizeof(**array_of_years));
I have the program print Hi, I'm alive to test it out. Anyways this works on Dev-C, but when I try to run this on gcc it gives me the corrupted double-linked list error.
Reading up on this it looks like I probably overlapped the memory or something along those lines? Not familiar with this error at all. Any help to fix this would be greatly appreciated.
I stripped a lot of my code out to try to find out the source of the problem which is why I think it's the code I've given above.
Anyways I'll post the rest of the code just to show you what I have.
Thanks
Also this must be dynamically allocated for the counts.
Code:
#include <stdio.h>#include <stdlib.h>
#include <string.h>
struct struct_of_months
{
char months[20];
};
int main(void)
{
FILE *fp;
char buffer[50];
int counter1 = 0;
char *p;
int i;
struct struct_of_months *array_of_months = malloc (sizeof(*array_of_months));
int **array_of_years = malloc( 11* sizeof(**array_of_years));
//Opens the first file and stores the months in array of structures
if ( (fp = fopen("months.txt", "r" )) == NULL )
{
printf("Couldn't open file\n");
exit(1);
}
while(fgets(buffer, sizeof(buffer), fp))
{
if (counter1>0)
array_of_months=realloc(array_of_months,(counter1+1)*sizeof(*array_of_months));
if ((p = strchr(buffer, '\n')) != NULL)
*p = '\0';
strcpy(array_of_months[counter1].months, buffer);
counter1++;
}
fclose( fp );
//Supposed to allocate enough memory for the 2d array.
for(i = 0; i < counter1; i++)
array_of_years[i] = calloc(10 , sizeof(**array_of_years));
printf("Hi, I'm alive\n");
free(array_of_months);
free(array_of_years);
getchar();
return 0;
}