why it doesn't work?

C

Chief

hey you guys(and girls)....

if B is a 1D array (for instance int B[3]) and C is an int* pointer
(int*) then i can write C=B and everything will be cool.
but if B is a 2D array (for instance int B[3][5]) and C ia an int**
pointer (int**) then the same line , C=B, generates a warning
(incompatible types) and further use of C as a 2D array gets a runtime
error.....

y is this happening??? especially when we know that *(*(B+1)+2)
is legal !!


10x a lot,
the confused C-lover....
 
C

Chris Dollin

Chief said:
if B is a 1D array (for instance int B[3]) and C is an int* pointer
(int*) then i can write C=B and everything will be cool.
but if B is a 2D array (for instance int B[3][5]) and C ia an int**
pointer (int**) then the same line , C=B, generates a warning
(incompatible types) and further use of C as a 2D array gets a runtime
error.....

That's right.
y is this happening???

"Why". It's happening because B evaluates [1] to a pointer to its first
element, which is an array of 5 ints, so it's pointer-to-array-5-int,
not pointer-to-int.
especially when we know that *(*(B+1)+2)
is legal !!

Because *(B+1) [ie, B[1]] is an array-of-5-ints, which evaluates
to pointer-to-int.

Declare B appropriately. Isn't there a FAQ about this?

[1] In value context, ie not as the operand to sizeof or &.
 
C

CBFalconer

Chief said:
if B is a 1D array (for instance int B[3]) and C is an int*
pointer (int*) then i can write C=B and everything will be cool.
but if B is a 2D array (for instance int B[3][5]) and C ia an
int** pointer (int**) then the same line , C=B, generates a
warning (incompatible types) and further use of C as a 2D array
gets a runtime error.....

Of course. See the faq. Links below.
y is this happening??? especially when we know that
*(*(B+1)+2) is legal !!

You haven't defined the variable y. i.e. don't use foolish
abbreviations. Spelling counts.

--
Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
<http://www.dinkumware.com/refxc.html> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)
<http://clc-wiki.net> (C-info)
 
L

lane straatman

hey you guys(and girls)....

if B is a 1D array (for instance int B[3]) and C is an int* pointer
(int*) then i can write C=B and everything will be cool.
but if B is a 2D array (for instance int B[3][5]) and C ia an int**
pointer (int**) then the same line , C=B, generates a warning
(incompatible types) and further use of C as a 2D array gets a runtime
error.....

y is this happening??? especially when we know that *(*(B+1)+2)
is legal !!

10x a lot,
the confused C-lover....
That an array is 2-d doesn't mean that the pointer that has the most
relevance to it has 2 asterisks in its type. I think that my compiler
would say that it differs in level of indirection.

What part of C do you love the most? LS
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top