I'm trying to program something for a final project in my C Programming class but I'm stuck on a problem I can't figure out. My code is:
#include <stdio.h>
#include <stdlib.h>
char** allocateLevel(int sizeOfLevel);
int main(void)
{
char** level = allocateLevel(10);
int one, two;
for(one = 0 ; one < 10 ; one++)
{
for(two = 0 ; two < 10 ; two++)
{
level[one][two]='T'; //Crashes right here
}
}
printf("%c", level[4][5]); //tests to see if it prints
}
char** allocateLevel(int sizeOfLevel)
{
char **levelPointer;
levelPointer = (char **)malloc(sizeOfLevel * sizeof(char **));
int count = 0;
for(count = 0 ; count < sizeOfLevel ; count++)
{
levelPointer[count] = (char*) malloc(sizeOfLevel * sizeof(char*));
}
}
The program allocates the memory fine but it crashes when I try to assign a value to one of the elements. I can't figure out what's going wrong and my professor hasn't been of much help. Is there anything wrong?