Solved

pointer to pointer

Posted on 2007-04-06
3
446 Views
Last Modified: 2010-04-15
What is the real-time advantage of pointer to pointer in C programming...
it just looks confusing to me...

i found this code online:

int **p;
      int *q;

      p = (int **)malloc(sizeof(int *));
      *p = (int *)malloc(sizeof(int));
      **p = 12;
      q = *p;

why do we use pointer to pointer....
0
Comment
Question by:perlperl
3 Comments
 
LVL 7

Expert Comment

by:nixfreak
ID: 18863702
2 dimensional array is one common use.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 18863703

Hi perlperl,

The example that you've given isn't very good in that it shows that you CAN use a dynamic pointer to a pointer, but not why.

Consider a program that loads several tables.  If you don't know how big the table is you have two choices.  A predefined table that is at least as large as the largest table you expect to load, or build a dynamic table.  Keeping huge tables lying around mostly unused is wasteful and inefficient so dynamic tables is often the better choice.

int **MakeIntTable (int columns, int rows)
{
      int **p;
      int *q;
      int idx;

      p = (int **)malloc(sizeof(int *) * rows);
      for (idx = 0; idx < rows; ++idx)
        p[idx] = (int *)malloc(sizeof(int) * columns);

      return p;
}

The function will create a table of integers of any size that we want.  The pointer-to-pointer structure is a pointer to an array of pointers.  The array of pointers contains pointers to arrays of integers that is the actual data that we want to store in the table.


Good Luck,
Kent

0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 18863726
>> why do we use pointer to pointer....

Several situations ... Some examples :

1) a two-dimensional matrix :

        int **matrix = (int**) calloc(5, sizeof(int*));
        int i = 0;
        for (i = 0; i < 5; ++i) {
            matrix[i] = (int*) calloc(5, sizeof(int));
        }

    this creates a 5x5 matrix which can be accessed like this :

        matrix[3][2] = 22;

    note that you need to free the whole matrix :

        for (i = 0; i < 5; ++i) {
            free(matrix[i]);
        }
        free(matrix);



2) to pass an array to a function, that will be changed by the function :

        void fun(int **arr_ptr) {
            *arr_ptr = (int*) calloc(5, sizeof(int));
            int i = 0;
            for (i = 0; i < 5; ++i) {
                (*arr_ptr)[i] = i;
            }
            return;
        }

    call it like this :

        int *arr = 0;
        fun(&arr);
        /* the array has now been filled up with data by the function, and can be used */
        printf("%d\n", arr[2]);
        free(arr); /* clean up when the array is not needed any more */
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C programming help for beginner sort routine 5 261
Estimate porting issues. 9 118
C++ finding a sting in a char* string from a text file 3 99
Grammars for C C++ and java 1 114
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now