c - Printing address a pointer points to, value the address points to, and the address of the pointer itself -


i wrote little program familiarize myself pointers , relations between them little more. in order wrote small piece of code declares , initializes integer a, declares pointer *p address of a , goes on deeper assign pointer pointer *pp , on far ****pppp. if understood pointers correctly pointer pointer ... works this:

address of pointer (or integer) itself: 0x7fff08d1c658    0x7fff08d1c660    0x7fff08d1c668    0x7fff08d1c670    0x7fff08d1c67c                                               ↑                 ↑                  ↑                ↑                 ↑                                               pppp     -->      ppp       -->      pp      -->       p       -->       = 42                                               ↓                 ↓                  ↓                ↓                  address pointer points to:              0x7fff08d1c660    0x7fff08d1c668    0x7fff08d1c670    0x7fff08d1c67c 

the addresses on diagonal have identical because previous pointer points address of next pointer has been assigned. want check in program using printf() calls , here unsure if way print address more elaborate pointers **pp, ***ppp, , ****pppp point , how print addresses of these pointers correct. point out possible mistakes? here code followed output:

#include <stdio.h> #include <stdlib.h>  int main() {     int a;     int *p;     int **pp;     int ***ppp;     int ****pppp;      = 42;      /* take address of */     p = &a;      /* take address of p */     pp = &p;      /* take address of pp */     ppp = &pp;       /* take address of ppp */     pppp = &ppp;      printf("address of int &a:                             %p\n", &a);     printf("value of a:                                    %d\n\n", a);      printf("address p points via (void *)p:       %p\n", (void *)p);     printf("value *p points via *p:                %d\n", *p);     printf("address of *p via (void *)&p:           %p\n\n", (void *)&p);      printf("address pp points via (void *)pp:     %p\n", (void *)pp);     printf("value **pp points via **pp:            %d\n", **pp);     printf("address of **pp via (void *)&pp:        %p\n\n", (void *)&pp);      printf("address ppp points via (void *)ppp:   %p\n", (void *)ppp);     printf("value ***ppp points via ***ppp:        %d\n", ***ppp);     printf("address of ***ppp via (void *)&ppp:     %p\n\n", (void *)&ppp);      printf("address pppp points via (void *)pppp: %p\n", (void *)pppp);     printf("value ****pppp points via ****pppp:    %d\n", ****pppp);     printf("address of ****pppp via (void *)&pppp:  %p\n", (void *)&pppp);      return exit_success; } 

output:


address of int &a:                             0x7fff08d1c67c value of a:                                    42  address p points via (void *)p:       0x7fff08d1c67c value *p points via *p:                42 address of *p via (void *)&p:           0x7fff08d1c670  address pp points via (void *)pp:     0x7fff08d1c670 value **pp points via **pp:            42 address of **pp via (void *)&pp:        0x7fff08d1c668  address ppp points via (void *)ppp:   0x7fff08d1c668 value ***ppp points via ***ppp:        42 address of ***ppp via (void *)&ppp:     0x7fff08d1c660  address pppp points via (void *)pppp: 0x7fff08d1c660 value ****pppp points via ****pppp:    42 address of ****pppp via (void *)&pppp:  0x7fff08d1c658 

your code correct: understand printf prints value of pointers %p conversion specifier. actual output implementation specific can parsed pointer value scanf same %p specifier.

there 1 small detail got wrong in printf("address of int &a: %p\n", &a);: pointers should converted void * when passed printf value convert %p specifier. reason subtle: on architectures, pointers different types may have different representation, including different size, , may passed printf in different way. converting pointer void * ensures passed in form , manner expected printf function.

this conversion not automatic printf takes variable number of arguments of different types, these arguments passed in manner specific vararg functions: instance float values converted , passed double, various pointer types not converted void *, must write conversion explicitly (void *) cast.

examples of architectures different pointer representations tend less popular nowadays, older programmers may remember days of near , far pointers , various memory models function , data pointers had different size.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -