Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: sizeof(x) / sizeof(x[0]) Date: Sat, 14 Jan 2017 16:01:32 -0800 Organization: None to speak of Lines: 33 Message-ID: References: <1420cd8f-e5cb-4a8f-93e6-d80a8b050650@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="a3056e3d6a9ef9101ceee550b325befe"; logging-data="13715"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/FeqcBrkWTxbslCv8a7xm/" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:Qrb0TAEwPhajfVJzqAyxhbCD2s8= sha1:iT2ifmOm4VXpjiOjW/Ly9t6caJs= Xref: csiph.com comp.lang.c:101829 James Kuyper writes: [...] > char segmented_string[] = "abc\0defg\0hijklmop\0"; > > The array segmented_string contains all four of the strings that the > elements of your "array" pointed at, starting at segmented_string, > segmented_string+4, segmented_string+9, and segmented_string+18, > respectively. The expression "sizeof segmented_string" has a value of > 19, which is the total length of those strings, including their > terminating null characters. That array also contains 15 other strings, > such as "bc", which starts at segmented_string+1, and "p", which starts > at segmented_string+16. True, but I'd place a different emphasis. The "length" of a string by definition excludes the terminating null character (which is part of the string but does not contribute to its "length"). `sizeof segmented_string` does happen to be "the total length of those strings, including their terminating null characters", but it's simpler and more correct to say that it's the size of the array object, regardless of its contents. For example, if you had written: char segmented_string[42] = "abc\0defg\0hijklmop\0"; then `sizeof segmented_string` would be 42. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"