Array question

Thomas K. Kwong tomkwong at banana.ucsb.edu
Fri Feb 22 05:53:03 AEST 1991


In article <EbhAAgG00WBNM2PIt_ at andrew.cmu.edu> rg2c+ at andrew.cmu.edu (Robert Nelson Gasch) writes:
>
>    int *this_ptr;
>    this_ptr [0] = 1;
>    this_ptr [1] = 2;
>    . . . 
>    this_ptr [9] = 10;
>
>This works fine, but I really don't know why?? It seems you're using
>memory to store an array which was never really allocated. If anybody
>could briefly explain what exactly happens when you do this, I'd be
>greatly abliged as at this point I'm mystified. 

An array subscript is really a shorthand of a pointer notation:
	a[i]   is exactly the same as    *(a+i)
This conversion is done in compile time, so what you're doing is just
using the space allocated in previous malloc() statement.

-Thomas.

============================================================
		     *   
     o      |        *   --------------------
   -----    |-       *   tomkwong at cs.ucsb.edu
     \/     |        *   6600tom at ucsbuxa.edu 
  --------  --       *   --------------------
     __       |      *   
    |  |   |--       *   "Good work does not make a good
    |--|   |--       *    man, but a good man does good
    |--|   |--       *    work."
    |  |   |--       *                        -Martin Luther
      \|   ------\   *   
		     *	  :-) :-) :-) :-) :-) :-) :-) :-)
============================================================



More information about the Comp.lang.c mailing list