[SOLVED] What are the advantages of double pointer ? and I know some what about that
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
could you explain me why is it a stupid question ?
Quote:
Originally Posted by johnsfine
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.