I have declared pointer of pointers **a;
so In a loop I assign a block to a pointer and put a value in it
and then I want to print these values.
My following program doesnt work.
Also How to do same program with int *a[]; (array of pointers).
Any help would really help.
#include <stdio.h>
#include <conio.h>
no such standard include file. Remove this.
No such standard include file. You probably want stdlib.h
All bets are off. main returns int. Say so.
{
int **a;
int v=0;
for (v=0;v<5;v++)
The use of blanks is allowed, and adds legibility. No extra charge
is made for them.
for (v = 0; v < 5; v++)
etc.
{
*(a+v)=(int *)malloc(sizeof(int));
Do not cast the return value of malloc. It only hides errors.
Besides, how can the left side, which has no associated memory,
hold the result? a has been defined to hold one single pointer to
a pointer to an int, and has never been initialized. Who knows
what (a + v) refers to. Even if the program somehow survived the
earlier gross errors, this made it go KA-BOOM. The approved way to
use malloc is:
ptr = malloc(SOMENUMBER * sizeof *ptr);
followed by checking for success, i.e. (ptr != NULL). Note that
the *s above are not the same thing, one is a multiplicative
operator, and one is a dereferencing operator.
In your case you would need multiple mallocs, something like:
if (a = malloc(5 * sizeof *a)) {
for (n = 0; n < 5; n++) {
if (*(a + n) = malloc(sizeof *(a + n))) {
/* now you have a place to store an int */
}
else handlemallocfailure();
}
else handlemallocfailure();
and somewhere there should be a define for the magic number 5.
**(a+v)=v;
}
for (v=0;v<5;v++)
{
printf("%d\n",**(a+v));
}
exit(0);
}
If you want to post a followup via groups.google.com, don't use the
broken "Reply" link at the bottom of the article. Click on "show
options" at the top of the article, then click on the "Reply" at
the bottom of the article headers.
More details at: <
http://cfaj.freeshell.org/google/>
Also see <
http://www.safalra.com/special/googlegroupsreply/>
--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html