I am new to C++ and i am trying to learn pointers.
As a working exercise i try to read a nxn matrix using pointers to pointers.
This is what i have tried so far, but the scanf is failing.
What am i doing wrong?
Later edit:
int **matrix;
int i=0;
int j=0;
int li=0;
int dim;
printf("What is the dimmension:");
scanf("%d",&dim);
matrix=(int **)malloc(sizeof(int *) * dim);
for(li=0;li<dim;li++)
{
matrix[li] = (int *)malloc(sizeof(int) * dim);
}
printf("Type the elements:\n");
for(i=0;i<dim;i++)
{
for(j=0;j<dim;j++)
{
scanf("%d", matrix[i][j]);
}
}
you should be able to access it like a regular 2d array
matrix[i][j]then forscanfuse the location of thatEdit also you need to allocate the pointers themselves:
Also if this is straight
Cyou shouldn’t cast the return type from malloc becauseCupgrades it automatically and the cast can hide a bug.