Hi guys,
can anybody explain to me with a example why we people emphasize to pass double pointer in a function instead a single pointer when we want to fill some variable inside that function.
or what are thge advantage of double pointers in c over single pointer?
johnray31 0 Junior Poster in Training
Recommended Answers
Jump to Postargv parameter of main is a good and common example of a double pointer -- it is a two dimensional array of strings. It can be coded in two ways and the use of the argv is identical in both version.
int main( int argc, char **argv) …
Jump to Post>>void foo( char * ptr)
The problem is that ptr is a local object which has scope only within the function foo(). So the memory allocated with malloc() will be tossed into the bit bucket as soon as foo() returns to main(), and that will cause a memory leak. …
Jump to PostBut Why this works !!!!!
That works because function foo() is not attemptig to modify a pointer that was declared in main(). It's perfectly acceptable for a function to return a pointer like it did in the code you posted.
Jump to PostBut Why this works !!!!!
That works because foo's ptr, which is a local scope variable, is returned, which means the memory allocated for it won't get lost (it would still hold the correct value anyway, but this way we don't lose its address).
However, you're not really modifying …
All 16 Replies
Infarction 503 Posting Virtuoso
johnray31 0 Junior Poster in Training
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
NicAx64 commented: Thanks for the example ,helps me alot +1
johnray31 0 Junior Poster in Training
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
johnray31 0 Junior Poster in Training
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster
jadavbheda 0 Newbie Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Alibeg commented: He answers everything...right. +1
xgdaniweb 0 Newbie Poster
xgdaniweb 0 Newbie Poster
Waldyrious 27 Newbie Poster
AumOrOm -4 Newbie Poster
Salem commented: a) you're wrong, as already explained, b) you're 3 years too late -4
anandmadhab -1 Newbie Poster
praveenmatanam 0 Newbie Poster
Bharat_1 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.