By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,107 Members | 1,281 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,107 IT Pros & Developers. It's quick & easy.

Help please

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.