Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output of a pointer program [closed]

Tags:

c

pointers

I know little about pointers.

I came across the following program. The output seems normal but what is actually going going on I could not figure it out.

#include<stdio.h>
#include<conio.h>
void main()
{
     int k;
     int a[] = {1,2,3}; int *b[3] ; int **c[3];
     int ***d[3]; int ****e[3]; int*****f[3];
     for (k = 0 ; k <3; k++)
     {
         b[k] = a + k; c[k] = b + k ; d[k] = c + k;
         e[k] = d + k ; f[k] = e + k;

     }
     for (k = 0 ; k <3; k++)
     {
        printf("%3d", *b[k]); printf("%3d", **c[k]);
        printf("%3d", ***d[k]); printf("%3d", ****e[k]);
        printf("%3d\n", *****f[k]);
     }
}
like image 802
Rohit Sthapit Avatar asked Aug 28 '26 23:08

Rohit Sthapit


1 Answers

The first for loop is just basic pointer arithmetic. a[] holds ints, each array after that holds a pointer.
b[] is a pointer to int
c[] is a pointer to pointer to int
etc

So it's something like this in memory:

Memory Address:      0x00441234 <---+   0x00441238 <----+     0x0044123C <---+
                     **********     |   **********      |     **********     |
var name:            * a (+0) *     |   * a (+1) *      |     * a (+2) *     |
                     **********     |   **********      |     **********     |
value:               *   1    *     |   *   2    *      |     *   3    *     |
                     **********     |   **********      |     **********     |
                                    |                   |                    |
                                    |                   |                    |
                  +-> 0x00442345    | +->0x00442349     | +->0x0044234D      |
                  |   ************  | |  ************   | |  ************    |
                  |   *  b (+0)  *  | |  *  b (+1)  *   | |  * b  (+2)  *    |
                  |   ************  | |  ************   | |  ************    |
                  |   *0x00441234* -+ |  *0x00441238* --+ |  *0x0044123C*  --+
                  |   ************    |  ************     |  ************     
                  |                   |                   |
                  |                   |                   |
                  |   0x00443345      | 0x00443349        | 0x0044334D      
                  |   ************    | ************      | ************    
                  |   *  c (+0)  *    | *  c (+1)  *      | * c  (+2)  *    
                  |   ************    | ************      | ************    
                  +-- *0x00442345*    +-*0x00442349*      +-*0x0044234D* 
                      ************      ************        ************     

And each element of D points to each element of C, and so on. The end result being you're setting each element in each of the arrays (via some chain of pointers) back to the elements of a. And then in the second for loop you're printing the elements of a[] over and over again.

like image 156
Mike Avatar answered Aug 31 '26 12:08

Mike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!