vi************@gmail.com wrote:
float (*ptr)[31][13][256][256], *p;
main()
{
float ****A = NULL;
int cnt, cnt1, cnt2, cnt3;
A = malloc(31* sizeof *A);
for(cnt = 0; cnt < 31; ++cnt) {
A[cnt] = malloc(130* sizeof **A);
}
for(cnt = 0; cnt < 31; ++cnt) {
for(cnt1 = 0; cnt1 < 130; ++cnt1) {
A[cnt][cnt1] = malloc(256 * sizeof ***A) ;
}
}
for(cnt = 0; cnt < 3; ++cnt) {
for(cnt1 = 0; cnt1 < 130; ++cnt1) {
for(cnt2 = 0; cnt2 < 256; ++cnt2) {
A[cnt][cnt1][cnt2] = malloc(256 * sizeof ****A);
}
}
}
ptr=&A[0];
}
when i do this its gives me assignment from incompatible pointer type
for the statement"ptr=&A[0]".If i replace array [A] with a static array
like say A [31][130][256][256]
i dont get any warning messages.Why am i egtting an error when iam
assigning the pointer tto the dynamic array?
Thanks
Well, this might sound a bit obvious, but there is indeed a type
mismatch here and that is why the compiler is giving you an error.
Before you call me crazy, let me explain. MOST OF THE TIME, you can use
pointers and arrays interchangeably. For instance, char * string and
char string[] can USUALLY be used interchangeably. If a function
expects an array of char, you can give it a char * and vise-versa.
*JUST WHAT IS A?*
When you defined A, you told the compiler that it is a pointer to a
pointer to a pointer to a pointer to a float. That means, when you try
and access the float, the computer will look in the address named by A
and expect to see a pointer(a memory address). When the computer jumps
to the address in that pointer it needs to know what type of data is
there. You told the compiler that the data to expect is yet another
pointer(memory address) and the computer will follow that. This will be
repeated three more times, at which time the computer expects to
finally find the float.
*ISN'T THAT WHAT ptr IS?*
No. ptr is a pointer to a four dimensional array of floats. That means,
when you use indirection to access data through ptr, the computer will
look in the memory address that is named by ptr. The computer expects
to see a pointer(memory address) which it will. So far, this is the
same kind of thing that A is. Now once the computer jumps over to the
address that is pointed to by ptr, the computer expects to see a bunch
of adjacent floats!!! Not another pointer!!!! That is what is different
about these two variables. This stuff is tricky, so watch out.
I recommend that you read a good book that discusses this subject at
length, such as Expert C Programming by Peter Van Der Linden.
P.S. There might be other errors in your code fragment here, be careful
how you are using sizeof!! I don't think it will return what you think!