Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!apple!netcom!teda!ditka!mcdchg!tellab5!balr!clrcom!rmartin From: rmartin@clear.com (Bob Martin) Newsgroups: comp.lang.c Subject: Re: confusion with char *a and char a[NUM] Message-ID: <1990Dec6.055844.5333@clear.com> Date: 6 Dec 90 05:58:44 GMT References: <7656@umd5.umd.edu> Distribution: na Organization: Clear Communications, Inc. Lines: 71 In article <7656@umd5.umd.edu> jjk@astro.umd.edu( Jim Klavetter) writes: > >I think part of the problem is with an inconsistency in c >and part is my understanding. > >I can have two identical files except that one I declare a to be > char a[NUM] >and the other has > char *a with a malloc of NUM+1 characters. > >I guess I can stop there and ask the general question, "what is the >difference between those two?" If done properly, they will both be >NUM+1 bytes (or whatever a char is) of memory and should be accessible >either by a[3] or *(a+3) for the forth element, for example. Yet, >there are differences. a[NUM] is an array of NUM chars. (NOT NUM+1 as the text implies). char *a is a pointer to a character, period. In the first case the compiler has knowledge about the number of bytes in 'a' and reserves those bytes for you. In the second case the compiler knows nothing about the size of the object you are pointing to and depends on you to explicitly allocate the bytes. > >In both cases, the following is accepted by both my sun4 compiler and >gcc > strcpy(string, a) >and there is no problem. Right. This is because of C's convention of treating the name of an array like a pointer to the first element of the array in an rvalue context. > >However, if I have > a=strchr(string, ":"); >I get the error message > 121: incompatible types in assignment >or some such thing (that one is from gcc). Right again. This is because 'a' is an lvalue in this statement. If 'a' was declared as an array, then in an lvalue context the name a represents the array, not a pointer at all. If you declared a as "char a[NUM]" you have set aside NUM bytes for your use. Ask yourself what you mean when you say: "a=strchr(string, ':');" 'a' is an array of NUM bytes, do you want to throw away those bytes and now ask 'a' to be a pointer to the innards of 'string'? > >The man page treats both the arguement of strcpy() and the return value >of strchr() as type (char *). So why this inconsistency? Am I using >strcpy() wrong above and just getting away with a flaw in the >compiler, or is there actually an inconsistency here. There is no inconsistency. The return value of strchr is char*, but 'a' (if declared as char a[NUM]) in the lvalue context is not a char*, it is a char[NUM]. > >Thanks in advance. > >jjk@astro.umd.edu >Jim Klavetter (accepting mail for Athabasca and Reudi) >Astronomy >UMD >College Park, MD 20742 -- +-Robert C. Martin-----+:RRR:::CCC:M:::::M:| Nobody is responsible for | | rmartin@clear.com |:R::R:C::::M:M:M:M:| my words but me. I want | | uunet!clrcom!rmartin |:RRR::C::::M::M::M:| all the credit, and all | +----------------------+:R::R::CCC:M:::::M:| the blame. So there. |