0
#include <stdio.h>
.
.
.
// Dynamic Array
int **tab=(int**)malloc(sizeof(int*)*n);
for(i=0;i<2;i++)
    {
   tab[i]=(int*)malloc(sizeof(int)*2);
    }
// Array = 0
for(i=0;i<n;i++)
{
    tab[i][0]=0;
    tab[i][1]=0;
}
.
.
.

The program crashes just after the Array = 0 comment. PLease help my assignment is due.

NOTES: n is given by the user and can be any int.

Mechanic45
  • 173
  • 1
  • 6
  • 16

1 Answers1

1

It looks like you're only allocating memory for the first 2 pointers of tab and not n elements. Then if n is greater than 2 you're trying to dereference pointers that could point to anything. To fix this just change the 2 in the first for loop to n.

for(i=0; i < n; i++)
{
    tab[i]=(int*)malloc(sizeof(int)*2);
}
sudowoodo
  • 473
  • 4
  • 14