Article 4RX8Z Passing double pointer to function to return value from function as argument

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?latest?d=yIl2AUoC8zA latest?i=SrjOg4EFg9o:D0kWUlTzfbQ:F7zBnMy latest?i=SrjOg4EFg9o:D0kWUlTzfbQ:V_sGLiP latest?d=qj6IDK7rITs latest?i=SrjOg4EFg9o:D0kWUlTzfbQ:gIN9vFwSrjOg4EFg9o
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments