0
votes

I code:

#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>

void nhap(int **s, int line, int column)
{
int x;
srand((unsigned int)time(NULL));
for(int i=0; i<line; i++)
    for(int j=0; j<column; j++)
    {
        x=rand()%10+0;
        *(*(s+i)+j)=x;
    }

}

void xuat(int **s, int line, int column)
{
for(int i=0; i<line; i++)
{
    printf("\n");
    for(int j=0; j<column; j++)
        printf("%3d",s[i][j]);
}
}

int main()
{
int line,column;
printf("\nLine: "); scanf("%d",&line);
printf("\nColumn: "); scanf("%d",&column);

int **s=new int*[line];
for(int i=0; i<line; i++)
    s[i]=new int[column];

nhap(s,line,column);
xuat(s,line,column);

getch();
for(int i=0; i<line; i++)
    delete[] (s+i);
delete[] s;
}

Error: Debug Assertion Failed! Exspression: _BLOCK_TYPE_IS_VALID. For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.

1
Running fine on my PC.. Windows + Visual Studio 10 - Hitesh Vaghani
You are inconsistent, *(*(s+i)+j) and s[i][j]. You should stick to the later, it's simpler to use. - Marius Bancila
Oh no. It's no error. Thank you! - Hoang

1 Answers

2
votes

delete [] (s+i); is your error. For clarity you should probably just stick to regular array syntax. The *(s+i) stuff is equivalent, but harder to read/understand. So I'd use delete [] s[i].