how to handle dereference operator in the following cases

Viewed 94

I have this int **i or int ***i or int ****k

how to assign value and to do printf to/of i, j and k?

also if I have

 struct abc { int *i; int **j; int ***k; };

then how to assign and print values to i, j, k if I have

  struct abc *b = ...;
  struct abc **c = ...;
  struct abc ***d = ...;

I assume malloc will never be needed.

update

I tried like this

int *i=malloc(sizeof(int)*2);
i[0]=5;
int **j=(int **)i;
int ***k=(int ***)j;
printf("%d\n",k[0][0][0]);

but at printf it throws segFault

And also

int *a=malloc(sizeof(int)*3);
int *b=malloc(sizeof(int)*3);
int *c=malloc(sizeof (int) *3);
int **i={a,b,c};
i[0]=a;
i[1]=b;
i[2]=c;
i[0][0]=5;
printf("%d\n",i[0][0]);

again segFault at printf

Update

and how to call scanf and pass i, j and k, and abc like in chqrlie answer ? and if I have a function like

void call_this(struct abc ***d)
{
   //how to assign and print
}
2 Answers

Here is an example without any malloc() calls:

#include <stdio.h>

struct abc { int *i; int **j; int ***k; };

int main() {
    int n;
    int *p = &n;
    int **pp = &p;
    int ***ppp = &pp;
    struct abc a = { p, pp, ppp };
    struct abc *b = &a;
    struct abc **c = &b;
    struct abc ***d = &c;

    n = 42;
    // all expressions ulimately point to the same integer
    printf("          n=%d\n", n);
    printf("         *p=%d\n", *p);
    printf("       **pp=%d\n", **pp);
    printf("     ***ppp=%d\n", ***ppp);
    printf("       *a.i=%d\n", *a.i);
    printf("    *(*b).i=%d\n", *(*b).i);        // same as *b->i
    printf("   *(**c).i=%d\n", *(**c).i);       // same as *(*c)->i
    printf("  *(***d).i=%d\n", *(***d).i);      // same as *(**d)->i
    printf("      **a.j=%d\n", **a.j);
    printf("   **(*b).j=%d\n", **(*b).j);       // same as **b->j
    printf("  **(**c).j=%d\n", **(**c).j);      // same as **(*c)->j
    printf(" **(***d).j=%d\n", **(***d).j);     // same as **(**d)->j
    printf("     ***a.k=%d\n", ***a.k);
    printf("  ***(*b).k=%d\n", ***(*b).k);      // same as ***b->k
    printf(" ***(**c).k=%d\n", ***(**c).k);     // same as ***(*c)->k
    printf("***(***d).k=%d\n", ***(***d).k);    // same as ***(**d)->k
    return 0;
}

Output:

          n=42
         *p=42
       **pp=42
     ***ppp=42
       *a.i=42
    *(*b).i=42
   *(**c).i=42
  *(***d).i=42
      **a.j=42
   **(*b).j=42
  **(**c).j=42
 **(***d).j=42
     ***a.k=42
  ***(*b).k=42
 ***(**c).k=42
***(***d).k=42

The parentheses in the above expressions are required because of precedence rules. Postfix unary operators are applied first from left to right, then prefix unary operators are applied from right to left. ***(***d).k is parsed as *(*(*(*(*(*d))).k)), which is even less readable.

Note however that it is considered bad style to use triple indirections, that are rarely needed in practice. Don't be a 3 star programmer

First we need to create object to assign addresses to that b, c and d variables

struct abc a; // create empty object

int tmp = 1; // create int to store it's address in a

a.i = &tmp; // store address of tmp in i
a.j = &a.i; // store address of i in j
a.k = &a.j; // store address of j in k

Then we can assign a's reference to b

struct abc *b = &a;

Then to c and d

struct abc **c = &b;
struct abc *** d = &c;

Now we have 1 object a and have direct address it via b, also we have direct address to b via c and so on...

We can also access a from c. First we need to get address of b

printf("%p", c); // address of b
printf("%p", *c); // address of a

struct abc a_cpy = **c; // create copy of object a
printf("%p", a_cpy.i); // address that was stored in the copy of a
Related