LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-05-2019, 01:52 PM   #1
srinietrx
Member
 
Registered: May 2013
Posts: 100

Rep: Reputation: Disabled
Passing double pointer to function to return value from function as argument


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("<////////Test1//////>\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?

Last edited by srinietrx; 10-05-2019 at 11:33 PM.
 
Old 10-05-2019, 09:13 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,508

Rep: Reputation: 1811Reputation: 1811Reputation: 1811Reputation: 1811Reputation: 1811Reputation: 1811Reputation: 1811Reputation: 1811Reputation: 1811Reputation: 1811Reputation: 1811
Quote:
Originally Posted by srinietrx View Post
1. Which one is proper way?
Test1 is wrong, it invokes undefined behaviour. Test2 is correct.

Quote:
2. In Mingw why I am not able to print twice correctly in case of Test1?
Test1 (or rather, the code just after the call to Test1) tries to print the value of a stack variable after it has been destroyed, so it may do anything. In Mingw it prints some bad value, on your Linux machine it appears to print the expected value, but you could also say it prints a garbage value that just happens to look like the right value.
 
2 members found this post helpful.
Old 10-06-2019, 12:55 AM   #3
srinietrx
Member
 
Registered: May 2013
Posts: 100

Original Poster
Rep: Reputation: Disabled
So,
In Test1 --> Address of stack allocated variable(i.e &a) is passed to *fiptr. This address goes out of scope in main function
In Test2 --> Address of heap allocated (i.e j) is passed to *fiptr.Value of j(address of a) get updated in Newiptr1 in main function.

Understood.

Last edited by srinietrx; 10-06-2019 at 12:58 AM.
 
  


Reply

Tags
c programming, pointers


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
difference between value *value and value * value PoleStar Linux - Newbie 1 11-26-2010 03:37 PM
warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type eCli Programming 1 01-19-2009 07:44 AM
warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast nasim751 Programming 2 02-10-2008 10:47 PM
warning: passing argument 2 of ‘memset’ makes integer from pointer without a cast nasim751 Programming 3 01-30-2008 09:16 AM
passing function pointer as argument worldmagic Programming 7 08-04-2004 03:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration