I
Irrwahn Grausewitz
Hey, y'all.
While doing some pointer experiments I encountered
a problem. I know that I know the answer already,
but trying to remember I just screwed up my mind.
I wonder if someone would be so kind to enlighten
me, please.
Consider the code below; my specific questions
are embedded in the comment lines:
/* sample code for pointer-to-array problem */
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
int i;
int arr[5][10];
/*
** Declare 'arr' as 'array of 5 arrays of 10 ints',
** right?
*/
int (*ap)[10];
/*
** Declare 'ap' as 'pointer to array of 10 ints',
** right?
*/
ap = (int (*)[10])arr[0]; /* this works; accepted. */
ap = &arr[0]; /* this works, too; why? */
/*
** But if I try this:
** ap = arr[0];
** my compiler produces a diagnostic:
** "Warning: assignment from incompatible pointer type"
**
** arr[0] should already serve as a
** pointer to array of 10 ints, shouldn't it ???
*/
for ( i = 0; i < 5; i++ )
printf( "ap+%d %s &arr[%d]\n",
i,
(( ap+i == &arr ) ? "==" : "!=" ) ,
/* ^^^^^^^ WHY NOT: arr ??? */
i );
return EXIT_SUCCESS;
}
/* end sample code */
If this is a faq, I apologize for not being able to
find it in the faq list ...
Thanks,
Irrwahn
While doing some pointer experiments I encountered
a problem. I know that I know the answer already,
but trying to remember I just screwed up my mind.
I wonder if someone would be so kind to enlighten
me, please.
Consider the code below; my specific questions
are embedded in the comment lines:
/* sample code for pointer-to-array problem */
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
int i;
int arr[5][10];
/*
** Declare 'arr' as 'array of 5 arrays of 10 ints',
** right?
*/
int (*ap)[10];
/*
** Declare 'ap' as 'pointer to array of 10 ints',
** right?
*/
ap = (int (*)[10])arr[0]; /* this works; accepted. */
ap = &arr[0]; /* this works, too; why? */
/*
** But if I try this:
** ap = arr[0];
** my compiler produces a diagnostic:
** "Warning: assignment from incompatible pointer type"
**
** arr[0] should already serve as a
** pointer to array of 10 ints, shouldn't it ???
*/
for ( i = 0; i < 5; i++ )
printf( "ap+%d %s &arr[%d]\n",
i,
(( ap+i == &arr ) ? "==" : "!=" ) ,
/* ^^^^^^^ WHY NOT: arr ??? */
i );
return EXIT_SUCCESS;
}
/* end sample code */
If this is a faq, I apologize for not being able to
find it in the faq list ...
Thanks,
Irrwahn