Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with double pointers 1

Status
Not open for further replies.

Sunny2

Programmer
Sep 11, 2003
15
CA
Hi..I am confused with this double pointer stuff...could somebody explain please...

Well, I have this typedef structure:

typedef struct
{
int scan_number;
int num_returns;
float min_time;
float max_time;
Measurement *measurements; //another typedef Structure
} MeasurementFrame;

typedef MeasurementFrame** ScanList;

Now I have a function that has a parameter as (ScanList *S1)

for example:
function(ScanList *S1)
{
...
}

What does this really mean. See... MeasurementFrame is a structure, and then ScanList is a double pointer to Measurement Frame Rite???(not sure)...somebody told me when u have a double pointer, it's like a double matrix, but in this situation it's hard to imagine like that, so if somebody coudl explain to me, I really would appreciate it. Thank you in advance.

Sunny




 
This shows you how to use it, but doesn't really explain a great deal.
Study and feel free to ask for clarifications on the points you're unsure of.

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct
{
    int scan_number;
    int num_returns;
    float min_time;
    float max_time;
} MeasurementFrame;
typedef MeasurementFrame** ScanList;

#define ROWS    10
#define COLS    20

/* This function creates ScanList array[ROWS][COLS] using dynamic memory */
void function(ScanList *S1)
{
    ScanList    temp;   /* use a temp to remove a level of indirection */
    int         i,j;

    /* we use a temp because excessive * and () are tedious and error prone */
    /* (*S1) = malloc( ROWS * sizeof *(*S1) ); */
    temp = malloc( ROWS * sizeof *temp );
    for ( i = 0 ; i < ROWS ; i++ ) {
        temp[i] = malloc( COLS * sizeof *temp[i] );

        /* just fill in some data we'll recognise later */
        for ( j = 0 ; j < COLS ; j++ ) {
            temp[i][j].scan_number = i*j;
        }
    }

    /* assign our temp var to the location specified by the function parameter */
    *S1 = temp;
}

int main ( ) {
    ScanList foo;
    int      r, c;

    /* foo is a ScanList, and function wants a pointer to a ScanList object */
    /* so stick an apersand in front of the variable to take its address */
    function( &foo );

    /* just show what function() wrote in the structure */
    for ( r = 0 ; r < ROWS ; r++ ) {
        for ( c = 0 ; c < COLS ; c++ ) {
            printf( &quot;%3d &quot;, foo[r][c].scan_number );
        }
        printf( &quot;\n&quot; );
    }
    return 0;
}

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top