Problem about multidimensional array.

About the source file, the compiler doesn't tell me error, but during the execution of the program, it stops working..

#include <iostream>
#define R_MAX 1024
#define C_MAX 1024
using namespace std;

int main()
{
int r=0, c=0;
char **matrix, temp;

matrix=(char**)malloc(R_MAX*sizeof(char*));

for(int c=0; c<C_MAX; c++)
{
matrix[c]=(char*)malloc(C_MAX*sizeof(char));
}

for(int r=0; r<10; r++)
{
for(int c=0; c<10; c++)
{
matrix[r][c]='\0';
}
}

c=0;
r=0;
for(int i=0; i<5; i++)
{
cout<<"Inserisci un nome: ";
while((temp=getchar())!='\n')
{
matrix[r][c]=temp;
c++;
matrix[r]=(char*)realloc(matrix[r],(c+1)*sizeof(char));
}
matrix[r][c]='\0';
r++;
matrix=(char**)realloc(matrix,(r+1)*sizeof(char*));
c=0;
}

r=0;
while(matrix[r][0]!='\0')
{
cout<<matrix[r]<<endl;
r++;
}

system("pause");
}
Last edited on
I don't understand why you're calling realloc. It's wrong but if tell me what you're trying to do, I can explain why it's wrong.
Topic archived. No new replies allowed.