Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!decwrl!pyramid!ctnews!mitisft!kemnitz From: kemnitz@mitisft.Convergent.COM (Gregory Kemnitz) Newsgroups: comp.lang.c Subject: Re: Indefinite-length array as member of struct; sizeof(char) Keywords: char string [] [0] [1] Message-ID: <786@mitisft.Convergent.COM> Date: 19 Jul 89 07:23:33 GMT References: <7360@c3pe.UUCP| <821@fozzy.UUCP> <14474@dartvax.Dartmouth.EDU> Reply-To: kemnitz@mitisft.UUCP (Greg Kemnitz) Organization: Unisys Network Computing Group, San Jose Lines: 61 In article <14474@dartvax.Dartmouth.EDU| ari@eleazar.dartmouth.edu (Ari Halberstadt) writes: |I sent the following article, but it seems to have arrived without its |body [probably would have forgotten its head if it wasn't attached :-)]. |I'm still new to the news networks, so I hope it doesn't appear twice. |The article follows: | |In article <821@fozzy.UUCP> ellis@fozzy.UUCP (Randy Ellis) writes: |>In article <7360@c3pe.UUCP>, charles@c3pe.UUCP (Charles Green) writes: |>> When I know how long the string is I'm pushing onto the stack, I say: |>> nodeptr = malloc(strlen(data)+5); |>> to cover the struct node* and terminating NULL, and then simply |>> strcpy(nodeptr->string, data); |> |>Does this fit your needs? Change string into a char pointer, then allocate |>dynamic memory for the string and save the pointer into nodeptr->string. |> nodeptr = malloc(sizeof(struct node)); |> nodeptr->string = malloc(strlen(data)+1); |> strcpy(nodeptr->string,data); | |The sollution given by Randy Ellis is incomplete. Specifically, it omits |a type cast from type "char *" to type "struct node *". The correct line |should read: | nodeptr = (struct node *) malloc(sizeof(struct node)); | |I also have a question about memory allocation. Are characters guaranteed |to be only one memory address long? This is very important in expressions |such as: | nodeptr->string = malloc(strlen(data)+1); |Should the above line really be written as: | nodeptr->string = malloc( (strlen(data)+1) * sizeof(char) ); |I have seen the latter in several books, but it really wasn't clear from the |examples what should be done. | On page 188 in K & R (First Edition): The "sizeof" operator yields the size, in bytes, of its operand. (A byte is undefined in the language except in terms of the value of sizeof. However, in all existing implementations a byte is the space required to hold a "char".) In my experience, I have never seen an implementation of C where sizeof(char) != 1. (Is there any out there????) However, I have seen many where a "byte" is not eight bits, like the Unisys 1100 and the Honeywell 6000 computers (both extremely old architectures). Since I have not used an ANSI C compiler, I don't know what ANSI says about sizeof(char), but if it is not 1 the ANSI committee should all be fired :-). The only other size definition I have seen (this has been talked about at length on another thread) is sizeof char <= sizeof short <= sizeof int <= sizeof long |If possible, please include a source (e.g., book name) with your response. |Ari Halberstadt '91, "Long live short signatures" ----------------------------------+-------------------------------------- Greg Kemnitz | Software without hardware is an idea. kemnitz@Convergent.COM | Hardware without software is a space heater. | | --Unknown author