Path: utzoo!utgpu!water!watmath!clyde!att!ttrdc!levy From: levy@ttrdc.UUCP (Daniel R. Levy) Newsgroups: comp.lang.c Subject: Re: string assignment in C Message-ID: <2960@ttrdc.UUCP> Date: 16 Oct 88 03:26:42 GMT References: <1988Oct11.143728.28627@gpu.utcs.toronto.edu> <6777@chinet.chi.il.us> Organization: AT&T, Skokie, IL Lines: 34 In article <6777@chinet.chi.il.us>, john@chinet.chi.il.us (John Mundt) writes: > char p[] = { 't','h','i','s',' ','a',' ','s','t','r','i','n','g','\n' }; > > main() > { > printf(p); > } Be careful with this kind of declaration: without the terminating '\0' there is no guarantee where the "string" p[] ends. In fact, try this on a vax, 3b, or machine of similar architecture: char p[] = { 't','h','i','s',' ','a',' ','s','t','r','i','n','g',' ', ' ','\n' }; /* 16 bytes, NO NULL TERMINATOR */ char q[] = { 't','h','i','s',' ','g','a','r','b','a','g','e','\n','\0' }; main() { printf(p); } The output of the program will be: this a string this garbage The 16 bytes in p[] is to make it end just before the word boundary on which q[] begins. Otherwise the system will probably pad p[] with nulls and you won't notice the lack of explicit null terminator. -- |------------Dan Levy------------| THE OPINIONS EXPRESSED HEREIN ARE MINE ONLY | Bell Labs Area 61 (R.I.P., TTY)| AND ARE NOT TO BE IMPUTED TO AT&T. | Skokie, Illinois | |-----Path: att!ttbcad!levy-----|