Passing double pointer to function to return value from function as argument
by srinietrx from LinuxQuestions.org on (#4RX8Z)
Code:#include <stdio.h>
void Test1(int **fiptr);
void Test2(int **fiptr);
int main()
{
int *Newiptr = NULL;
int *Newiptr1 = NULL;
Test1(&Newiptr);
// printf("==>%d\n", *Newiptr);
printf("==>%d\n", *Newiptr);
free(Newiptr);
Test2(&Newiptr1);
// printf("==>%d\n", *Newiptr1);
printf("==>%d\n", *Newiptr1);
free(Newiptr1);
}
void Test1(int **fiptr)
{
printf("<////////Test6//////>\n");
*fiptr = (int*)malloc(sizeof(int));
int a = 10;
*fiptr =&a;
printf("--->%d\n", **fiptr);
}
void Test2(int **fiptr)
{
printf("<////////Test2//////>\n");
int *j = (int*)malloc(sizeof(int));
int a = 10;
*j = a;
*fiptr =j;
printf("--->%d\n", **fiptr);
}Issue with above program is
Test2 --> Print proper in function as well as in main function
Test1 --> Print proper in function but in main when I try to print twice in second time prints garbage value.
Also the problem arises only in Mingw not in Linux gcc.
Questions are
1. Which one is proper way?
2. In Mingw why I am not able to print twice correctly in case of Test1?


void Test1(int **fiptr);
void Test2(int **fiptr);
int main()
{
int *Newiptr = NULL;
int *Newiptr1 = NULL;
Test1(&Newiptr);
// printf("==>%d\n", *Newiptr);
printf("==>%d\n", *Newiptr);
free(Newiptr);
Test2(&Newiptr1);
// printf("==>%d\n", *Newiptr1);
printf("==>%d\n", *Newiptr1);
free(Newiptr1);
}
void Test1(int **fiptr)
{
printf("<////////Test6//////>\n");
*fiptr = (int*)malloc(sizeof(int));
int a = 10;
*fiptr =&a;
printf("--->%d\n", **fiptr);
}
void Test2(int **fiptr)
{
printf("<////////Test2//////>\n");
int *j = (int*)malloc(sizeof(int));
int a = 10;
*j = a;
*fiptr =j;
printf("--->%d\n", **fiptr);
}Issue with above program is
Test2 --> Print proper in function as well as in main function
Test1 --> Print proper in function but in main when I try to print twice in second time prints garbage value.
Also the problem arises only in Mingw not in Linux gcc.
Questions are
1. Which one is proper way?
2. In Mingw why I am not able to print twice correctly in case of Test1?