pointers

C

Chathu

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);
}
 
E

Emmanuel Delahaye

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top