Path: utzoo!utgpu!news-server.csri.toronto.edu!me!writer Newsgroups: comp.lang.c From: writer@me.utoronto.ca (Tim Writer) Subject: Re: typedef-ing an array Message-ID: <90Jun28.203927edt.19451@me.utoronto.ca> Organization: University of Toronto, Department of Mechanical Engineering References: <78627@srcsip.UUCP> <78633@srcsip.UUCP> Date: 29 Jun 90 00:39:38 GMT In article <78633@srcsip.UUCP> pclark@SRC.Honeywell.COM (Peter Clark) writes: >typedef char foo[29]; >foo bar = "Hello World, this is my test"; >void > main() >{ > bar = "Silly old me"; > printf("%s\n",bar); >} >The initializer works fine, but the assignment inside main() doesn't. You are forgetting the difference between an array and a pointer. To quote from K&RII (p. 99), "a pointer is a variable .... But an array name is not." Therefore, you cannot assign an array, "Silly old me", to bar. If `bar' were a pointer, the compiler would simply assign the address of the first character, `S' to `bar'. But, `bar' is not a pointer. Your compiler should give a syntax error, something like `illegal lhs of assignment operator.' To prove this to yourself, try this. char foo[29]="Hello world"; main() { printf("%s\n",foo); foo="Goodbye world"; printf("%s\n",foo); } If this does not generate a syntax error, your compiler is buggy. Now try this. typedef char foo[29]; foo bar="Hello world"; main() { printf("%s\n",bar); strcpy(foo,"Goodbye world"); printf("%s\n",bar); } Tim