Triple pointers in C: is it a matter of style? -


i feel triple pointers in c looked @ "bad". me, makes sense use them @ times.

starting basics, single pointer has 2 purposes: create array, , allow function change contents (pass reference):

char *a; = malloc... 

or

void foo (char *c); //means i'm going modify parameter in foo. { *c = 'f'; }  char a; foo(&a); 

the double pointer can 2d array (or array of arrays, since each "column" or "row" need not same length). use when need pass 1d array:

void foo (char **c); //means i'm going modify elements of array in foo. { (*c)[0] = 'f'; }  char *a; = malloc... foo(&a); 

to me, helps describe foo doing. however, not necessary:

void foo (char *c); //am modifying char or passing char array? { c[0] = 'f'; }  char *a; = malloc... foo(a); 

will work.

according first answer this question, if foo modify size of array, double pointer required.

one can see how triple pointer (and beyond, really) required. in case if passing array of pointers (or array of arrays), use it. evidently required if passing function changing size of multi-dimensional array. array of arrays of arrays not common, other cases are.

so of conventions out there? question of style/readability combined fact many people have hard time wrapping heads around pointers?

using triple+ pointers harming both readability , maintainability.

let's suppose have little function declaration here:

void fun(int***); 

hmmm. argument three-dimensional jagged array, or pointer two-dimensional jagged array, or pointer pointer array (as in, function allocates array , assigns pointer int within function)

let's compare to:

void fun(intmatrix*); 

surely can use triple pointers int operate on matrices. but that's not are. fact they're implemented here triple pointers irrelevant user.

complicated data structures should encapsulated. 1 of manifest ideas of object oriented programming. in c, can apply principle extent. wrap data structure in struct (or, common in c, using "handles", is, pointers incomplete type - idiom explained later in answer).

let's suppose implemented matrices jagged arrays of double. compared contiguous 2d arrays, worse when iterating on them (as don't belong single block of contiguous memory) allow accessing array notation , each row can have different size.

so problem can't change representations now, usage of pointers hard-wired on user code, , you're stuck inferior implementation.

this wouldn't problem if encapsulated in struct.

typedef struct matrix_ {     double** data; } matrix;  double get_element(matrix* m, int i, int j) {     return m->data[i][j]; } 

simply gets changed to

typedef struct matrix_ {     int width;     double data[]; //c99 flexible array member } matrix;  double get_element(matrix* m, int i, int j) {     return m->data[i*m->width+j]; } 

the handle technique works this: in header file, declare incomplete struct , functions work on pointer struct:

// struct declaration no body.  struct matrix_; // optional: allow people declare matrix matrix* instead of struct matrix* typedef struct matrix_ matrix;  matrix* create_matrix(int w, int h); void destroy_matrix(matrix* m); double get_element(matrix* m, int i, int j); double set_element(matrix* m, double value, int i, int j); 

in source file declare actual struct , define functions:

typedef struct matrix_ {     int width;     double data[]; //c99 flexible array member } matrix;  double get_element(matrix* m, int i, int j) {     return m->data[i*m->width+j]; }  /* definition of rest of functions */ 

the rest of world doesn't know struct matrix_ contain , doesn't know size of it. means users can't declare values directly, using pointer matrix , create_matrix function. however, fact user doesn't know size means user doesn't depend on - means can remove or add members struct matrix_ @ will.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -