dietz%slb-doll.csnet@CSNET-RELAY.ARPA (Paul Dietz) (04/02/86)
I don't remember if this was mentioned, but you can have pointers
to arrays in C without declaring multidimensional arrays. For example,
the program
main()
{
int (*x)[10];
printf("%d\n", sizeof(*x));
}
prints 40 (on a VAX). x doesn't point to anything, though.
dietz%slb-doll.csnet@CSNET-RELAY.ARPA (04/05/86)
>> int (*x)[10]; > >That is not a pointer to an array. It is an array of pointers to ints. > George Tomasevich, ihnp4!twitch!grt > AT&T Bell Laboratories, Holmdel, NJ It *is* a pointer to an array. Try running this program: main() { char (*x)[10]; printf("%d\n%d\n%d\n", sizeof(*x), sizeof(x), sizeof((*x)[2])); } It prints (on a VAX running ULTRIX): 10 4 1 So, *x is an object of size 10 (the array), x is an object of size 4 (a pointer), and (*x)[2] is a single byte (a char). C's strange type syntax strikes again. Paul Dietz dietz@slb-doll.csnet
jrv@siemens.UUCP (04/07/86)
>It *is* a pointer to an array. Try running this program: > > main() > { > char (*x)[10]; > printf("%d\n%d\n%d\n", sizeof(*x), sizeof(x), sizeof((*x)[2])); > } > >It prints (on a VAX running ULTRIX): > >10 >4 >1 > >So, *x is an object of size 10 (the array), x is an object of size 4 (a >pointer), and (*x)[2] is a single byte (a char). C's strange type >syntax strikes again. > >Paul Dietz >dietz@slb-doll.csnet >/* End of text from siemens:net.lang.c */ When I learned C an instructor showed me a trick to use to keep straight in one's mind what a particular data construction was. To find out what data type something is go back to the original declaration of it cover over what you have in the expression and what is left is the data type. For example using the program above. sizeof(*x) char (*x)[10]; cover ^^^^ and you get an array of 10 characters sizeof(x) char (*x)[10]; cover ^ and you get a pointer to an array of 10 characters sizeof((*x)[2]) char (*x)[10]; cover ^^^^^^^^ and you get a character So by this "convention" C's syntax is consistent and the sizeof() operator worked as I would expect. Admittedly this scheme works easily on simple data types (hey, it was an intro class :-)) and there are some expressions which I have seen in net.lang.c on which I did not even attempt it. Jim Vallino Siemens Research and Technology Lab. Princeton, NJ {allegra,ihnp4,seismo,philabs}!princeton!siemens!jrv
rbj@icst-cmr.ARPA (Root Boy Jim) (04/07/86)
>> int (*x)[10]; > >That is not a pointer to an array. It is an array of pointers to ints. > George Tomasevich, ihnp4!twitch!grt > AT&T Bell Laboratories, Holmdel, NJ It always amuses me when someone from TPC posts such an obviously bogus item about C. I guess I assume they can just walk right up to Dennis Ritchie and ask him if they're in left field or right. (Root Boy) Jim Cottrell <rbj@cmr>