RicardasSim

non 0 based

Mar 21st, 2019
623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.80 KB | None | 0 0
  1. //-std=c99 -Wall -Wextra -Wpedantic -Wshadow
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. int main (int argc, char **argv){
  7.  
  8.     //it is illegal to refer to objects that are not within the array bounds
  9.    
  10.     int arr[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21};
  11.     int* tmp;
  12.    
  13.     tmp = arr;
  14.    
  15.     tmp-=10;
  16.    
  17.     printf("%d\n",tmp[10]);
  18.    
  19.     tmp+=21;
  20.  
  21.     printf("%d\n",tmp[0]);
  22.     printf("%d\n\n",tmp[-1]);
  23.    
  24.     //http://c-faq.com/aryptr/non0based.html
  25.    
  26.     //int realarray[10];
  27.     int realarray[10]={1,2,3,4,5,6,7,8,9,0};
  28.     int *array = &realarray[-1];
  29.    
  30.     printf("%p\n",(void*)realarray);
  31.     printf("%p\n",(void*)array);
  32.    
  33.     printf("%d\n",array[1]);
  34.    
  35. return 0;
  36. }
  37.  
  38. /*
  39. output:
  40.  
  41. 1
  42. 12
  43. 11
  44.  
  45. 0x7fff69ae6830
  46. 0x7fff69ae682c
  47. 1
  48.  
  49. */
Advertisement
Add Comment
Please, Sign In to add comment