pointers #6

  • Follow


Hi everyone!..
 I'm back and sorry for these idotic questions.
 I need help on understanding the following program. When I run it I
got the answer 2 5, but I can't understand how it is happenning. Can
anybody help me on this.....

#include<stdio.h>

int main()
{
  int a[][3] = { 1,2,3 ,4,5,6};
  int (*ptr)[3] =a;

  printf("%d %d "  ,(*ptr)[1], (*ptr)[2] );

  ++ptr;
  printf("%d %d"  ,(*ptr)[1], (*ptr)[2] );

return(0);
}

0
Reply chuthurika (5) 2/11/2005 5:37:45 PM

Chathu wrote on 11/02/05 :
> Hi everyone!..
>  I'm back and sorry for these idotic questions.
>  I need help on understanding the following program. When I run it I
> got the answer 2 5, but I can't understand how it is happenning. Can
> anybody help me on this.....

Comments added:

#include<stdlib.h>
#include<stdio.h>

int main (void)
{
   /* Define an array of 2 arrays of 3 int's */
   int a[][3] =
   {
      {1, 2, 3},
      {4, 5, 6}
   };

   /* Define a pointer to an array of 3 int's
      Initialize it with the address of the previous array

      Actually, points to its first array of 3 int's.
   */
   int (*ptr)[3] = a;


   /* display the elements [1] and [2] of the first array */
   printf ("%d %d\n", (*ptr)[1], (*ptr)[2]);

   /* increment the pointer value. Due tu pointer arithmetics,
    * points to the second array
    */
   ++ptr;

   /* display the elements [1] and [2] of the second array */
   printf ("%d %d\n", (*ptr)[1], (*ptr)[2]);


   /* Dev-C++ trick (ignore it) */
   system ("pause");

   return 0;
}

-- 
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.

0
Reply emdel (952) 2/12/2005 8:41:02 AM


1 Replies
37 Views

(page loaded in 0.072 seconds)

Similiar Articles:
















6/23/2012 1:01:43 AM


Reply: