B
beginner
I have the follwing code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int k;
int **p;
p = (int **) malloc( 10* sizeof(int*));
// error checking
p[0] = (int *) malloc( sizeof(int));
p[1] = (int *)malloc( sizeof(int));
*(p[0]) = 1;
*(p[1]) = 2;
/* the following is ok */
printf("%d\n", *p[0]);
printf("%d\n", *p[1]);
/* the following is not ok */
k = 0;
while( p[k] )
{
printf("%d\n", *p[k]);
k++;
}
return 0;
}
I am wondering if using "while loop" in the case above is a good idea
or not.
valgrind complaint the value of p[2] has not been initialized yet
thanks
dwisianto
#include <stdio.h>
#include <stdlib.h>
int main()
{
int k;
int **p;
p = (int **) malloc( 10* sizeof(int*));
// error checking
p[0] = (int *) malloc( sizeof(int));
p[1] = (int *)malloc( sizeof(int));
*(p[0]) = 1;
*(p[1]) = 2;
/* the following is ok */
printf("%d\n", *p[0]);
printf("%d\n", *p[1]);
/* the following is not ok */
k = 0;
while( p[k] )
{
printf("%d\n", *p[k]);
k++;
}
return 0;
}
I am wondering if using "while loop" in the case above is a good idea
or not.
valgrind complaint the value of p[2] has not been initialized yet
thanks
dwisianto