Question:

Why this doesn't work ?

by  |  earlier

0 LIKES UnLike



int ***p;

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

***p=12;

printf("%d",***p);

 Tags:

   Report

3 ANSWERS


  1. http://www.howstuffworks.com/c.htm/print...


  2. This leads to  Segmentation fault

  3. Yikes!

    int ***p is a pointer to a pointer to a pointer to an int.  However, it isn't ever set to point at a pointer to a pointer to an int.

    There isn't a pointer to a pointer to an int for it to point at.  And even if there was (a pointer to a pointer to an int), there isn't a pointer to a int for that to point at. But you do actually malloc an int. Confused?

    You would need to do this -

    int *p;

    int **pp = &p;

    int ***ppp = &pp;

    *p = malloc (sizeof (int));

    ***ppp = 12;

    printf("%d", *p);

    if you did -

    printf ("%d", ***ppp);

    that should work (I think). You can also do -

    printf ("%d", *pp);

    printf ("%d", *ppp);

    These would print the address of p, and pp respectively.  None of this is a particularly good idea.

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions