LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   What are the advantages of double pointer ? and I know some what about that (https://www.linuxquestions.org/questions/linux-newbie-8/what-are-the-advantages-of-double-pointer-and-i-know-some-what-about-that-926481/)

rpittala 01-30-2012 06:46 AM

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

johnsfine 01-30-2012 06:56 AM

Quote:

Originally Posted by rpittala (Post 4588209)
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.

rpittala 01-30-2012 07:02 AM

could you explain me why is it a stupid question ?
 
Quote:

Originally Posted by johnsfine (Post 4588217)
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!!!!

rpittala 01-30-2012 07:06 AM

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.

johnsfine 01-30-2012 08:53 AM

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.


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