i’m using a pointer to pointer to intger as a 2 Dims array, i wrote these code but i failed on getting the integer value.
#include<stdio.h>
#include<conio.h>
void main(void)
{
int **pptr = 0, size = 0, size2 = 0, i, j;
clrscr();
printf("Enter Size of Main Store n");
scanf("%d", &size);
pptr = (int **) malloc(size * sizeof(int *));
printf("Enter Size of Sub Store n");
scanf("%d", &size2);
for (i = 0; i < size; i++) {
pptr[i] = (int *) malloc(size2 * sizeof(int));
}
printf("Enter Values n");
for (i = 0; i < size; i++) {
for (j = 0; j < size2; j++) {
scanf("%dn", pptr[i][j]);
}
}
clrscr();
printf(" Valuesn");
for (i = 0; i < size2; i++, pptr++) {
printf("%dn", *pptr + i);
}
getch();
}
it prints rubbish!!
scanf("%d", arg)expects a pointer toint, butyou pass it an
int, an uninitialisedintat that. The indeterminate value in that memory location is then interpreted as a pointer, and the scan tries to store the converted value who-knows-where. It is not unlikely that that will cause a segmentation fault.You should pass
&pptr[i][j]as the argument there.prints the
int**pptr +i, which is&pptr[0][i], using the%dformat that expects anintargument.You should
if you want to print the values of the diagonal (as seems to be the case, since you also increment
pptrin the loop), or, betteror, if you want to print the entire grid,