468,467 Members | 2,685 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,467 developers. It's quick & easy.

Help please

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

Jul 3 '06 #1
3 1021
vinod.bhavn...@gmail.com wrote:
float (*ptr)[31][13][256][256], *p;
.... snip ...

Multidimensional arrays are a common newbie problem, so it is a FAQ.
See:
<http://c-faq.com/>

Specifically:
<http://c-faq.com/aryptr/dynmuldimary.html>

Jul 3 '06 #2
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!

Jul 3 '06 #3
backasswards <ba**********@gmail.comwrote:
*JUST WHAT IS A?*
A is A, saith Ayn Rand.
I recommend that you read a good book that discusses this subject at
length, such as Expert C Programming by Peter Van Der Linden.
It seems to be a good text, but OP seems more likely to benefit from
K&R and the FAQ in the short term.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by the_proud_family | last post: by
12 posts views Thread by Christo | last post: by
5 posts views Thread by Craig Keightley | last post: by
17 posts views Thread by JT | last post: by
7 posts views Thread by tyler_durden | last post: by
23 posts views Thread by Jason | last post: by
2 posts views Thread by =?Utf-8?B?U2NvdHRSYWREZXY=?= | last post: by
1 post views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.