LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-30-2012, 07:46 AM   #1
rpittala
Member
 
Registered: Jan 2012
Location: PUNE
Distribution: SunOS sun4v sparc sun4v Solaris
Posts: 56
Blog Entries: 1

Rep: Reputation: Disabled
Red face What are the advantages of double pointer ? and I know some what about that


Hi All,
I know the advantage of the double pointer like, what I know about the double pointer is shown in the below code
#include<stdio.h>
#include<stdlib.h>
void double_fun(int **q)
{
*q=malloc(10);
**q=10;
}
int main()
{
int *p;
double_fun(&p);
printf("%d \n",*p);
}

PLEASE EXPLAIN ME SOME MORE POINTS ABOUT THE DOUBLE POINTER, OTHER THAN THE ABOVE CODE AND I AM EXPECTING THE ANSWERS WITH CODE EXAMPLES
 
Old 01-30-2012, 07:56 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
Quote:
Originally Posted by rpittala View Post
PLEASE EXPLAIN ME SOME MORE POINTS ABOUT THE DOUBLE POINTER, OTHER THAN THE ABOVE CODE AND I AM EXPECTING THE ANSWERS WITH CODE EXAMPLES
That sounds like a homework assignment from an incompetent instructor. If so, please don't ask us to do your homework, and you have my sympathies regarding your instructor.

If it isn't homework, it is still a stupid question, especially together with the "advantage of" viewpoint from the thread title.

C allows whatever level of pointer indirection you choose to code. You should use two levels whenever that is appropriate to the problem. A good instructor should think of problems that cause the specific technique being taught at the moment to be appropriate. A student should practice finding the techniques that fit a specified problem, not finding the problems that fit a specified technique.

Last edited by johnsfine; 01-30-2012 at 08:03 AM.
 
Old 01-30-2012, 08:02 AM   #3
rpittala
Member
 
Registered: Jan 2012
Location: PUNE
Distribution: SunOS sun4v sparc sun4v Solaris
Posts: 56
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
could you explain me why is it a stupid question ?

Quote:
Originally Posted by johnsfine View Post
that sounds like a homework assignment from an incompetent instructor. If so, please don't ask us to do your homework, and you have my sympathies regarding your instructor.

If it isn't homework, it is still a stupid question.
it is not a home work question!!!!
 
Old 01-30-2012, 08:06 AM   #4
rpittala
Member
 
Registered: Jan 2012
Location: PUNE
Distribution: SunOS sun4v sparc sun4v Solaris
Posts: 56
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
could you explain me why is it a stupid question ?

It is not a hone work assignment question and I would like to know the answer for the stupid question.
 
Old 01-30-2012, 09:53 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
1) If you want a function to be able to modify one of its input variables in C, you pass the address of that variable and the function works with that variable using one extra layer of indirection. If the variable itself is a pointer then the function works with a pointer to pointer.

An important area where this is common is in parsing text. In the top level of a parser, you would have a char* pointing to the spot in the text you are currently parsing. But a well modularized parser does most of the actual work in specialized helper functions (rather than directly in the top level parser). So those typically take a pointer to pointer to the text being parsed, so they can parse some text and advance the pointer across the text being parsed.

2) Multi dimensional arrays. Directly declaring multi-dimensional arrays in C only works well when the dimensions are known at compile time. So multi dimensional arrays are often implemented using an array of pointers to arrays of one less dimension. So in a three dimensional array, the top level data is a pointer to pointer. But even in a two dimensional array, it is often more effective to work with an "iterator" (pointer into the array) rather than an index. If each row is a pointer then a row iterator is a pointer to pointer.

There are many many other reasons for using a pointer to pointer, but I think the above two general cases are the most important for a beginner to understand.

As for the code samples you want, I think you would learn more if you tried to create them yourself. Otherwise, they could be found pretty easily with google.

Last edited by johnsfine; 01-30-2012 at 09:55 AM.
 
  


Reply


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
pointer to pointer segmentation fault Guilherme Programming 7 06-28-2009 10:47 AM
Double the desktop, not double the fun! bizshop SUSE / openSUSE 3 08-26-2005 01:22 PM
pass pointer,, return pointer??? blizunt7 Programming 3 07-23-2005 02:36 PM
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 02:20 PM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 07:40 AM


All times are GMT -5. The time now is 04:21 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